colinhacks / zod

TypeScript-first schema validation with static type inference

Home Page:https://zod.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error Handling: differentiating .errors and .issues on ZodError

ducin opened this issue · comments

Whenever working with zod errors/issues I find out that issues and errors have exactly the same content, like below:

image

sample code below:

try {
Schema.parse(something);
} catch (e) {
  if (e instanceof z.ZodError) {
    for (const zodIssue of e.issues) { // A
    for (const zodIssue of e.errors) { // B
      console.error(zodIssue.code);
      console.error(zodIssue.message);
      console.error(zodIssue.path);
    }
  }
}

and inside internal TS zod types, both properties relate to the same ZodIssue type:

export declare class ZodError<T = any> extends Error {
    issues: ZodIssue[];
    get errors(): ZodIssue[];

My question is: what's the difference between them? Why are they separate? I see docs relate to issues (and methods such as addIssue/addIssues exist). What's the point of errors anyway?

I've read error handling but it doesn't answer this question.

It's there for backwards compatibility with some 1.x version from 2020 😅 I'm cleaning a lot of this up in Zod 4!