question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for custom validation

See original GitHub issue

Is 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

14reactions
jjhiggzcommented, Jun 23, 2022

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.

const isValidPhoneNumber = (phonenumber: string) => phonenumber.length > 3

const SignupValidation = z.object({
  email: z.string().email(),
  cell: z
   // Input 1 seems to be your validator function, which should return true or false depending on the input
   // Input 2 seems to be how you control what messages show up
    .custom(isValidPhoneNumber, { message: "Not a valid phone number" })
    .transform(parseInt),
})

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 cool

    const isPhoneNumber = (ph: string) => ph.length > 4;
    const phoneNumberSchema = z.string().refine(isPhoneNumber, (val) => ({
      message: `${val} is not a valid phone number`,
    }));

It also means you can infer the schema type which you can not do with z.custom

1reaction
colinhackscommented, Apr 23, 2020

This is doable. I’m working on a big rewrite currently (probably gonna be Zoe 2) and I’ll include this in that release.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found