def is_valid(self, adict, schema, messages=None, draft=4): def trace_error_value(error): if len(error.path) != 0: return (error.path[-1], error.message) return ('keyError', error.message) if draft == 4: self.__errors = dict( trace_error_value(e) for e in sorted(Draft4Validator(schema).iter_errors(adict), key=exceptions.by_relevance())) if draft == 6: self.__errors = dict( trace_error_value(e) for e in sorted(Draft6Validator(schema).iter_errors(adict), key=exceptions.by_relevance())) if draft == 7: self.__errors = dict( trace_error_value(e) for e in sorted(Draft7Validator(schema).iter_errors(adict), key=exceptions.by_relevance())) if len(self.__errors) > 0 and messages: self.__errors = self.remap_error_message(self.__errors, messages) self.__data = adict if len(self.__errors) == 0 else [] return len(self.__errors) == 0
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.by_relevance()) self.assertIs(match, shallow) match = max([deep, shallow], key=exceptions.by_relevance()) self.assertIs(match, shallow)
def test_global_errors_are_even_better_matches(self): shallow = exceptions.ValidationError("Oh no!", path=[]) deep = exceptions.ValidationError("Oh yes!", path=["foo"]) errors = sorted([shallow, deep], key=exceptions.by_relevance()) self.assertEqual( [list(error.path) for error in errors], [["foo"], []], ) errors = sorted([deep, shallow], key=exceptions.by_relevance()) self.assertEqual( [list(error.path) for error in errors], [["foo"], []], )
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)
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)
#!/usr/bin/env python # -*- coding: utf-8 -*- from jsonschema import Draft4Validator from jsonschema.exceptions import by_relevance, relevance schema = { "properties": { "name": {"type": "string", "minLength": 3}, "phones": { "type": "object", "properties": { "home": {"type": "string"} } } } } instance = {"name": "r", "phones": {"home": [123]}} v = Draft4Validator(schema) errors = list(v.iter_errors(instance)) print [e.validator for e in errors] print [e.path[-1] for e in sorted(errors, key=relevance)] print [e.validator for e in sorted(errors, key=by_relevance(weak=set(["type"]), strong=set(["minLength"])))]