TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Object'
| property 'anObjectProperty' -> object with constructor 'Object'
--- property 'cicrularObject' closes the circle
at JSON.stringify (<anonymous>)
at get message [as message] (node_modules/zod/lib/ZodError.js:105:21)
at Context.<anonymous> (test/00.zodIssue.js:23:11)
at process.processImmediate (node:internal/timers:478:21)
get message() {
return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);
}
JSON.stringify is called on this.issues. JSON.stringify cannot cope with circular data structures. In the example, the circular data structure appears in the received property of the issue.
jsonStringifyReplacer does not deal with this either:
Good catch. For security reasons, Zod isn't supposed to print the input data when it throws an error...but you've found the exception here with ZodInvalidLiteralIssue. This will be fixed in Zod 4.
Example
A Mocha test that shows the issue:
This test fails with
Analysis
ZodError.ts, line 283:
JSON.stringify
is called onthis.issues
.JSON.stringify
cannot cope with circular data structures. In the example, the circular data structure appears in thereceived
property of the issue.jsonStringifyReplacer
does not deal with this either:helpers/util.ts#jsonStringifyReplacer, line 91: