-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Order of z.object props with preprocess gives wrong parse results #3123
Comments
Was just investigating this as well. I believe the behavior changed with #2426, specifically this bit (all comments are mine): // This runs preprocess
const processed = effect.transform(ctx.data, checkCtx);
// Early return if there are any issues on `ctx`
if (ctx.common.issues.length) {
return {
status: "dirty",
value: ctx.data,
};
}
if (ctx.common.async) {
return Promise.resolve(processed).then((processed) => {
return this._def.schema._parseAsync({
data: processed,
path: ctx.path,
parent: ctx,
});
});
} else {
// This runs the "inner" schema
return this._def.schema._parseSync({
data: processed,
path: ctx.path,
parent: ctx,
});
}
} |
Perhaps this will help: const schemaFixed = z.object( {
first: z.string(),
second: z.any().transform( () => undefined ).pipe( z.string() ),
} )
// works as expected
const result = schemaFixed.safeParse( {} )
!result.success && console.log( result.error.issues )
// [
// {
// code: "invalid_type",
// expected: "string",
// received: "undefined",
// path: [ "first" ],
// message: "Required",
// }, {
// code: "invalid_type",
// expected: "string",
// received: "undefined",
// path: [ "second" ],
// message: "Required",
// }
// ] const schemaFixed = z.object( {
second: z.any().transform( () => undefined ).pipe( z.string() ),
first: z.string(),
} )
// works as expected
const result = schemaFixed.safeParse( {} )
!result.success && console.log( result.error.issues )
// [
// {
// code: "invalid_type",
// expected: "string",
// received: "undefined",
// path: [ "second" ],
// message: "Required",
// }, {
// code: "invalid_type",
// expected: "string",
// received: "undefined",
// path: [ "first" ],
// message: "Required",
// }
// ] If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏 |
|
Version: 3.22.4
It seems that Zod is unable of properly validating props with
preprocess
if they are not placed at the beginning of the Zod Object.The following schema should always return an error for
second
.If we switch the order of the props, then it works:
Playground: https://stackblitz.com/edit/typescript-wqj5ep?file=schemas%2FschemaWithDefaultType.ts
Note: Version 3.21 works correctly
The text was updated successfully, but these errors were encountered: