z-schema
z-schema copied to clipboard
Schemas might get modified in place and lead to a `Maximum call stack size exceeded` error
Hey, i wasn't able to come with a test case for this, but i thought that a general warning to devs might be helpful as well. I am using version 3.4.3
of Z-schema, so this could have been fixed in more recent releases.
Running validate
i was getting a Maximum call stack size exceeded
error. I found out that it was risen by a deep cloning function, because the schema i was passing to validate
had a circular reference.
The point is that the schema did not have any circular reference at the beginning, the circular reference was added by Z-schema somehow. Before using it in validate
, i am passing the schema to setRemoteReference
, because it is referred by another one.
I fixed my issue making sure to always pass a copy of my schemas to Z-schema, so that they cannot be mutated in place
Can you submit a test-case for the issue?
I think that it would be hard for me to extract a test case, since i think to be using Z-schema normally so i wouldn't know how to reduce the case. However, luckily the project having this problem is open source.
On this branch https://github.com/danse/data-models/tree/test-case-for-z-schema-160 i reproduced the error, just npm install
and npm test
should show you the error. The commit at HEAD^
uses immutable schemas and the error does not appear there.
This schema https://github.com/danse/data-models/blob/test-case-for-z-schema-160/schemas/Lock.json is referenced here https://github.com/danse/data-models/blob/test-case-for-z-schema-160/schemas/Callback.json, and when i add the reference, validation fails for the Lock schema, while before it succeeded. The lock schema is added as a reference here https://github.com/danse/data-models/blob/test-case-for-z-schema-160/index.js#L32, and i believe that it is modified in place there and turned into a cyclical structure which cannot be deeply cloned
I've seen this as well and the way I addressed it was I pass a cloned schema to z-schema to avoid the original being modified. This might help until there is a fix.