Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How get values inside min(), max() in zod?

I have the following schema.

const schema = z.object({
 name: z.string().min(1)
})

Is there any way in zod to get the value stored in min?

const minValue = schema.shape...? // should be 1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Yes, after a little poking around, there’s a hidden _def field that you probably need to //@ts-ignore:

const minValue = schema.shape.name._def.checks[0].value;

If you have more than one check, you can find the one you want:

const minValue = schema.shape.name._def.checks.find(({ kind }) => kind === "min").value;

Note that find will return undefined if no such check was found.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading