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)
- Type Inference not working HOT 1
- add `actual` for `invalid_string` error in `.regex()` HOT 2
- Add a Real Example of File Upload Validation to Documentation HOT 1
- Recursive types losing types and get `any`
- Feature Request: Autoscroll to Input Field on Validation Error HOT 1
- Documentation example for recursive types gives type error
- Does anyone know how to conditionally validate form fields? HOT 2
- [Bug ?] Zod union ignoring zod object when fields are z.optional() HOT 3
- How to use discriminatedUnion with multiple conditions? HOT 1
- [Bug]: ZodFormattedError type breaks for ReadonlyArrays.
- safeParse not working server sider with z.union() : Error: Attempted to call indexOf() from the server but indexOf is on the client. HOT 1
- If object with nested refine fails, it doesn't apply transform before superRefine, causing superRefine to not be typesafe
- ZodReadonly missing methods to manipulate schema shape HOT 1
- Generic type function converting property with a default value to optional
- Lowercase ULIDs should be considered valid ULIDs HOT 1
- Valid union-of-partials data does not parse even though it conforms to one (or more) of the union members HOT 1
- Changelog.md looks abandoned since version 3.10 HOT 1
- Request to support 'hostname' string validation HOT 5
- Zod .partial() no longer able to use programmatically HOT 1
- Overloaded function return schema with z.union
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.