zod icon indicating copy to clipboard operation
zod copied to clipboard

Unable to chain min & max method on string

Open vinay-khatri opened this issue 1 year ago • 3 comments

I have created a custom zod Instance to check any string if it contains "undefined" or "null" as string.

import { z } from 'zod'

export const cZ = {
    ...z,
    string: () =>
        z
            .string()
            .refine((val) => val !== 'undefined' && val !== 'null', {
                message: 'string cannot be "undefined" or "null"',
            })
}

Now when i tried to use this custom zod instance i am unable to chain min and max methods on strings.

import { cZ } from '..utils/custom-zod.js'

const user = cZ.object({
    name : cZ.string().min(2).max(20)
})

vinay-khatri avatar Oct 11 '24 14:10 vinay-khatri

refine() returns a ZodEffects, but min() requires a ZodType.

sunnylost avatar Oct 14 '24 06:10 sunnylost

Instead make your custom string a different name: cString for example.

m10rten avatar Nov 03 '24 14:11 m10rten

You can achieve that now with Zod v4. Please close the issue if you can reproduce it.

Reference: https://v4.zod.dev/v4#refinements-now-live-inside-schemas

Christopher96u avatar Apr 11 '25 07:04 Christopher96u