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

Error validating with nested oneOf/anyOf/allOf #1515

Closed
mikethecalamity opened this issue Mar 26, 2021 · 2 comments
Closed

Error validating with nested oneOf/anyOf/allOf #1515

mikethecalamity opened this issue Mar 26, 2021 · 2 comments

Comments

@mikethecalamity
Copy link

mikethecalamity commented Mar 26, 2021

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

Ajv options object

const Ajv = require("ajv/dist/2019").default;
const ajvFormats = require('ajv-formats').default;
const ajv = new Ajv({
    allErrors: true,
    validateFormats: true
});
ajvFormats(ajv);

JSON Schema

{
    "$schema": "https://json-schema.org/draft/2019-09/schema",
    "$id": "https://test.com/test.json",
    "type": "object",
    "anyOf": [
        {
            "required": [ "flag" ],
            "properties": {
                "type": {
                    "const": "FOO"
                },
                "flag": {
                    "type": "boolean"
                }
            }
        },
        {
            "properties": {
                "type": {
                    "enum": [ "BAR", "BAZ" ]
                }
            },
            "anyOf": [
                {
                    "required": [ "name" ],
                    "properties": {
                        "name": {
                            "type": "string"
                        }
                    }
                },
                {
                    "required": [ "value" ],
                    "properties": {
                        "value": {
                            "type": "number"
                        }
                    }
                }
            ]
        }
    ]
}

Sample data

{
    "type": "BAR",
    "value": 500
}

Your code

https://runkit.com/mweber03/605d4480645b6e001a445577

var validate = ajv.compile(schema);
console.log(validate(data));

Validation result, data AFTER validation, error messages

TypeError: Cannot convert undefined or null to object
    at Function.assign ()
    at validate539 (eval at compileSchema (node_modules\ajv\dist\c ompile\index.js:88:30), :3:4381)

What results did you expect?
A successful validation (It worked with draft04 and v5.2.0)

Are you going to resolve the issue?

@epoberezkin
Copy link
Member

Confirmed - it is caused by handling of evaluated properties (to support unevaluatedProperties).

https://runkit.com/esp/605d8a27a850060013d0af69

If you do not use unevaluatedProperties keyword (or any other new draft-2019-09 features), you should use default import - it would have a smaller generated code.

Another workaround is to use ajv.opts.unevaluated = false (it would not work if you use unevaluatedProperties/Items keywords).

@epoberezkin
Copy link
Member

in 7.2.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants