def any_validator(obj, validators, **kwargs): """ Attempt multiple validators on an object. - If any pass, then all validation passes. - Otherwise, raise all of the errors. """ if not len(validators) > 1: raise ValueError( "any_validator requires at least 2 validator. Only got " "{0}".format(len(validators))) errors = ErrorDict() for key, validator in validators.items(): try: validator(obj, **kwargs) except ValidationError as err: errors[key] = err.detail else: break else: if len(errors) == 1: # Special case for a single error. Just raise it as if it was the # only validator run. error = errors.values()[0] raise ValidationError(error) else: # Raise all of the errors with the key namespaces. errors.raise_()
def apply_validator_to_object(obj, validator, **kwargs): with ErrorDict() as errors: for key, value in obj.items(): try: validator(value, **kwargs) except ValidationError as err: errors.add_error(key, err.detail)
def validate_deferred_references(schema, context, **kwargs): try: deferred_references = context['deferred_references'] except: raise KeyError("`deferred_references` not found in context") with ErrorDict() as errors: for reference in deferred_references: parts = urlparse.urlparse(reference) if any((parts.scheme, parts.netloc, parts.params, parts.query)): errors.add_error( reference, MESSAGES['reference']['unsupported'].format(reference), ) continue if parts.path: from flex.core import load_source if parts.path.startswith('/'): schema = load_source(parts.path) elif 'base_path' in kwargs: schema = load_source( os.path.join(kwargs['base_path'], parts.path)) try: jsonpointer.resolve_pointer(schema, parts.fragment) except jsonpointer.JsonPointerException: errors.add_error( reference, MESSAGES['reference']['undefined'].format(reference), )
def validate_object(self, obj, **kwargs): with ErrorDict() as errors: for key, validator in self.items(): try: validator(obj, **kwargs) except ValidationError as err: errors.add_error(key, err.detail)
def validate_schema_definitions(definitions, **kwargs): with ErrorDict() as errors: for name, schema in definitions.items(): try: schema_validator(schema, **kwargs) except ValidationError as err: errors.add_error(name, err.detail)
def validate_deferred_references(schema, context, **kwargs): try: deferred_references = context['deferred_references'] except: raise KeyError("`deferred_references` not found in context") with ErrorDict() as errors: for reference in deferred_references: parts = urlparse.urlparse(reference) if any((parts.scheme, parts.netloc, parts.path, parts.params, parts.query)): errors.add_error( reference, MESSAGES['reference']['unsupported'].format(reference), ) continue try: jsonpointer.resolve_pointer(schema, parts.fragment) except jsonpointer.JsonPointerException: errors.add_error( reference, MESSAGES['reference']['undefined'].format(reference), )
def validate_required(value, required_fields, **kwargs): with ErrorDict() as errors: for key in required_fields: if key not in value: errors.add_error(key, MESSAGES['required']['required'])
def test_bare_instantiation(): error_dict = ErrorDict() assert 'a' not in error_dict assert isinstance(error_dict['a'], ErrorList)
def test_instantiation_with_initial_value(): value = {'a': 3} error_dict = ErrorDict(value) assert 'a' in error_dict assert 3 in error_dict['a']