Z
Zod11mo ago
segfault

segfault - Hey! I'm currently testing my schema...

Hey! I'm currently testing my schemas and have encountered what I believe to be a typescript type definition issue. I am examining error cases where parsing a string with a regex results in an error. My goal is to verify that the failure validation was due to the regex and not some other reason. However, when accessing the error.issues field, which has the ZodIssue type, it does not recognize the validation field. I can see that it is present when I use a simple console.log. Is there a workaround to access validation with type checking activated? Here is an example of the schema. It works fine in JavaScript or when TypeScript type checking is disabled.
import { z } from "zod";

const Example = z.object({
exampleDate: z.string().regex(/\d{2}-\d{2}-\d{2}/),
});

try {
Example.parse({ exampleDate: "01/01/01" });
} catch (err) {
console.log(err.issues);
console.log(err.issues[0].validation)
}
import { z } from "zod";

const Example = z.object({
exampleDate: z.string().regex(/\d{2}-\d{2}-\d{2}/),
});

try {
Example.parse({ exampleDate: "01/01/01" });
} catch (err) {
console.log(err.issues);
console.log(err.issues[0].validation)
}
Solution:
Okay, I found the specific type definition for my case. The link to the error variants in the documentation page is broken (https://zod.dev/ERROR_HANDLING?id=zodissue), so it was a bit tricky.
GitHub
TypeScript-first schema validation with static type inference
TypeScript-first schema validation with static type inference
Jump to solution
1 Reply
Solution
segfault
segfault11mo ago
Okay, I found the specific type definition for my case. The link to the error variants in the documentation page is broken (https://zod.dev/ERROR_HANDLING?id=zodissue), so it was a bit tricky.
GitHub
TypeScript-first schema validation with static type inference
TypeScript-first schema validation with static type inference
Want results from more Discord servers?
Add your server