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

spurious error "must NOT have additional properties" for required field #235

Open
ZeeD opened this issue Jul 12, 2023 · 3 comments
Open

Comments

@ZeeD
Copy link

ZeeD commented Jul 12, 2023

I am encountering an unexpected behavior from ajv-cli.

$ npx ajv -s sch.json -d dat.json
dat.json invalid
[
  {
    instancePath: '/obj',
    schemaPath: '#/properties/obj/additionalProperties',
    keyword: 'additionalProperties',
    params: { additionalProperty: 'r' },
    message: 'must NOT have additional properties'
  }
]
$

with the following sch.json

{
    "$id": "sch.json",
    "$schema": "http://json-schema.org/schema",
    "definitions": {
        "Obj": {
            "type": "object",
            "properties": {
                "r": {
                    "type": "number"
                },
                "o": {
                    "type": "string"
                }
            },
            "required": [
                "r"
            ],
            "additionalProperties": false
        }
    },
    "type": "object",
    "properties": {
        "obj": {
            "type": "object",
            "$ref": "#/definitions/Obj",
            "additionalProperties": false
        }
    }
}

and the following dat.json

{
    "$id": "dat.json",
    "$schema": "./sch.json",
    "obj": {
        "r": 123
    }
}

but the field r is required, and must be set.

@shoops
Copy link

shoops commented Oct 16, 2023

Your schema should be:

{
    "$id": "sch.json",
    "$schema": "http://json-schema.org/schema",
    "definitions": {
        "Obj": {
            "type": "object",
            "properties": {
                "r": {
                    "type": "number"
                },
                "o": {
                    "type": "string"
                }
            },
            "required": [
                "r"
            ],
            "additionalProperties": false
        }
    },
    "type": "object",
    "properties": {
        "obj": {
            "$ref": "#/definitions/Obj"
        }
    }
}

@ZeeD
Copy link
Author

ZeeD commented Oct 17, 2023

Hi @shoops
From what I can see comparing the original schema from yours, you suggest to drop type and additionalProperties from obj's properties.

immagine

I'm not sure to understand the reason, however: according to the 2020-12 draft

The "$ref" keyword is an applicator that is used to reference a statically identified schema. Its results are the results of the referenced schema. Note that this definition of how the results are determined means that other keywords can appear alongside of "$ref" in the same schema object.

I may have misunderstood the paragraph, but to me this means that

  1. the two fields are allowed
  2. they are "merged" with the definition in the referenced object
    2.1 in this specific case they are not necessary, but shouldn't change the behavior of the validator

@shoops
Copy link

shoops commented Oct 17, 2023

Yes, that is correct but the problem is that the "additinalProperties": false is evaluated without any properties being defined. I have seen this behavior with all schema validators I tried. You can use "additionalProperties": "[or]" which would allow only properties o and r.

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

No branches or pull requests

2 participants