Comments (9)
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.
Implemented in [email protected] 👍
Take it for a spin and let me know if you have any issues.
from zod.
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.
@joe-bruce Closing this for now. If you really prefer a lib that does type coercion check out myzod.
from zod.
Did you ever implement the refinement validation?
from zod.
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.
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.
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.
Great :)
from zod.
Related Issues (20)
- Default generates same value for object array when passed a function HOT 1
- z.infer doesn't infer types correctly HOT 2
- Cannot use z.infer. Property 'infer' does not exist on type HOT 1
- deepPartial doesn't work with superRefine HOT 7
- Empty error message gets evaluated as falsy and overriden
- Getting a weird issue with parsing my zod schema HOT 1
- add method to find if the given field is required HOT 5
- TypeScript Mismatch using Pick or Omit HOT 1
- `custom` should exist on the method chain instead of only at the root HOT 4
- Wrong type after parse HOT 1
- Feature Request | Question: Reference the input value in the error message? HOT 1
- The required error message is not functioning properly HOT 5
- feat: function implement with comment HOT 3
- Discussion: Simplify `.safeParse()` response type to include `error: null` for a better dev experience
- Order of z.object props with preprocess gives wrong parse results HOT 3
- TypeScript Error in Discriminated Union HOT 1
- Feature request: allow .required() and .partial() on discriminatedUnion HOT 4
- String tag validation
- Type Narrowing Not Retained in Chained Array Methods with safeParse HOT 1
- Type error when combining `catchall` and `array` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zod.