diff options
author | Harald Nezbeda <hn@nezhar.com> | 2021-06-15 09:19:56 +0200 |
---|---|---|
committer | Harald Nezbeda <hn@nezhar.com> | 2021-07-19 18:14:31 +0200 |
commit | 491168de3b21ea30531406f0f8ddd1415b6f0cba (patch) | |
tree | 3d9cf9906ded91894a0d48dace8225a0ff04bcdf | |
parent | e87769871ac4d3cc05be9da72fd203b56e7bfdba (diff) | |
download | jsonschema-491168de3b21ea30531406f0f8ddd1415b6f0cba.tar.gz |
Julian/jsonschema#782: Add dependentRequired and dependentSchemas validation
-rw-r--r-- | jsonschema/_validators.py | 38 | ||||
-rw-r--r-- | jsonschema/validators.py | 3 |
2 files changed, 40 insertions, 1 deletions
diff --git a/jsonschema/_validators.py b/jsonschema/_validators.py index 0f7b6fb..0e973f3 100644 --- a/jsonschema/_validators.py +++ b/jsonschema/_validators.py @@ -234,6 +234,10 @@ def maxLength(validator, mL, instance, schema): def dependencies(validator, dependencies, instance, schema): + """ + The dependencies keyword has been deprecated since draft 2019-09 and has been split into dependentRequired + and dependentSchemas. + """ if not validator.is_type(instance, "object"): return @@ -253,6 +257,40 @@ def dependencies(validator, dependencies, instance, schema): yield error +def dependentRequired(validator, dependentRequired, instance, schema): + """ + Split from dependencies + """ + if not validator.is_type(instance, "object"): + return + + for property, dependency in dependentRequired.items(): + if property not in instance: + continue + + for each in dependency: + if each not in instance: + message = "%r is a dependency of %r" + yield ValidationError(message % (each, property)) + + +def dependentSchemas(validator, dependentSchemas, instance, schema): + """ + Split from dependencies + """ + if not validator.is_type(instance, "object"): + return + + for property, dependency in dependentSchemas.items(): + if property not in instance: + continue + + for error in validator.descend( + instance, dependency, schema_path=property, + ): + yield error + + def enum(validator, enums, instance, schema): if instance == 0 or instance == 1: unbooled = unbool(instance) diff --git a/jsonschema/validators.py b/jsonschema/validators.py index c1b0753..8c0edd4 100644 --- a/jsonschema/validators.py +++ b/jsonschema/validators.py @@ -438,7 +438,8 @@ Draft202012Validator = create( u"anyOf": _validators.anyOf, u"const": _validators.const, u"contains": _validators.contains, - u"dependencies": _validators.dependencies, + u"dependentRequired": _validators.dependentRequired, + u"dependentSchemas": _validators.dependentSchemas, u"enum": _validators.enum, u"exclusiveMaximum": _validators.exclusiveMaximum, u"exclusiveMinimum": _validators.exclusiveMinimum, |