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

JTD fails validation of discriminated union #1971

Closed
ioder opened this issue Apr 29, 2022 · 1 comment
Closed

JTD fails validation of discriminated union #1971

ioder opened this issue Apr 29, 2022 · 1 comment
Milestone

Comments

@ioder
Copy link

ioder commented Apr 29, 2022

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

Ajv options object

{}

JSON Schema

{
    "discriminator": "dicriminatorProperty",
    "mapping": {
        "manual": {
            "properties": {
                "first": { "type": "uint16" },
                "second": { "type": "uint16" },
                "third": { "type": "uint16" },
                "fourth": { "type": "uint16" },
                "fifth": { "type": "uint16" },
                "sixth": { "type": "uint16" },
                "additionalOne": { "type": "uint16" },
                "additionalTwo": { "type": "uint16" }
            }
        },
        "auto": {
            "properties": {
                "first": { "type": "uint16" },
                "second": { "type": "uint16" },
                "third": { "type": "uint16" },
                "fourth": { "type": "uint16" },
                "fifth": { "type": "uint16" },
                "sixth": { "type": "uint16" },
                "additionalThree": { "type": "uint16" }
            }
        }
    }
}

Sample data

 {
      "dicriminatorProperty": "manual",
      "first": 17,
      "second": 17,
      "third": 17,
      "fourth": 17,
      "fifth": 17,
      "sixth": 17,
      "additionalOne": 17,
      "additionalTwo": 17
}

Your code

const commonProperties = {
    first: { type: "uint16" },
    second: { type: "uint16" },
    third: { type: "uint16" },
    fourth: { type: "uint16" },
    fifth: { type: "uint16" },
    sixth: { type: "uint16" }
};

const schema = {
    discriminator: "dicriminatorProperty",
    mapping: {
        manual: {
            properties: {
                ...commonProperties,
                additionalOne: { type: "uint16" },
                additionalTwo: { type: "uint16" }
            }
        },
        auto: {
            properties: {
                ...commonProperties,
                additionalThree: { type: "uint16" }
            }
        }
    }
};

const Ajv = require("ajv/dist/jtd");
const ajv = new Ajv();
const validate = ajv.compile(schema);

const data1 = {
    dicriminatorProperty: "manual",
    first: 17,
    second: 17,
    third: 17,
    fourth: 17,
    fifth: 17,
    sixth: 17,
    additionalOne: 17,
    additionalTwo: 17
};

const data2 = {
    dicriminatorProperty: "auto",
    first: 17,
    second: 17,
    third: 17,
    fourth: 17,
    fifth: 17,
    sixth: 17,
    additionalThree: 17
};

// ---------------------------------------------------
// This validation passes
// ---------------------------------------------------
console.log("Valid data1? ", validate(data1));

// ---------------------------------------------------
// This validation fails
// ---------------------------------------------------
console.log("Valid data2? ", validate(data2));

Validation result, data AFTER validation, error messages

{
    instancePath: "/dicriminatorProperty",
    schemaPath: "/mapping/manual",
    keyword: "properties",
    params: {
         error: "additional",
         additionalProperty: "dicriminatorProperty"
    },
    message: "must NOT have additional properties"
}

What results did you expect?
The number of properties in the object seems to be affecting the validation result. This should not happen and discriminator should not be recognised as an additional property.

Are you going to resolve the issue?
Resolution of this issue is non actionable by me

@ioder ioder changed the title JDT fails validation of discriminated union JTD fails validation of discriminated union Apr 29, 2022
@epoberezkin epoberezkin added this to the 8.12 milestone Nov 13, 2022
@epoberezkin
Copy link
Member

not reproducible on smaller example: https://runkit.com/esp/637163192eb1b90008da2666

seems to need exactly that number of properties, for me data1 fails that has more properties, data2 passes.

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