You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/* eslint-env mocha */const{ z, ZodError }=require('zod')describe('zod',function(){it('cannot deal with circular data structures',function(){constAnObjectSchema=z.object({someLiteralProperty: z.literal(1)})constcicrularObject={aProperty: 'a property',anotherProperty: 137,anObjectProperty: {anObjectPropertyProperty: 'an object property property'},anArrayProperty: [{anArrayObjectPropertyProperty: 'an object property property'}]}cicrularObject.anObjectProperty.cicrularObject=cicrularObjectcicrularObject.anArrayProperty.push(cicrularObject.anObjectProperty)constviolatingObject={someLiteralProperty: cicrularObject}const{ success, error }=AnObjectSchema.safeParse(violatingObject)success.should.be.false()error.should.be.an.instanceof(ZodError)error.message.should.be.a.String()})})
This test fails with
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)
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:
The text was updated successfully, but these errors were encountered: