Skip to content
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

Closed
joe-bruce opened this issue Apr 6, 2020 · 9 comments
Closed

Date type handling? #30

joe-bruce opened this issue Apr 6, 2020 · 9 comments

Comments

@joe-bruce
Copy link

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?

@colinhacks
Copy link
Owner

colinhacks commented Apr 7, 2020

Implemented in zod@1.2.3 👍

Take it for a spin and let me know if you have any issues.

@joe-bruce
Copy link
Author

Thanks! I see Date validation, but was hoping for Date parsing. (Something like https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse)
Am I barking up the wrong tree?

@colinhacks
Copy link
Owner

Hey Joe,

So you want to be able to pass in, say, an ISO string to a Date field and have .parse return a proper Date object in its place?

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, sincezod expects Person.createdAt to be a Date. To get it to work, I'd have to create a variant of the inferred schema type that changes all Date fields to Date | string, to accommodate the loosened restrictions on the input to .parse. Over time these "loosenings" would get messier and less intuitive.

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 ({ is: 'email' }, { is: 'url' }, etc.).

This way you could parse your schema, then be confident that passing Person.createdAt into Date.parse will return a valid date. Would that work for you?

@colinhacks colinhacks reopened this Apr 9, 2020
@colinhacks
Copy link
Owner

@joe-bruce Closing this for now. If you really prefer a lib that does type coercion check out myzod.

@TroyMorvant
Copy link

Did you ever implement the refinement validation?

@colinhacks
Copy link
Owner

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.

@maneetgoyal
Copy link

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?

@colinhacks
Copy link
Owner

Feel free to open this as a separate issue. I'm open to implementing AJV's date/time/date-time regexes as built-ins.

@maneetgoyal
Copy link

Great :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants