예제 #1
0
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_()
예제 #2
0
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)
예제 #3
0
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),
                )
예제 #4
0
 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)
예제 #5
0
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)
예제 #6
0
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),
                )
예제 #7
0
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'])
예제 #8
0
def test_bare_instantiation():
    error_dict = ErrorDict()
    assert 'a' not in error_dict
    assert isinstance(error_dict['a'], ErrorList)
예제 #9
0
def test_instantiation_with_initial_value():
    value = {'a': 3}
    error_dict = ErrorDict(value)
    assert 'a' in error_dict
    assert 3 in error_dict['a']