Any way to print the provided value when zod parse fails? #1804
-
|
Hi folks! I'm using Zod to validate API response shape in my node scripts. Is there currently any built-in way to show a jest-style diff that shows expected value vs received value? All I'm finding currently is ways to print the error message like "your input is too low". I'm looking for: Anything like this already exist in Zod? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
|
I think what you want is const schema = z.string()
const result = schema.safeParse( 123 )
!result.success && console.log( result.error.issues )
// [
// {
// code: "invalid_type",
// expected: "string",
// received: "number",
// path: [],
// message: "Expected string, received number"
// }
// ]Let me know if you have any questions. |
Beta Was this translation helpful? Give feedback.
-
|
I don't think there is anything built into zod that does that. However, I have made this: |
Beta Was this translation helpful? Give feedback.
-
|
Looks like this was added to zod for v4 https://zod.dev/error-customization?id=include-input-in-issues |
Beta Was this translation helpful? Give feedback.
I don't think there is anything built into zod that does that. However, I have made this:
https://gist.github.com/JacobWeisenburger/d5dbb4d5bcbb287b7661061a78536423