You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
.omit() doesn't do anything when using .passthrough(): the omitted keys are still there.
import{z}from'zod';// zod@3.20.6// Our schema, just two known keys `a` and `b`constschema=z.object({a: z.number(),b: z.number()});// We want to omit `b`, but preserve unknown keys// No luck here: we get { a: 1, b: 2, x: 654 }console.log(schema.omit({b: true}).passthrough().parse({a: 1,b: 2,x: 654}));// Still not what we want: { a: 1, b: 2, y: 286 }console.log(schema.passthrough().omit({b: true}).parse({a: 1,b: 2,y: 286}));// Workaround for expected result: { a: 1, z: 123 }console.log(schema.passthrough().transform(({ b, ...res})=>res).parse({a: 1,b: 2,z: 123}));
Am I doing something wrong? Am I not supposed to chain those two methods?
I understood that .passthrough() would just pass unknown keys, not omitted ones:
https://www.npmjs.com/package/zod#passthrough
By default Zod object schemas strip out unrecognized keys during parsing.
Instead, if you want to pass through unknown keys, use .passthrough().
The text was updated successfully, but these errors were encountered:
https://github.com/colinhacks/zod#pickomit .omit omits keys from the schema, not from the parsed/validated data. Therefore it makes b an unknown key, thus causing it to be passed through.
This is the way that I would do what you are trying to do:
.omit()
doesn't do anything when using.passthrough()
: the omitted keys are still there.Am I doing something wrong? Am I not supposed to chain those two methods?
I understood that
.passthrough()
would just pass unknown keys, not omitted ones:The text was updated successfully, but these errors were encountered: