-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Correct way to use a third party Class/type in a Zod schema object? #71
Comments
Hey Callum, It's definitely not obvious. It may make sense to add a method specifically for this use case, I'll add it to the list. In the meantime, it's already achievable but it's not super pretty: const TimestampSchema: z.ZodType<Timestamp> = z.any().refine(x => x instanceof Timestamp);
You can "hard code" the type you want to validate by casting to type Timestamp = z.infer<typeof TimestampSchema>;
const time: Timestamp = new Timestamp(); // works fine |
Your suggestion using Yes, it's not super pretty at first glance. It does make sense now I see it, but it feels like a clever trick. I agree that something like const x: = z.custom<MyType>((value: any) => {
// examine value and return true/false
}))
z.infer<x> // MyType One more thing while I'm here: I think the name Just to reiterate, I am in love with this library! Thank you for writing it. It's made writing TypeScript a joy. |
Lots of great ideas here! Just added all three of these things to my todo list 🤙 I follow up here when they're implemented. |
I was dreaming about it. Unfortunately `zod` does not provide any way to make a custom or branded or third-party schema that can be identified as one programmatically. Developer of `zod` also does not want to make any method to store metadata in schemas, instead, recommends to wrap schemas in some other structures, which is not suitable for the purposes of `express-zod-api`. There are many small inconvenient things in making custom schema classes, that I'd like to replace into native methods, and use `withMeta` wrapper for storing proprietary identifier, so the generators and walkers could still handle it. Related issues: ``` colinhacks/zod#1718 colinhacks/zod#2413 colinhacks/zod#273 colinhacks/zod#71 colinhacks/zod#37 ``` PR I've been waiting for months to merged (programmatically distinguishable branding): ``` colinhacks/zod#2860 ```
I'm trying to do this, but TypeScript complains:
Error:
I basically want to make a schema that asserts that
timeStart
is an instance of the classTimestamp
(which itself is a class I imported from a third party library).How can I do this? Sorry if it's obvious.
Thank you for writing this library by the way, it's by far the best solution for TypeScript runtime checking I've found (and I've tried many).
The text was updated successfully, but these errors were encountered: