-
-
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
Support for date, time and date-time format strings #126
Comments
Agreed! Although date and time validation is a tricky issue as different developers expect different data (ISO? YYYY-MM-DD? 12h Time? With milliseconds? Timezone notation?). |
|
I use the |
Hey @vriad are you still open to having AJV date/time/date-time regexes as built-ins? If so I'll open a PR sometime this week. |
RFC for datetime methodsThe hard problem here is naming the methods such that the validation behavior is clear to everyone. I'm opposed to adding a method called Here is a set of method names I'd be happy with:
The longer method name Milliseconds will be supported but not required in all cases. |
Could something be achieved in user land via something akin to yup's It would be great to add in custom methods of built-in types to allow extensions. |
I don't know how to implement |
I stumbled across this comment where you recommend a subclass. Given that could we achieve the same as above like so?
then consume...
|
The general approach works but there are some problems with this implementation. You'd have to return an instance of import * as z from '.';
import { parse, parseISO } from 'date-fns';
function dateFromString(value: string): Date {
return parse(value, 'yyyy-MM-dd', new Date());
}
function dateTimeFromString(value: string): Date {
return parseISO(value);
}
export class MyZodString extends z.ZodString {
static create = () =>
new MyZodString({
t: z.ZodTypes.string,
validation: {},
});
date = () => this.transform(z.date(), dateFromString);
iso8601 = () => this.transform(z.date(), dateTimeFromString);
}
const stringType = MyZodString.create;
export { stringType as string }; |
Yes, thanks for the reply. Looks like I have some copypasta issues and didn't update the names. Thanks for confirming! |
@colinhacks what about using the built-in const User = z.object({
name: z.string(),
birthDate: z.date.fromString(),
})
User.parse({ name: 'Alice', birthDate: '1980-01-01' })
// -> ok, returns { name: 'Alice', birthDate: [[Date object]] }
User.parse({ name: 'Bob', birthDate: 'hello' })
// -> Error `'hello' could not be parsed into a valid Date`
User.parse({ name: 'Bob', birthDate: 123 })
// -> Error `123 is not a string` |
This should be implemented! |
Any updates on this? |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
It'd be great to have this as proposed in the RFC. Or if we could just easily extend it with |
What if this were implemented
I 100% agree with implementing validators that enforce best practices (i.e. iso8601 strings) for Date objects, but perhaps a more useful feature would be validators that conform to the TC39 proposal for the Temporal global. I'd certainly feel more comfortable using the polyfill if zod provided a set of validators for the proposal! |
👋 I know this has been a long time since this has been discussed. I'm fairly happy to implement some of this but I think However, in terms of implementation if we wanted to enable both we could go with something like z.string().utc(); // accept both milliseconds and no milliseconds
z.string().utc({ milliseconds: false }); // accept only no milliseconds
z.string().utc({ milliseconds: true }); // accept only milliseconds |
It isn't implemented (colinhacks#126)
Support for a configurable
|
@colinhacks
|
I can speak to this but the initial regex was based on a StackOverFlow post which referenced the W3 spec for date time. It's also the default format for |
ISO-8601 is actually |
Does it mean that there are non-default options (for the ones that doesn't end in |
@colinhacks Can they though? |
Feel like you cut the quote off short there. He does mention that it can be add down the road. Feel free to contribute if you'd like. You can probably use the existing datetime regex to figure something out though I don't think that's perfect either.
It's literally in the section below where you just read? |
|
@asnov There is an open issue for supporting this #2385 And I've just opened a PR to support non-UTC time here #2913 |
Having |
@mbsanchez01 Similar concerns -- I'm using https://github.com/astahmer/typed-openapi for Zod object generation based on openapi spec. Is there a tool you're using to properly generate Zod date objects from openapi? Zod generator recognizes them as just strings. Have an open issue here for generation of |
Hey @tonydattolo I'm using |
date of type - |
Currently, Zod is lacking support for validating date, time and date-time stamps. As a result, users may need to implement custom validation via
.refine
. But the use case for date/time/date-time is very common, so if Zod ships it out-of-the-box like it does foruuid
,email
, etc, it would be helpful.The text was updated successfully, but these errors were encountered: