Giter Club home page Giter Club logo

Comments (9)

colinhacks avatar colinhacks commented on May 23, 2024 3

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?

from zod.

colinhacks avatar colinhacks commented on May 23, 2024

Implemented in [email protected] 👍

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

from zod.

joe-bruce avatar joe-bruce commented on May 23, 2024

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?

from zod.

colinhacks avatar colinhacks commented on May 23, 2024

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

from zod.

TroyMorvant avatar TroyMorvant commented on May 23, 2024

Did you ever implement the refinement validation?

from zod.

colinhacks avatar colinhacks commented on May 23, 2024

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.

from zod.

maneetgoyal avatar maneetgoyal commented on May 23, 2024

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?

from zod.

colinhacks avatar colinhacks commented on May 23, 2024

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

from zod.

maneetgoyal avatar maneetgoyal commented on May 23, 2024

Great :)

from zod.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.