-
-
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
Date type handling? #30
Comments
Implemented in zod@1.2.3 👍 Take it for a spin and let me know if you have any issues. |
Thanks! I see |
Hey Joe, So you want to be able to pass in, say, an ISO string to a Date field and have const Person = z.object({
createdAt: z.date()
})
const person = Person.parse({
createdAt: `2020-04-09T22:11:52.520Z`
}) // passes
person.createdAt // Date object I'm trying to keep this library from becoming a casting library, since casting is generally messy and difficult, especially when you're dealing with static types. Currently the above code wouldn't pass static validation, since I'd rather implement this is a refinement string type: const Person = z.object({
createdAt: z.string().refine({ is: 'isoString' });
}) I'm already planning to implement other built-in refinement validators ( This way you could parse your schema, then be confident that passing |
@joe-bruce Closing this for now. If you really prefer a lib that does type coercion check out myzod. |
Did you ever implement the refinement validation? |
You can do custom refinements now but there isn't a built-in way to check for valid ISO date strings because that can mean many different things. I recommend creating a custom refinement using a regex that does exactly what you want. Zod will also support transformations soon (#100) so the original use case will also be possible. |
Was about to request date/time/date-time formats and then stumbled upon this issue. Would it make sense to use the regex that AJV is also using? I am thinking of using refinement with these regexes thinking they would be very robust. But looking at the Stackoverflow answer shared above, @vriad do you mean that the regex to correctly handle all those cases is just isn't available yet? |
Feel free to open this as a separate issue. I'm open to implementing AJV's date/time/date-time regexes as built-ins. |
Great :) |
This lib looks like a really nice approach. I don't see support for
Date
types, though. Do you have a plan to support them?The text was updated successfully, but these errors were encountered: