-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
zod.preprocess v3.22.2 issues are ignored in case of another error in the model #2677
Comments
I just stumbled over this one as well. A simple test case to reproduce: const result = z
.array(z.preprocess((v) => v, z.number()))
.safeParse(['a', 'b'])
expect((result as z.SafeParseError<unknown>).error.errors).toStrictEqual([
{
code: 'invalid_type',
expected: 'number',
received: 'string',
path: [0],
message: 'Expected number, received string',
},
{
code: 'invalid_type',
expected: 'number',
received: 'string',
path: [1],
message: 'Expected number, received string',
},
]) This test fails because I can also confirm that the latest version where this worked as expected was |
fix with #2719 ? |
This seems to be a bug with const wrapToArray = <T extends z.ZodTypeAny> ( schema: T ) =>
z.any().transform( x => [ x ] ).pipe( schema.array() )
const schema = z.object( {
foo: z.number(),
bar: wrapToArray( z.object( { qux: z.number() } ) ),
} )
const result = schema.safeParse( { foo: 'invalid', bar: 'invalid' } )
!result.success && console.log( result.error.issues )
// [
// {
// code: "invalid_type",
// expected: "number",
// received: "string",
// path: [ "foo" ],
// message: "Expected number, received string"
// }, {
// code: "invalid_type",
// expected: "object",
// received: "string",
// path: [ "bar", 0 ],
// message: "Expected object, received string"
// }
// ] If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏 |
Hey @JacobWeisenburger, your solution works for small projects, but we currently have multiple projects/services with lots of schemas using Would the fix proposed in #2719 be good enough to merge and release a quick fix? Multiple people are trying to update to latest version of Zod to fix the Email Regex ReDoS vulnerability but this preprocess bug introduced in https://github.com/colinhacks/zod/releases/tag/v3.22.0 is a breaking change... |
#2719 seems good enough. |
Seems to be fixed in 3.23.0 with #2912 |
It seems to be fixed in version 3.23.0—it works for me now. For reference, here’s a simple bug reproduction using version 3.22.4: https://stackblitz.com/edit/typescript-t8mtna?file=index.ts. |
Another issue we found in v3.22.2 is that the schema inside
preprocess
either isn't executed or doesn't add issues to the context in case the model being parsed contains an error in another field (nonNullableObject
below):An error in
nonNullableObject
hides thepreprocess
errors. Also, if I remove thepreprocess
call completely the behaviour becomes correct. This doesn't look right as preprocess stops errors from being populated.3.21.4:
3.22.2:
The text was updated successfully, but these errors were encountered: