Python jsonschema.exceptions.ValidationError() Examples
The following are 30
code examples of jsonschema.exceptions.ValidationError().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
jsonschema.exceptions
, or try the search function
.
Example #1
Source File: test_schema_validation.py From drydock with Apache License 2.0 | 7 votes |
def _test_validate(self, schema, expect_failure, input_files, input): """validates input yaml against schema. :param schema: schema yaml file :param expect_failure: should the validation pass or fail. :param input_files: pytest fixture used to access the test input files :param input: test input yaml doc filename""" schema_dir = pkg_resources.resource_filename('drydock_provisioner', 'schemas') schema_filename = os.path.join(schema_dir, schema) schema_file = open(schema_filename, 'r') schema = yaml.safe_load(schema_file) input_file = input_files.join(input) instance_file = open(str(input_file), 'r') instance = yaml.safe_load(instance_file) if expect_failure: with pytest.raises(ValidationError): jsonschema.validate(instance['spec'], schema['data']) else: jsonschema.validate(instance['spec'], schema['data'])
Example #2
Source File: test_exceptions.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def test_str_works_with_instances_having_overriden_eq_operator(self): """ Check for https://github.com/Julian/jsonschema/issues/164 which rendered exceptions unusable when a `ValidationError` involved instances with an `__eq__` method that returned truthy values. """ instance = mock.MagicMock() error = exceptions.ValidationError( "a message", validator="foo", instance=instance, validator_value="some", schema="schema", ) str(error) self.assertFalse(instance.__eq__.called)
Example #3
Source File: _validators.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def dependencies(validator, dependencies, instance, schema): if not validator.is_type(instance, "object"): return for property, dependency in iteritems(dependencies): if property not in instance: continue if validator.is_type(dependency, "object"): for error in validator.descend( instance, dependency, schema_path=property, ): yield error else: dependencies = _utils.ensure_list(dependency) for dependency in dependencies: if dependency not in instance: yield ValidationError( "%r is a dependency of %r" % (dependency, property) )
Example #4
Source File: _validators.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def additionalItems(validator, aI, instance, schema): if ( not validator.is_type(instance, "array") or validator.is_type(schema.get("items", {}), "object") ): return len_items = len(schema.get("items", [])) if validator.is_type(aI, "object"): for index, item in enumerate(instance[len_items:], start=len_items): for error in validator.descend(item, aI, path=index): yield error elif not aI and len(instance) > len(schema.get("items", [])): error = "Additional items are not allowed (%s %s unexpected)" yield ValidationError( error % _utils.extras_msg(instance[len(schema.get("items", [])):]) )
Example #5
Source File: _validators.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def type_draft3(validator, types, instance, schema): types = _utils.ensure_list(types) all_errors = [] for index, type in enumerate(types): if type == "any": return if validator.is_type(type, "object"): errors = list(validator.descend(instance, type, schema_path=index)) if not errors: return all_errors.extend(errors) else: if validator.is_type(instance, type): return else: yield ValidationError( _utils.types_msg(instance, types), context=all_errors, )
Example #6
Source File: _validators.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def properties_draft3(validator, properties, instance, schema): if not validator.is_type(instance, "object"): return for property, subschema in iteritems(properties): if property in instance: for error in validator.descend( instance[property], subschema, path=property, schema_path=property, ): yield error elif subschema.get("required", False): error = ValidationError("%r is a required property" % property) error._set( validator="required", validator_value=subschema["required"], instance=instance, schema=schema, ) error.path.appendleft(property) error.schema_path.extend([property, "required"]) yield error
Example #7
Source File: _validators.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def oneOf_draft4(validator, oneOf, instance, schema): subschemas = enumerate(oneOf) all_errors = [] for index, subschema in subschemas: errs = list(validator.descend(instance, subschema, schema_path=index)) if not errs: first_valid = subschema break all_errors.extend(errs) else: yield ValidationError( "%r is not valid under any of the given schemas" % (instance,), context=all_errors, ) more_valid = [s for i, s in subschemas if validator.is_valid(instance, s)] if more_valid: more_valid.append(first_valid) reprs = ", ".join(repr(schema) for schema in more_valid) yield ValidationError( "%r is valid under each of %s" % (instance, reprs) )
Example #8
Source File: _validators.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def properties_draft3(validator, properties, instance, schema): if not validator.is_type(instance, "object"): return for property, subschema in iteritems(properties): if property in instance: for error in validator.descend( instance[property], subschema, path=property, schema_path=property, ): yield error elif subschema.get("required", False): error = ValidationError("%r is a required property" % property) error._set( validator="required", validator_value=subschema["required"], instance=instance, schema=schema, ) error.path.appendleft(property) error.schema_path.extend([property, "required"]) yield error
Example #9
Source File: _validators.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def oneOf_draft4(validator, oneOf, instance, schema): subschemas = enumerate(oneOf) all_errors = [] for index, subschema in subschemas: errs = list(validator.descend(instance, subschema, schema_path=index)) if not errs: first_valid = subschema break all_errors.extend(errs) else: yield ValidationError( "%r is not valid under any of the given schemas" % (instance,), context=all_errors, ) more_valid = [s for i, s in subschemas if validator.is_valid(instance, s)] if more_valid: more_valid.append(first_valid) reprs = ", ".join(repr(schema) for schema in more_valid) yield ValidationError( "%r is valid under each of %s" % (instance, reprs) )
Example #10
Source File: test_exceptions.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def test_str_works_with_instances_having_overriden_eq_operator(self): """ Check for https://github.com/Julian/jsonschema/issues/164 which rendered exceptions unusable when a `ValidationError` involved instances with an `__eq__` method that returned truthy values. """ instance = mock.MagicMock() error = exceptions.ValidationError( "a message", validator="foo", instance=instance, validator_value="some", schema="schema", ) str(error) self.assertFalse(instance.__eq__.called)
Example #11
Source File: test_exceptions.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def test_unset_error(self): error = exceptions.ValidationError("message") self.assertEqual(str(error), "message") kwargs = { "validator": "type", "validator_value": "string", "instance": 5, "schema": {"type": "string"}, } # Just the message should show if any of the attributes are unset for attr in kwargs: k = dict(kwargs) del k[attr] error = exceptions.ValidationError("message", **k) self.assertEqual(str(error), "message")
Example #12
Source File: _validators.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def dependencies(validator, dependencies, instance, schema): if not validator.is_type(instance, "object"): return for property, dependency in iteritems(dependencies): if property not in instance: continue if validator.is_type(dependency, "object"): for error in validator.descend( instance, dependency, schema_path=property, ): yield error else: dependencies = _utils.ensure_list(dependency) for dependency in dependencies: if dependency not in instance: yield ValidationError( "%r is a dependency of %r" % (dependency, property) )
Example #13
Source File: test_exceptions.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def test_unset_error(self): error = exceptions.ValidationError("message") self.assertEqual(str(error), "message") kwargs = { "validator": "type", "validator_value": "string", "instance": 5, "schema": {"type": "string"}, } # Just the message should show if any of the attributes are unset for attr in kwargs: k = dict(kwargs) del k[attr] error = exceptions.ValidationError("message", **k) self.assertEqual(str(error), "message")
Example #14
Source File: _validators.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def type_draft3(validator, types, instance, schema): types = _utils.ensure_list(types) all_errors = [] for index, type in enumerate(types): if type == "any": return if validator.is_type(type, "object"): errors = list(validator.descend(instance, type, schema_path=index)) if not errors: return all_errors.extend(errors) else: if validator.is_type(instance, type): return else: yield ValidationError( _utils.types_msg(instance, types), context=all_errors, )
Example #15
Source File: test_exceptions.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def test_strong_validators_are_higher_priority(self): weak = exceptions.ValidationError("Oh no!", path=[], validator="a") normal = exceptions.ValidationError("Oh yes!", path=[], validator="b") strong = exceptions.ValidationError("Oh fine!", path=[], validator="c") best_match = exceptions.by_relevance(weak="a", strong="c") match = max([weak, normal, strong], key=best_match) self.assertIs(match, strong) match = max([strong, normal, weak], key=best_match) self.assertIs(match, strong)
Example #16
Source File: test_exceptions.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def test_weak_validators_are_lower_priority(self): weak = exceptions.ValidationError("Oh no!", path=[], validator="a") normal = exceptions.ValidationError("Oh yes!", path=[], validator="b") best_match = exceptions.by_relevance(weak="a") match = max([weak, normal], key=best_match) self.assertIs(match, normal) match = max([normal, weak], key=best_match) self.assertIs(match, normal)
Example #17
Source File: test_exceptions.py From core with MIT License | 5 votes |
def test_children_have_their_errors_dicts_built(self): e1, e2 = ( exceptions.ValidationError("1", validator="foo", path=["bar", 0]), exceptions.ValidationError("2", validator="quux", path=["bar", 0]), ) tree = exceptions.ErrorTree([e1, e2]) self.assertEqual(tree["bar"][0].errors, {"foo" : e1, "quux" : e2})
Example #18
Source File: test_exceptions.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def test_it_contains_an_item_if_the_item_had_an_error(self): errors = [exceptions.ValidationError("a message", path=["bar"])] tree = exceptions.ErrorTree(errors) self.assertIn("bar", tree)
Example #19
Source File: test_exceptions.py From core with MIT License | 5 votes |
def test_it_contains_an_item_if_the_item_had_an_error(self): errors = [exceptions.ValidationError("a message", path=["bar"])] tree = exceptions.ErrorTree(errors) self.assertIn("bar", tree)
Example #20
Source File: test_exceptions.py From core with MIT License | 5 votes |
def test_it_creates_a_child_tree_for_each_nested_path(self): errors = [ exceptions.ValidationError("a bar message", path=["bar"]), exceptions.ValidationError("a bar -> 0 message", path=["bar", 0]), ] tree = exceptions.ErrorTree(errors) self.assertIn(0, tree["bar"]) self.assertNotIn(1, tree["bar"])
Example #21
Source File: test_exceptions.py From core with MIT License | 5 votes |
def test_validators_that_failed_appear_in_errors_dict(self): error = exceptions.ValidationError("a message", validator="foo") tree = exceptions.ErrorTree([error]) self.assertEqual(tree.errors, {"foo" : error})
Example #22
Source File: test_exceptions.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def test_regression_multiple_errors_with_instance(self): e1, e2 = ( exceptions.ValidationError( "1", validator="foo", path=["bar", "bar2"], instance="i1"), exceptions.ValidationError( "2", validator="quux", path=["foobar", 2], instance="i2"), ) # Will raise an exception if the bug is still there. exceptions.ErrorTree([e1, e2])
Example #23
Source File: test_exceptions.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def test_short_paths_are_better_matches(self): shallow = exceptions.ValidationError("Oh no!", path=["baz"]) deep = exceptions.ValidationError("Oh yes!", path=["foo", "bar"]) match = max([shallow, deep], key=exceptions.relevance) self.assertIs(match, shallow) match = max([deep, shallow], key=exceptions.relevance) self.assertIs(match, shallow)
Example #24
Source File: _validators.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def not_draft4(validator, not_schema, instance, schema): if validator.is_valid(instance, not_schema): yield ValidationError( "%r is not allowed for %r" % (not_schema, instance) )
Example #25
Source File: _validators.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def anyOf_draft4(validator, anyOf, instance, schema): all_errors = [] for index, subschema in enumerate(anyOf): errs = list(validator.descend(instance, subschema, schema_path=index)) if not errs: break all_errors.extend(errs) else: yield ValidationError( "%r is not valid under any of the given schemas" % (instance,), context=all_errors, )
Example #26
Source File: _validators.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def maxProperties_draft4(validator, mP, instance, schema): if not validator.is_type(instance, "object"): return if validator.is_type(instance, "object") and len(instance) > mP: yield ValidationError("%r has too many properties" % (instance,))
Example #27
Source File: _validators.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def required_draft4(validator, required, instance, schema): if not validator.is_type(instance, "object"): return for property in required: if property not in instance: yield ValidationError("%r is a required property" % property)
Example #28
Source File: _validators.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def type_draft4(validator, types, instance, schema): types = _utils.ensure_list(types) if not any(validator.is_type(instance, type) for type in types): yield ValidationError(_utils.types_msg(instance, types))
Example #29
Source File: _validators.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def disallow_draft3(validator, disallow, instance, schema): for disallowed in _utils.ensure_list(disallow): if validator.is_valid(instance, {"type": [disallowed]}): yield ValidationError( "%r is disallowed for %r" % (disallowed, instance) )
Example #30
Source File: _validators.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def maxLength(validator, mL, instance, schema): if validator.is_type(instance, "string") and len(instance) > mL: yield ValidationError("%r is too long" % (instance,))