Date validation
Question: Any good examples on how to do min/max date validation with
zod
?11 Replies
I would just use
refine
if you don't mind the ZodEffect
output.What I've ended up with currently is a file with various transformations and refinements, including these two for dates:
Used like this:
honestly, I think we'd be open to a
min
and max
method on the ZodDate
structure, I just don't think anyone has suggested it.To hook it into the errorMap and stuff it ended up being kind of weird though... Created a schema for custom issues:
I'm personally a minimalist when it comes to the core library, but I think the precedent is here already
GitHub
Request: ZodDate min and max · Issue #1089 · colinhacks/zod
I'm happy to give this a shot myself if you think it's worthwhile to add! I get that this can currently be achieved with a refinement: z.date().refine((d) => d >= new ...
cool, yeah, thanks for weighing in.
Yeah, I agree it makes sense to not go too crazy. Like, I don't mind at all to add my own validation of norwegian bank account numbers for example 😂
But, yeah, min and max for dates, are pretty basic and common I would say. Forms where you choose a date without any sort of limitation are quite rare I feel.
yeah, agreed.
FWIW, a lot of people look at Zod (especially coming from Yup) and wonder about it's lack of features like this, but I think if you consider what Zod is trying to be, it makes sense. Zod is a runtime equivalent to TypeScript types, not a form validation library. Lots of overlap there, but philosophically it tries to be very unopinionated about what kind of IO you're doing (forms, database, disk, etc etc)
Yeah, that's true
Thanks for the help (again). Maybe I'll look into that
min
/max
date issue, if I manage to get over my mild hatred of yarn
😛(same 🥲 )