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

Time:09-21

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

CodePudding user response:

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.

  • Related