Support for custom validation
See original GitHub issueIs there any plan to support custom validation?
It could be of the form: z.string().validate(someValidationFunction)
.
That would be a great help to build advanced schemas.
This could also take the form of custom types, but for now, z.ZodType
/ z.ZodTypeDef
not being exposed as part of the API make it a hack IMO.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Spring MVC Custom Validation - Baeldung
In this tutorial, we'll do just that; we'll create a custom validator to validate a form with a phone number field, and then...
Read more >Response Requirements & Validation - Qualtrics
Custom validation is used when you need your respondent to answer a question in a specific way. For example, you may want them...
Read more >Support for custom validation #37 - colinhacks/zod - GitHub
Is there any plan to support custom validation? It could be of the form: z.string().validate(someValidationFunction).
Read more >How To Use Custom Form Validation in Angular - DigitalOcean
Learn how to create a custom validator in Angular for both template-driven and reactive forms.
Read more >Custom validation - Prisma
This page explains how to add custom validation to Prisma Client.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I don’t see it in the documentation on this for some reason, but I’m wondering if this got added somewhere.
I was poking around and found a .custom on the z object, and got it to work by doing the following.
Obviously that’s not a real phone validation, but the logic works for implementing a custom validation. You can ignore the transform key, and also the email in the validation. I just like providing a little extra context with these answers.
Update
I think this approach using
refine
is probably better because then zod can first verify the type, then refine that type. As a side bonus you can use the input value to make your error message which seems coolIt also means you can infer the schema type which you can not do with
z.custom
This is doable. I’m working on a big rewrite currently (probably gonna be Zoe 2) and I’ll include this in that release.