Closed sjdonado closed 4 months ago
Is this what you are looking for? https://github.com/colinhacks/zod#parseasync
try {
const schema = z.object( { name: z.string(), age: z.number() } )
await schema.parseAsync( { name: null, age: 42 } )
} catch ( error ) {
console.log( ( error as any ).issues )
// [
// {
// code: "invalid_type",
// expected: "string",
// received: "null",
// path: [ "name" ],
// message: "Expected string, received null",
// }
// ]
}
Is this what you are looking for? https://github.com/colinhacks/zod#parseasync
try { const schema = z.object( { name: z.string(), age: z.number() } ) await schema.parseAsync( { name: null, age: 42 } ) } catch ( error ) { console.log( ( error as any ).issues ) // [ // { // code: "invalid_type", // expected: "string", // received: "null", // path: [ "name" ], // message: "Expected string, received null", // } // ] }
The error is the same, but the response is not flat, it always returns { success, data, errors }
, so it's kinda misleading to have a try catch and inside a if (result.success)
check.
In the example that I gave, I don't see { success, data, errors }
, so I don't understand what you are talking about. I also don't see if (result.success)
. Please explain.
@JacobWeisenburger you right, this is not necessary. My bad, we can close it
I found this useful when you have to do several validations in a row
very open to feedback :)