-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use instanceof
to class with private constructor
#384
Comments
Hm, interesting problem. Based on a quick review, it doesn't appear to be solvable inside of However, in your particular case you can easily build your own using const ServerTimestampSchema = z.custom<firebase.firestore.FieldValue>(
(data) => data instanceof firebase.firestore.FieldValue
); |
For those like me trying to validate a luxon import { z } from "zod";
import { Duration } from "luxon";
const IntervalValidator = z.custom<Duration>((t) => t instanceof Duration); |
Here are schemas for common Firestore FieldValue and Timestamp cases: import { FieldValue, Timestamp } from "firebase-admin/firestore";
import { z } from "zod";
// this is a Timestamp from the database
export const TimestampSchema = z.custom<Timestamp>(
(data) => data instanceof Timestamp
);
// this is a FieldValue instance, to be written to the database
export const FieldValueSchema = z.custom<FieldValue>(
(data) => data instanceof FieldValue
);
// this is a FieldValue, specifcally ServerTimestampTransform, to be written to the database
export const FieldValueServerTimestampSchema = z.custom<any>(
(data) => (data as Object).constructor.name === FieldValue.serverTimestamp().constructor.name
); |
first, I want to use zod with
firebase/firestore
. And there isFieldValue
class use forcreatedAt
orupdatedAt
.So, I think like this to use zod with firebase
maybe, this works at run time, but typescript compiler throw this message.
If someone has any solutions, please tell me!!
The text was updated successfully, but these errors were encountered: