Liquid Studio 2021
JSON Schema Editor / Graphical Notation Overview / Not
In This Topic
    Not
    In This Topic
    Draft 04 Draft 06 Draft 07 Draft 2019-09

    The 'Not' node contains a single schema, if this schema validates against the instance value then the validation fails.

    Put another way, it's a negative operator, it says this instance value MUST NOT validate against the schema contained within the 'not' node.

    Although the 'not' operator can be very useful, it's unintuitive and easy to miss-interpreted, so should be used only when absolutely necessary.

    Let's say we want to have a schema the has values that are Index1, Index2, ... index3484 etc.

    Our simple schema may look something like this

    Json Schema
    Copy Code
    {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "type": "object",
        "title": "My schema",
        "additionalProperties": false,
        "patternProperties": {
            "Index[0-9]*": {
                "type": "string"
            }
        }
    }
    

    That's fine, but let's say you really don't want a value of Index0 to be allowed (which it currently is).

    One way to solve this is to explicitly exclude it, we can do this with a Not node.

    Json Schema
    Copy Code
    {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "type": "object",
        "title": "My schema",
        "additionalProperties": false,
        "patternProperties": {
            "Index[0-9]*": {
                "type": "string"
            }
        },
        "not": {
            "type": "object",
            "required": [
                "Index0"
            ]
        }
    }
    

    These things are sometimes easier to read in reverse order, so looking at the properties collection in the 'Not' node, it's saying that it will validate any instance object as long as it contains a member called 'Index0'.

    Moving down, the Not node negates this schema, so is satisfied by an instance document as long as it DOES NOT contain a member called 'Index0'.

    The properties collection at the root level must also validate. It validates as long as all the instance object member names conform to the regex 'Index[0-9]*'

    Put it altogether and you get, the instance object must contain members with names that conform to the regex 'Index[0-9]*', but are not named 'Index0'.

     

    This is an artificial example, and in this case the problem is much more effectively solved with a better regular expression.