Zod - instanceof z.ZodError

try {
coursePOST.body.parse(requestBody);
} catch (error) {
if (error instanceof z.ZodError) {
setError(error.issues[0].message);
}
return;
}
try {
coursePOST.body.parse(requestBody);
} catch (error) {
if (error instanceof z.ZodError) {
setError(error.issues[0].message);
}
return;
}
For some reason, the error is not being recognized as an instanceof Zod and I have no idea why. Anything wrong I am not seeing?
3 Replies
ygor perez
ygor perez2y ago
Maybe try extracting the coursePost to it's own schema? This is from the docs:
try {
person.parse({
names: ["Dave", 12], // 12 is not a string
address: {
line1: "123 Maple Ave",
zipCode: 123, // zip code isn't 5 digits
extra: "other stuff", // unrecognized key
},
});
} catch (err) {
if (err instanceof z.ZodError) {
console.log(err.issues);
}
}
try {
person.parse({
names: ["Dave", 12], // 12 is not a string
address: {
line1: "123 Maple Ave",
zipCode: 123, // zip code isn't 5 digits
extra: "other stuff", // unrecognized key
},
});
} catch (err) {
if (err instanceof z.ZodError) {
console.log(err.issues);
}
}
Your code seems right, have you tried passing random things to be parsed?
Perfect
Perfect2y ago
I guess it’s a known bug if importing a zod object @ygor perez Just force typed it for now I guess
ygor perez
ygor perez2y ago
Good to know!
Want results from more Discord servers?
Add your server