zod icon indicating copy to clipboard operation
zod copied to clipboard

v4: .refine no longer passes through refined type

Open blackdynamo opened this issue 6 months ago • 3 comments

Expected:

Image

Actual:

Image

TSPlay to reproduce

blackdynamo avatar May 19 '25 22:05 blackdynamo

This is documented in the changelog here:

Image

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.

colinhacks avatar May 19 '25 23:05 colinhacks

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)

jesselatimer avatar May 21 '25 21:05 jesselatimer

that was really useful @jesselatimer thanks for the snippet

edcaron avatar May 23 '25 18:05 edcaron

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.

rhinodavid avatar Jun 13 '25 18:06 rhinodavid