GithubHelp home page GithubHelp logo

Comments (5)

LazerJesus avatar LazerJesus commented on September 22, 2024

Reproduction:

import Ajv from "ajv";

const ajv = new Ajv({ allErrors: false, verbose: true, $data: true });

const annotationschema = {
    type: "object",
    properties: {
        pos: {type: "string"},
        lemma: {type: "string"},
        verbform: {type: "string", enum: ["fin", "inf"]},
        tense: {type: "string"},
        gender: {type: "string"}
    },
    required: ["pos", "lemma", "verbform"],
    allOf: [
        {
            if: {properties: {verbform: {const: "fin"}}},
            then: {required: ["tense"], properties: {gender: {not: {}}}}
        }
    ]
};

const annotation = {
    pos: "verb",
    lemma: "something"
    // verbform: "inf" // toggle this to see the error magically disappear, even tho the if condition is just as false.
};

const validate = ajv.compile(annotationschema);
const valid = validate(annotation);
console.log(valid);
console.log(validate.errors);

from ajv.

jasoniangreen avatar jasoniangreen commented on September 22, 2024

Hi @LazerJesus, thanks for brining this to our attention. After playing around with your example for a bit it does indeed look like a bug to me. I can even pass an empty object and it still complains about missing tense. I will look into it.

from ajv.

epoberezkin avatar epoberezkin commented on September 22, 2024

schema in "if" says that when verbForm is present it should be "fin" but it doesn't require that it's present.

So in the absense of verbForm the schema in "if" passes validation, and schema in "then" is applied.

meaning that the if: { properties: { verbform: { const: "fin" } } } is somehow true for { pos: 'verb' } ?!

That's exactly how JSON schema spec is supposed to work 🤷‍♂️

If you make this property required, it will be "false", but it's not implied.

from ajv.

jasoniangreen avatar jasoniangreen commented on September 22, 2024

Ah yes of course, I was thrown by the fact that the outer schema has "verbform" in the required array, but the if schema is treated separately so will need it's own required.

        {
            if: {
                properties: {verbform: {const: "fin"}},
                required: ["verbform"],
            },
            then: {required: ["tense"]},
        }

from ajv.

LazerJesus avatar LazerJesus commented on September 22, 2024

Indeed that solves it. thank you for the information.

i must say tho, that its very counterintuitive and seems to conflict with logic.
since the statementif: { properties: { verbform: { const: "fin" } } } is true, the logical branch to execute is then.
adding the qualifier required doesnt, for me, change the proposition.
the verbform either is, or is not fin.

that being said, ill just add the qualifier and thats that ✌️ all the best

from ajv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.