Skip to content

additionalProperties conflicts with required property #654

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

Closed
mfrlin opened this issue Feb 10, 2020 · 2 comments
Closed

additionalProperties conflicts with required property #654

mfrlin opened this issue Feb 10, 2020 · 2 comments

Comments

@mfrlin
Copy link

mfrlin commented Feb 10, 2020

Here is an example:

import jsonschema


all_of = [
    {"if": {"properties": {"enum_type": {"const": "TESTA"}}},
    "then": {
        "properties": {"enum_data": {"required": ["TESTA_FIELD1", "TESTA_FIELD2"], "additionalProperties": False}}
    }},
    {"if": {"properties": {"enum_type": {"const": "TESTB"}}},
    "then": {
        "properties": {"enum_data": {"required": ["TESTB_FIELD1", "TESTB_FIELD2"], "additionalProperties": False}}
    }},
]

top_level = {
    "type": "object",
    "properties": {
        "enum_type": {
            "type": "string",
             "enum": ["TESTA", "TESTB"],
        },
    },
    "required": ["enum_type", "enum_data"],
    "allOf": all_of,
}

data = {
    "enum_type": "TESTA",
    "enum_data": {"TESTA_FIELD1": "value"}
}

data2 = {
    "enum_type": "TESTA",
    "enum_data": {"TESTA_FIELD1": "value", "TESTA_FIELD2": "value"}
}

data3 = {
    "enum_type": "TESTA",
    "enum_data": {"TESTA_FIELD1": "value", "TESTA_FIELD2": "value", "UNWANTED_FIELD": "value"}
}


for d in [data, data2, data3]:
    try:
        jsonschema.validate(instance=d, schema=top_level)
    except jsonschema.ValidationError as ve:
        key = '__root__.' + '.'.join(ve.path)
        msg = f"Key <{key}>: {ve.message}"
        print(msg)

Output:

Key <__root__.enum_data>: 'TESTA_FIELD2' is a required property
Key <__root__.enum_data>: Additional properties are not allowed ('TESTA_FIELD1', 'TESTA_FIELD2' were unexpected)
Key <__root__.enum_data>: Additional properties are not allowed ('TESTA_FIELD1', 'UNWANTED_FIELD', 'TESTA_FIELD2' were unexpected)

First validation is expected because TESTA_FIELD2 is required.
Second validation is wrong. It should pass without errors.
Third validation should fail but with only UNWANTED_FIELD as unexpected.

@Julian
Copy link
Member

Julian commented Feb 10, 2020

Hi. This is correct behavior, this is how additionalProperties works.

You need to define all properties in properties, required doesn't count.

@Julian Julian closed this as completed Feb 10, 2020
@mfrlin
Copy link
Author

mfrlin commented Feb 10, 2020

Thank you.

What you said made me think how can I define properties and it was actually pretty easy:

{
    "if": {"properties": {"enum_type": {"const": "TESTA"}}},
    "then": {
        "properties": {
            "enum_data": {
                "required": ["TESTA_FIELD1", "TESTA_FIELD2"], 
                "additionalProperties": False, 
                "properties": {"TESTA_FIELD1": {}, "TESTA_FIELD2": {}}
            }
        }
    }
},

Julian added a commit that referenced this issue Apr 25, 2023
6afa9b38d Merge pull request #664 from santhosh-tekuri/empty-tokens
e4bceb1ad Bump the python-jsonschema version used for the sanity check.
8025fc0d5 Merge pull request #128 from iainbeeston/foundations-of-json-schema-paper
cf7677078 Make all root $ids absolute URIs
07fd389a3 Added test cases from Foundations of JSON Schema research paper
1008edcee ref: test empty tokens in json-pointer
9beb3cfba Merge pull request #627 from json-schema-org/ether/output-readme-fixes
f2b0490ba minor edit to trigger gh action
c305ce54f Merge pull request #669 from hauner/typo
5e2845c1e Merge pull request #668 from hauner/if-without-then-else-creates-annotations
2f1df2293 typo
c1fae0022 test unevaluated* can see annotations from if without then/else
987a4c8fc Merge pull request #666 from json-schema-org/gregsdennis/file-refs
90b2a58ce fix *nix uris
68d18c6ac rename tests to fix sanity check
e9166bcbe fix indentation
1d1ec749a add file-id ref tests
fb60ed17c Merge pull request #663 from json-schema-org/ether/restore-format-tests
f32cd8b80 Revert "Revert "by default, "format" only annotates, not validates""
47958f82d Merge pull request #654 from santhosh-tekuri/output-escape
5262997e1 Merge pull request #661 from santhosh-tekuri/2019-output
ce2c16573 output-tests: correct 2019 output-schema.json
c9d943856 output: ensure ~ and / are escaped in json-pointer
f6b2324bf minor spelling and markdown formatting fixes; `valid` has also been removed from the tests

git-subtree-dir: json
git-subtree-split: 6afa9b38d84d45550ec703123eb4e8ec67a8ae75
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants