{"id":"0aa0664eed08f585","repo":"colinhacks/zod","slug":"not-a-zoderror-value","errorCode":null,"errorMessage":"Not a ZodError: ${value}","messagePattern":"Not a ZodError: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/zod/src/v3/ZodError.ts","lineNumber":273,"sourceCode":"            curr = curr[el];\n            i++;\n          }\n        }\n      }\n    };\n\n    processError(this);\n    return fieldErrors;\n  }\n\n  static create = (issues: ZodIssue[]) => {\n    const error = new ZodError(issues);\n    return error;\n  };\n\n  static assert(value: unknown): asserts value is ZodError {\n    if (!(value instanceof ZodError)) {\n      throw new Error(`Not a ZodError: ${value}`);\n    }\n  }\n\n  override toString() {\n    return this.message;\n  }\n  override get message() {\n    return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);\n  }\n\n  get isEmpty(): boolean {\n    return this.issues.length === 0;\n  }\n\n  addIssue = (sub: ZodIssue) => {\n    this.issues = [...this.issues, sub];\n  };\n","sourceCodeStart":255,"sourceCodeEnd":291,"githubUrl":"https://github.com/colinhacks/zod/blob/912f0f51b0ced654d0069741e7160834dca742ee/packages/zod/src/v3/ZodError.ts#L255-L291","documentation":"Thrown by the static guard ZodError.assert(value), which uses `value instanceof ZodError` and throws a plain Error if the assertion fails. It exists to narrow an unknown value to a ZodError before calling ZodError-specific methods like .format() or .flatten(). It is a developer-assertion error, not a validation failure from parsing.","triggerScenarios":"Calling ZodError.assert(x) on a value that is not a ZodError instance, e.g. an Error, a string, or undefined. Common when forwarding a caught value to formatting helpers or when crossing an API boundary that promises a ZodError but delivers something else.","commonSituations":"Error-handling middleware that wraps parse failures; test helpers that assume every thrown value from zod is a ZodError; code that catches `unknown` after a `.parse()` and forgets the surrounding code can also throw non-ZodErrors (e.g. config errors, thrown plain Errors).","solutions":["Type-narrow with `error instanceof ZodError` (or `import { ZodError }`) before formatting, instead of relying on assert().","If you need the assert, only call it inside a catch block where you truly expect a ZodError, and rethrow anything else.","Check the value's `.name` / `.issues` array if you cannot import ZodError in that module."],"exampleFix":"// before\ntry { schema.parse(data); }\ncatch (e) { ZodError.assert(e); return e.flatten(); }\n\n// after\nimport { ZodError } from \"zod\";\ntry { schema.parse(data); }\ncatch (e) {\n  if (e instanceof ZodError) return e.flatten();\n  throw e;\n}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"import { ZodError } from \"zod\";\nfunction isZodError(e: unknown): e is ZodError {\n  return e instanceof ZodError;\n}","tryCatchPattern":"try { schema.parse(data); }\ncatch (e) {\n  if (e instanceof ZodError) { /* format e */ }\n  else throw e;\n}","preventionTips":["Never forward caught values to ZodError-specific APIs without an instanceof check.","Type catch variables as `unknown` and narrow explicitly.","Treat assert() as a debug aid, not an input validator."],"tags":["zoderror","type-narrowing","assertion"],"analyzedSha":"912f0f51b0ced654d0069741e7160834dca742ee","analyzedAt":"2026-08-03T17:41:55.908Z","schemaVersion":2}