playlyfe / themis

A blazing fast JSON Schema v4 validator!
Other
60 stars 7 forks source link

Error with $ref & nested definitions #18

Open NathanRSmith opened 9 years ago

NathanRSmith commented 9 years ago

Hi all,

So came across an interesting error from Themis today. Here is a gist (https://gist.github.com/NathanRSmith/e09d671f0a72b43b781b) which illustrates the problem. It may seem contrived, but it reflects another schema that I was originally working on for declaratively building a tree-like structure.

Here is the error stack trace for the case that fails.

RangeError: Maximum call stack size exceeded
    at String.replace (native)
    at buildError (/Users/nathansmith/dev/tmp/node_modules/themis/src/themis.js:627:51)
    at ValidationGenerators.type (/Users/nathansmith/dev/tmp/node_modules/themis/src/themis.js:708:7)
    at generateSchema (/Users/nathansmith/dev/tmp/node_modules/themis/src/themis.js:1749:26)
    at Object.0#/definitions/stuff/anyStuff (/Users/nathansmith/dev/tmp/node_modules/themis/src/themis.js:1694:31)
    at generateSchema (/Users/nathansmith/dev/tmp/node_modules/themis/src/themis.js:1794:64)
    at generateSchema (/Users/nathansmith/dev/tmp/node_modules/themis/src/themis.js:1872:24)
    at generateSchema (/Users/nathansmith/dev/tmp/node_modules/themis/src/themis.js:1878:26)
    at Object.0#/definitions/stuff/stuff1 (/Users/nathansmith/dev/tmp/node_modules/themis/src/themis.js:1694:31)
    at generateSchema (/Users/nathansmith/dev/tmp/node_modules/themis/src/themis.js:1794:64)

I am not intimately familiar with the themis core, but it appears to be coming from where it builds error messages to throw if data fails validation. This error seems to be building a message that has the path of the circular "$ref" and there is a recursion error. But what is interesting is that it only does this when I have my definitions grouped and not when things are a bit flatter, despite there still being cycles...

Anyone have any insight as to why this is happening? I think I can work around it in my real schema for now by keeping things flat, but other validators (tv4, is-my-json-valid) are able to validate without issue.

$ node --version
v0.12.0
$ npm ls
/Users/nathansmith/dev/tmp
└─┬ themis@1.1.5
  └─┬ uglify-js@2.4.15
    ├── async@0.2.10
    ├─┬ optimist@0.3.7
    │ └── wordwrap@0.0.3
    ├─┬ source-map@0.1.34
    │ └── amdefine@0.1.1
    └── uglify-to-browserify@1.0.2
pyrossh commented 9 years ago

I don't think themis is tested on node v0.12.0? Maybe it would work on node v0.10.33.

NathanRSmith commented 9 years ago

Just tried, same error.