Skip to content

jsonschema fails to validate patternProperties with additionalProperties=False #821

@michael-robbins

Description

@michael-robbins

Having this weird issue, not sure if I'm just misunderstanding the usage, or if it's a bug?

It looks like setting additionalProperties to False doesn't understand patternProperties and validation fails?

>>> jsonschema.validate(
...   {"a": "a", "b": "a"},
...   schema={
...     "$schema": "http://json-schema.org/draft-07/schema#",
...     "required": ["a"],
...     "properties": {
...       "a": {"type": "string"},
...       "patternProperties": {"b": {"type": "string"}}
...     },
...     "additionalProperties": False
...   }
... )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "~/venv/lib/python3.9/site-packages/jsonschema/validators.py", line 776, in validate
    raise error
jsonschema.exceptions.ValidationError: Additional properties are not allowed ('b' was unexpected)

Failed validating 'additionalProperties' in schema:
    {'$schema': 'http://json-schema.org/draft-07/schema#',
     'additionalProperties': False,
     'properties': {'a': {'type': 'string'},
                    'patternProperties': {'b': {'type': 'string'}}},
     'required': ['a', 'b']}

On instance:
    {'a': 'a', 'b': 'a'}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions