diff options
author | Julian Berman <Julian@GrayVines.com> | 2021-08-25 18:34:29 +0100 |
---|---|---|
committer | Julian Berman <Julian@GrayVines.com> | 2021-08-25 18:34:29 +0100 |
commit | 996437f0693adf66ac6d24df7770057fc0fd9b15 (patch) | |
tree | d07f9d80d67799ffa85b62f7f0e07398ef94417e /jsonschema/_validators.py | |
parent | 452169710f20a38372bdd686f79680df1215d188 (diff) | |
download | jsonschema-996437f0693adf66ac6d24df7770057fc0fd9b15.tar.gz |
Add Validator.evolve, deprecating passing _schema to methods.
A Validator should be thought of as encapsulating validation with a
single fixed schema.
Previously, iter_errors and is_valid allowed passing a second argument,
which was a different schema to use for one method call.
This was mostly for convenience, since the second argument is often
used during sub-validation whilst say, recursing.
The correct way to do so now is to say:
validator.evolve(schema=new_schema).iter_errors(...)
validator.evolve(schema=new_schema).is_valid(...)
instead, which is essentially equally convenient.
Closes: #522
Diffstat (limited to 'jsonschema/_validators.py')
-rw-r--r-- | jsonschema/_validators.py | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/jsonschema/_validators.py b/jsonschema/_validators.py index dec6b21..e0845ea 100644 --- a/jsonschema/_validators.py +++ b/jsonschema/_validators.py @@ -112,7 +112,7 @@ def contains(validator, contains, instance, schema): max_contains = schema.get("maxContains", len(instance)) for each in instance: - if validator.is_valid(each, contains): + if validator.evolve(schema=contains).is_valid(each): matches += 1 if matches > max_contains: yield ValidationError( @@ -388,7 +388,10 @@ def oneOf(validator, oneOf, instance, schema): context=all_errors, ) - more_valid = [s for i, s in subschemas if validator.is_valid(instance, s)] + more_valid = [ + each for _, each in subschemas + if validator.evolve(schema=each).is_valid(instance) + ] if more_valid: more_valid.append(first_valid) reprs = ", ".join(repr(schema) for schema in more_valid) @@ -396,13 +399,13 @@ def oneOf(validator, oneOf, instance, schema): def not_(validator, not_schema, instance, schema): - if validator.is_valid(instance, not_schema): + if validator.evolve(schema=not_schema).is_valid(instance): message = f"{instance!r} should not be valid under {not_schema!r}" yield ValidationError(message) def if_(validator, if_schema, instance, schema): - if validator.is_valid(instance, if_schema): + if validator.evolve(schema=if_schema).is_valid(instance): if "then" in schema: then = schema["then"] yield from validator.descend(instance, then, schema_path="then") |