zod
zod copied to clipboard
v4: .refine no longer passes through refined type
This is documented in the changelog here:
Refinements now live inside schemas (this is also described in more detail in the announcement post) which means there's no easy way for them to alter the inferred types.
From the discord: A combination of custom and pipe should do what you need now that refine doesn't work.
type Id = `${string}-${number}`
const idString = z.custom<Id>((val): val is Id => /\w+-\d+/.test(val))
const schema = z
.string()
.pipe(idString)
that was really useful @jesselatimer thanks for the snippet
Coming here after the robot pointed me from https://github.com/colinhacks/zod/discussions/4706.
One affect of this design is that .omit and .pick now disable checks silently. See discussion for more.