diff options
author | Harald Nezbeda <hn@nezhar.com> | 2021-07-14 14:56:31 +0200 |
---|---|---|
committer | Harald Nezbeda <hn@nezhar.com> | 2021-07-21 15:37:26 +0200 |
commit | 841a0a2ad5fac6dafed85ea395e7aa9176084549 (patch) | |
tree | 41879563757dbf4421ba7c140cfb89db52378891 | |
parent | 1caee547d3fc7a7d59d1e35ee098dc3a2c3667a0 (diff) | |
download | jsonschema-841a0a2ad5fac6dafed85ea395e7aa9176084549.tar.gz |
Julian/jsonschema#782: Code clenaup, fixes validation messages
-rw-r--r-- | jsonschema/_legacy_validators.py | 8 | ||||
-rw-r--r-- | jsonschema/_utils.py | 125 | ||||
-rw-r--r-- | jsonschema/_validators.py | 71 | ||||
-rw-r--r-- | jsonschema/tests/test_format.py | 6 | ||||
-rw-r--r-- | jsonschema/validators.py | 10 | ||||
-rw-r--r-- | setup.cfg | 2 |
6 files changed, 93 insertions, 129 deletions
diff --git a/jsonschema/_legacy_validators.py b/jsonschema/_legacy_validators.py index 54b5fad..b707889 100644 --- a/jsonschema/_legacy_validators.py +++ b/jsonschema/_legacy_validators.py @@ -28,10 +28,10 @@ def dependencies_draft3(validator, dependencies, instance, schema): def dependencies_draft4_draft6_draft7( - validator, - dependencies, - instance, - schema, + validator, + dependencies, + instance, + schema, ): """ Support for the ``dependencies`` validator from pre-draft 2019-09. diff --git a/jsonschema/_utils.py b/jsonschema/_utils.py index e963e39..1b81a09 100644 --- a/jsonschema/_utils.py +++ b/jsonschema/_utils.py @@ -255,55 +255,50 @@ def find_evaluated_item_indexes_by_schema(validator, instance, schema): Get all indexes of items that get evaluated under the current schema Covers all keywords related to unevaluatedItems: items, prefixItems, if, - then, else, 'contains', 'unevaluatedItems', 'allOf', 'oneOf', 'anyOf' + then, else, contains, unevaluatedItems, allOf, oneOf, anyOf """ - if not validator.is_type(schema, "object"): + if validator.is_type(schema, "boolean"): return [] evaluated_indexes = [] - if 'items' in schema: + if "items" in schema: return list(range(0, len(instance))) - if '$ref' in schema: - resolve = getattr(validator.resolver, "resolve", None) - if resolve: - scope, resolved = validator.resolver.resolve(schema['$ref']) - validator.resolver.push_scope(scope) + if "$ref" in schema: + scope, resolved = validator.resolver.resolve(schema["$ref"]) + validator.resolver.push_scope(scope) - try: - evaluated_indexes += find_evaluated_item_indexes_by_schema( - validator, instance, resolved) - finally: - validator.resolver.pop_scope() - - if 'prefixItems' in schema: - if validator.is_valid( - instance, {'prefixItems': schema['prefixItems']} - ): - evaluated_indexes += list(range(0, len(schema['prefixItems']))) - - if 'if' in schema: - if validator.is_valid(instance, schema['if']): + try: + evaluated_indexes += find_evaluated_item_indexes_by_schema( + validator, instance, resolved) + finally: + validator.resolver.pop_scope() + + if "prefixItems" in schema: + evaluated_indexes += list(range(0, len(schema["prefixItems"]))) + + if "if" in schema: + if validator.is_valid(instance, schema["if"]): evaluated_indexes += find_evaluated_item_indexes_by_schema( - validator, instance, schema['if'] + validator, instance, schema["if"] ) - if 'then' in schema: + if "then" in schema: evaluated_indexes += find_evaluated_item_indexes_by_schema( - validator, instance, schema['then'] + validator, instance, schema["then"] ) else: - if 'else' in schema: + if "else" in schema: evaluated_indexes += find_evaluated_item_indexes_by_schema( - validator, instance, schema['else'] + validator, instance, schema["else"] ) - for keyword in ['contains', 'unevaluatedItems']: + for keyword in ["contains", "unevaluatedItems"]: if keyword in schema: for k, v in enumerate(instance): if validator.is_valid(v, schema[keyword]): evaluated_indexes.append(k) - for keyword in ['allOf', 'oneOf', 'anyOf']: + for keyword in ["allOf", "oneOf", "anyOf"]: if keyword in schema: for subschema in schema[keyword]: errs = list(validator.descend(instance, subschema)) @@ -320,28 +315,26 @@ def find_evaluated_property_keys_by_schema(validator, instance, schema): Get all keys of items that get evaluated under the current schema Covers all keywords related to unevaluatedProperties: properties, - 'additionalProperties', 'unevaluatedProperties', patternProperties, - dependentSchemas, 'allOf', 'oneOf', 'anyOf', if, then, else + additionalProperties, unevaluatedProperties, patternProperties, + dependentSchemas, allOf, oneOf, anyOf, if, then, else """ - if not validator.is_type(schema, "object"): + if validator.is_type(schema, "boolean"): return [] evaluated_keys = [] - if '$ref' in schema: - resolve = getattr(validator.resolver, "resolve", None) - if resolve: - scope, resolved = validator.resolver.resolve(schema['$ref']) - validator.resolver.push_scope(scope) + if "$ref" in schema: + scope, resolved = validator.resolver.resolve(schema["$ref"]) + validator.resolver.push_scope(scope) - try: - evaluated_keys += find_evaluated_property_keys_by_schema( - validator, instance, resolved - ) - finally: - validator.resolver.pop_scope() + try: + evaluated_keys += find_evaluated_property_keys_by_schema( + validator, instance, resolved + ) + finally: + validator.resolver.pop_scope() for keyword in [ - 'properties', 'additionalProperties', 'unevaluatedProperties' + "properties", "additionalProperties", "unevaluatedProperties" ]: if keyword in schema: if validator.is_type(schema[keyword], "boolean"): @@ -356,27 +349,23 @@ def find_evaluated_property_keys_by_schema(validator, instance, schema): ): evaluated_keys.append(property) - if 'patternProperties' in schema: + if "patternProperties" in schema: for property, value in instance.items(): - for pattern, subschema in schema['patternProperties'].items(): - if re.search(pattern, property): - if validator.is_valid( - {property: value}, schema['patternProperties'] - ): - evaluated_keys.append(property) - - if 'dependentSchemas' in schema: - for property, subschema in schema['dependentSchemas'].items(): + for pattern, subschema in schema["patternProperties"].items(): + if re.search(pattern, property) and validator.is_valid( + {property: value}, schema["patternProperties"] + ): + evaluated_keys.append(property) + + if "dependentSchemas" in schema: + for property, subschema in schema["dependentSchemas"].items(): if property not in instance: continue + evaluated_keys += find_evaluated_property_keys_by_schema( + validator, instance, subschema + ) - errs = list(validator.descend(instance, subschema)) - if not errs: - evaluated_keys += find_evaluated_property_keys_by_schema( - validator, instance, subschema - ) - - for keyword in ['allOf', 'oneOf', 'anyOf']: + for keyword in ["allOf", "oneOf", "anyOf"]: if keyword in schema: for subschema in schema[keyword]: errs = list(validator.descend(instance, subschema)) @@ -385,19 +374,19 @@ def find_evaluated_property_keys_by_schema(validator, instance, schema): validator, instance, subschema ) - if 'if' in schema: - if validator.is_valid(instance, schema['if']): + if "if" in schema: + if validator.is_valid(instance, schema["if"]): evaluated_keys += find_evaluated_property_keys_by_schema( - validator, instance, schema['if'] + validator, instance, schema["if"] ) - if 'then' in schema: + if "then" in schema: evaluated_keys += find_evaluated_property_keys_by_schema( - validator, instance, schema['then'] + validator, instance, schema["then"] ) else: - if 'else' in schema: + if "else" in schema: evaluated_keys += find_evaluated_property_keys_by_schema( - validator, instance, schema['else'] + validator, instance, schema["else"] ) return evaluated_keys diff --git a/jsonschema/_validators.py b/jsonschema/_validators.py index 7acd178..b6a2f9c 100644 --- a/jsonschema/_validators.py +++ b/jsonschema/_validators.py @@ -129,10 +129,7 @@ def contains(validator, contains, instance, schema): if min_contains == 0: return - matches = len(list( - filter(lambda x: x, [validator.is_valid(element, contains) for - element in instance])) - ) + matches = sum(1 for each in instance if validator.is_valid(each, contains)) # default contains behavior if not matches: @@ -144,9 +141,9 @@ def contains(validator, contains, instance, schema): if min_contains and max_contains is None: if matches < min_contains: yield ValidationError( - "Invalid number or matches of %r under the given schema, " - "expected min %d, got %d" % ( - instance, min_contains, matches + "Too few matches under the given schema. " + "Expected %d but there were only %d." % ( + min_contains, matches ) ) return @@ -154,9 +151,9 @@ def contains(validator, contains, instance, schema): if min_contains is None and max_contains: if matches > max_contains: yield ValidationError( - "Invalid number or matches of %r under the given schema, " - "expected max %d, got %d" % ( - instance, max_contains, matches + "Too many matches under the given schema. " + "Expected %d but there were only %d." % ( + max_contains, matches ) ) return @@ -164,9 +161,9 @@ def contains(validator, contains, instance, schema): if min_contains and max_contains: if matches < min_contains or matches > max_contains: yield ValidationError( - "Invalid number or matches of %r under the given schema, " - "expected min %d and max %d, got %d" % ( - instance, min_contains, max_contains, matches + "Invalid number or matches under the given schema, " + "expected between %d and %d, got %d" % ( + min_contains, max_contains, matches ) ) return @@ -289,9 +286,6 @@ def maxLength(validator, mL, instance, schema): def dependentRequired(validator, dependentRequired, instance, schema): - """ - Split from dependencies - """ if not validator.is_type(instance, "object"): return @@ -306,12 +300,6 @@ def dependentRequired(validator, dependentRequired, instance, schema): 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 @@ -355,8 +343,8 @@ def dynamicRef(validator, dynamicRef, instance, schema): for url in scope_stack: lookup_url = urljoin(url, dynamicRef) with validator.resolver.resolving(lookup_url) as lookup_schema: - if "$dynamicAnchor" in lookup_schema \ - and fragment == lookup_schema["$dynamicAnchor"]: + if ("$dynamicAnchor" in lookup_schema + and fragment == lookup_schema["$dynamicAnchor"]): subschema = lookup_schema for error in validator.descend(instance, subschema): yield error @@ -375,10 +363,10 @@ def defs(validator, defs, instance, schema): if '$defs' in instance: for definition, subschema in instance['$defs'].items(): for error in validator.descend( - subschema, - schema, - path=definition, - schema_path=definition, + subschema, + schema, + path=definition, + schema_path=definition, ): yield error @@ -491,9 +479,6 @@ def if_(validator, if_schema, instance, schema): def unevaluatedItems(validator, unevaluatedItems, instance, schema): - if not validator.is_type(instance, "array"): - return - evaluated_item_indexes = find_evaluated_item_indexes_by_schema( validator, instance, schema ) @@ -501,7 +486,7 @@ def unevaluatedItems(validator, unevaluatedItems, instance, schema): for k, v in enumerate(instance): if k not in evaluated_item_indexes: for error in validator.descend( - v, unevaluatedItems, schema_path="unevaluatedItems" + v, unevaluatedItems, schema_path="unevaluatedItems" ): unevaluated_items.append(v) @@ -511,9 +496,6 @@ def unevaluatedItems(validator, unevaluatedItems, instance, schema): def unevaluatedProperties(validator, unevaluatedProperties, instance, schema): - if not validator.is_type(instance, "object"): - return - evaluated_property_keys = find_evaluated_property_keys_by_schema( validator, instance, schema ) @@ -521,10 +503,10 @@ def unevaluatedProperties(validator, unevaluatedProperties, instance, schema): for property, subschema in instance.items(): if property not in evaluated_property_keys: for error in validator.descend( - instance[property], - unevaluatedProperties, - path=property, - schema_path=property, + instance[property], + unevaluatedProperties, + path=property, + schema_path=property, ): unevaluated_property_keys.append(property) @@ -537,9 +519,8 @@ def prefixItems(validator, prefixItems, instance, schema): if not validator.is_type(instance, "array"): return - for k, v in enumerate(instance): - if k < len(prefixItems): - for error in validator.descend( - v, prefixItems[k], schema_path="prefixItems" - ): - yield error + for k, v in enumerate(instance[:min(len(prefixItems), len(instance))]): + for error in validator.descend( + v, prefixItems[k], schema_path="prefixItems" + ): + yield error diff --git a/jsonschema/tests/test_format.py b/jsonschema/tests/test_format.py index f4cec9d..06f841c 100644 --- a/jsonschema/tests/test_format.py +++ b/jsonschema/tests/test_format.py @@ -79,9 +79,9 @@ class TestFormatChecker(TestCase): def test_repr(self): checker = FormatChecker(formats=()) - checker.checks("foo")(lambda thing: True) - checker.checks("bar")(lambda thing: True) - checker.checks("baz")(lambda thing: True) + checker.checks("foo")(lambda thing: True) # pragma: no cover + checker.checks("bar")(lambda thing: True) # pragma: no cover + checker.checks("baz")(lambda thing: True) # pragma: no cover self.assertEqual( repr(checker), "<FormatChecker checkers=['bar', 'baz', 'foo']>", diff --git a/jsonschema/validators.py b/jsonschema/validators.py index 3da5091..8df65cc 100644 --- a/jsonschema/validators.py +++ b/jsonschema/validators.py @@ -52,13 +52,11 @@ def validates(version): def _validates(cls): validators[version] = cls meta_schema_id = cls.ID_OF(cls.META_SCHEMA) - if meta_schema_id: - meta_schemas[meta_schema_id] = cls + meta_schemas[meta_schema_id] = cls for vocabulary in cls.VOCABULARY_SCHEMAS: vocabulary_id = cls.ID_OF(vocabulary) - if vocabulary_id: - vocabulary_schemas[vocabulary_id] = vocabulary + vocabulary_schemas[vocabulary_id] = vocabulary return cls return _validates @@ -675,7 +673,6 @@ class RefResolver(object): def _finditem(self, schema, key): results = [] - if isinstance(schema, dict): if key in schema: results.append(schema) @@ -699,9 +696,6 @@ class RefResolver(object): if target_uri.rstrip("/") == uri.rstrip("/"): if fragment: subschema = self.resolve_fragment(subschema, fragment) - - if self.cache_remote: - self.store[url] = subschema return subschema def resolve(self, ref): @@ -55,7 +55,7 @@ console_scripts = jsonschema = jsonschema.cli:main [options.package_data] -jsonschema = schemas/*.json, schemas/draft2020-12/*.json +jsonschema = schemas/*.json, schemas/*/*.json [bdist_wheel] universal = 1 |