Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default values - RangeError: Maximum call stack size exceeded #802

Closed
fogine opened this issue Jun 8, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@fogine
Copy link

commented Jun 8, 2018

What version of Ajv are you using? Does the issue happen if you use the latest version?

6.5.0

Ajv options object

{useDefaults: false}

JSON Schema

function Default() {}
Default.prototype.toJSON = function toJSON() {
    return {};
};

const def = new Default;

def.prop = {
    Default: def // it fails because of this circular enumerable property
};

const schema = {
    type: 'object',
    properties: {
        prop: {
            type: 'object',
            default: def
        }
    }
};

Your code

ajv.addSchema(schema, 'test');

Validation result, data AFTER validation, error messages

RangeError: Maximum call stack size exceeded
    at _traverse (/path/to/project/node_modules/json-schema-traverse/index.js:58:5)
    at _traverse (/path/to/project/node_modules/json-schema-traverse/index.js:72:9)
    at _traverse (/path/to/project/node_modules/json-schema-traverse/index.js:72:9)
    at _traverse (/path/to/project/node_modules/json-schema-traverse/index.js:72:9)
    at _traverse (/path/to/project/node_modules/json-schema-traverse/index.js:72:9)
    at _traverse (/path/to/project/node_modules/json-schema-traverse/index.js:72:9)
    at _traverse (/path/to/project/node_modules/json-schema-traverse/index.js:72:9)
    at _traverse (/path/to/project/node_modules/json-schema-traverse/index.js:72:9)
    at _traverse (/path/to/project/node_modules/json-schema-traverse/index.js:72:9)
    at _traverse (/path/to/project/node_modules/json-schema-traverse/index.js:72:9)


What results did you expect?
When useDefaults=false I'd expect that ajv is gonna ignore default keyword and not traverse its enumerable properties.
Although it should not fail even with useDefaults=true when the default value object implements toJSON method.

Are you going to resolve the issue?
possible bugfix provided here:
epoberezkin/json-schema-traverse#5

In ajv library, used here:
https://github.com/epoberezkin/ajv/blob/master/lib/compile/resolve.js#L239

@fogine

This comment has been minimized.

Copy link
Author

commented Jun 8, 2018

note: I see now that default property value is serialized because ajv option serialize is not set to false...

@epoberezkin

This comment has been minimized.

Copy link
Owner

commented Jun 9, 2018

@fogine ajv indeed doesn't support schemas with recursive data structures - they need to be serialisable (unless you disable it, as you wrote, but they would still be used as keys - so unless you use WeakMap as a cache, nothing will work). I will release json-schema-traverse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.