zod
zod copied to clipboard
Unable to chain min & max method on string
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)
})
refine() returns a ZodEffects, but min() requires a ZodType.
Instead make your custom string a different name: cString for example.
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