Beispiel #1
0
def test_allow_unknown_with_oneof_rules(validator):
    # https://github.com/pyeve/cerberus/issues/251
    schema = {
        'test': {
            'oneof': [
                {
                    'type': 'dict',
                    'allow_unknown': True,
                    'schema': {'known': {'type': 'string'}}
                },
                {
                    'type': 'dict',
                    'schema': {'known': {'type': 'string'}}
                },
            ]
        }
    }
    # check regression and that allow unknown does not cause any different
    # than expected behaviour for one-of.
    document = {'test': {'known': 's'}}
    validator(document, schema)
    _errors = validator._errors
    assert len(_errors) == 1
    assert_has_error(_errors, 'test', ('test', 'oneof'),
                     errors.ONEOF, schema['test']['oneof'])
    assert len(_errors[0].child_errors) == 0
    # check that allow_unknown is actually applied
    document = {'test': {'known': 's', 'unknown': 'asd'}}
    assert_success(document, validator=validator)
def test_noneof():
    # prop1 can not be:
    # - greater than 10
    # - greater than 0
    # - equal to -5, 5, or 15

    schema = {"prop1": {"type": "integer", "noneof": [{"min": 0}, {"min": 10}, {"allowed": [-5, 5, 15]}]}}

    # document is valid
    doc = {"prop1": -1}
    assert_success(doc, schema)

    # document is not valid
    # prop1 is equal to -5
    doc = {"prop1": -5}
    assert_fail(doc, schema)

    # document is not valid
    # prop1 greater than 0
    doc = {"prop1": 1}
    assert_fail(doc, schema)

    # document is not valid
    doc = {"prop1": 5}
    assert_fail(doc, schema)

    # document is not valid
    doc = {"prop1": 11}
    assert_fail(doc, schema)

    # document is not valid
    # and equal to 15
    doc = {"prop1": 15}
    assert_fail(doc, schema)
def test_dependencies_on_boolean_field_with_value_in_list():
    # https://github.com/nicolaiarocci/cerberus/issues/138
    schema = {"deleted": {"type": "boolean"}, "text": {"dependencies": {"deleted": [False]}}}

    assert_success({"text": "foo", "deleted": False}, schema)
    assert_fail({"text": "foo", "deleted": True}, schema)
    assert_fail({"text": "foo"}, schema)
def test_nested_readonly_field_with_default_value():
    schema = {
        "some_field": {
            "type": "dict",
            "schema": {
                "created": {"type": "string", "readonly": True, "default": "today"},
                "modified": {"type": "string", "readonly": True, "default_setter": lambda d: d["created"]},
            },
        }
    }
    assert_success({"some_field": {}}, schema)
    expected_errors = [
        (
            ("some_field", "created"),
            ("some_field", "schema", "created", "readonly"),
            errors.READONLY_FIELD,
            schema["some_field"]["schema"]["created"]["readonly"],
        ),
        (
            ("some_field", "modified"),
            ("some_field", "schema", "modified", "readonly"),
            errors.READONLY_FIELD,
            schema["some_field"]["schema"]["modified"]["readonly"],
        ),
    ]
    assert_fail({"some_field": {"created": "tomorrow", "modified": "now"}}, schema, errors=expected_errors)
    assert_fail({"some_field": {"created": "today", "modified": "today"}}, schema, errors=expected_errors)
def test_a_dict_with_valueschema(validator):
    assert_success({"a_dict_with_valueschema": {"an integer": 99, "another integer": 100}})

    error = (
        "a_dict_with_valueschema",
        ("a_dict_with_valueschema", "valueschema"),
        errors.VALUESCHEMA,
        {"type": "integer"},
    )
    child_errors = [
        (
            ("a_dict_with_valueschema", "a string"),
            ("a_dict_with_valueschema", "valueschema", "type"),
            errors.BAD_TYPE,
            "integer",
        )
    ]

    assert_fail(
        {"a_dict_with_valueschema": {"a string": "99"}}, validator=validator, error=error, child_errors=child_errors
    )

    assert "valueschema" in validator.schema_error_tree["a_dict_with_valueschema"]
    v = validator.schema_error_tree
    assert len(v["a_dict_with_valueschema"]["valueschema"].descendants) == 1
Beispiel #6
0
def test_dependencies_dict_with_required_field():
    schema = {
        'test_field': {
            'required': True,
            'dependencies': {'foo': 'foo', 'bar': 'bar'}
        },
        'foo': {'type': 'string'},
        'bar': {'type': 'string'}
    }
    # False: all dependencies missing
    assert_fail({'test_field': 'foobar'}, schema)
    # False: one of dependencies missing
    assert_fail({'test_field': 'foobar', 'foo': 'foo'}, schema)
    assert_fail({'test_field': 'foobar', 'bar': 'bar'}, schema)
    # False: dependencies are validated and field is required
    assert_fail({'foo': 'foo', 'bar': 'bar'}, schema)
    # False: All dependencies are optional, but field is still required
    assert_fail({}, schema)
    # False: dependency missing
    assert_fail({'foo': 'bar'}, schema)

    assert_success({'test_field': 'foobar', 'foo': 'foo', 'bar': 'bar'},
                   schema)

    # True: dependencies are validated but field is not required
    schema['test_field']['required'] = False
    assert_success({'foo': 'bar', 'bar': 'foo'}, schema)
Beispiel #7
0
def test_ignore_none_values():
    field = 'test'
    schema = {field: {'type': 'string', 'empty': False, 'required': False}}
    document = {field: None}

    # Test normal behaviour
    validator = Validator(schema, ignore_none_values=False)
    assert_fail(document, validator=validator)
    validator.schema[field]['required'] = True
    validator.schema.validate()
    _errors = assert_fail(document, validator=validator)
    assert_not_has_error(_errors, field, (field, 'required'),
                         errors.REQUIRED_FIELD, True)

    # Test ignore None behaviour
    validator = Validator(schema, ignore_none_values=True)
    validator.schema[field]['required'] = False
    validator.schema.validate()
    assert_success(document, validator=validator)
    validator.schema[field]['required'] = True
    _errors = assert_fail(schema=schema, document=document, validator=validator)
    assert_has_error(_errors, field, (field, 'required'), errors.REQUIRED_FIELD,
                     True)
    assert_not_has_error(_errors, field, (field, 'type'), errors.BAD_TYPE,
                         'string')
Beispiel #8
0
def test_self_root_document():
    """ Make sure self.root_document is always the root document.
    See:
    * https://github.com/pyeve/cerberus/pull/42
    * https://github.com/pyeve/eve/issues/295
    """

    class MyValidator(Validator):
        def _validate_root_doc(self, root_doc, field, value):
            """ {'type': 'boolean'} """
            if ('sub' not in self.root_document or
                    len(self.root_document['sub']) != 2):
                self._error(field, 'self.context is not the root doc!')

    schema = {
        'sub': {
            'type': 'list',
            'root_doc': True,
            'schema': {
                'type': 'dict',
                'schema': {
                    'foo': {
                        'type': 'string',
                        'root_doc': True
                    }
                }
            }
        }
    }
    assert_success({'sub': [{'foo': 'bar'}, {'foo': 'baz'}]},
                   validator=MyValidator(schema))
def test_nested_unknown_keys():
    schema = {"field1": {"type": "dict", "allow_unknown": True, "schema": {"nested1": {"type": "string"}}}}
    document = {"field1": {"nested1": "foo", "arb1": "bar", "arb2": 42}}
    assert_success(document=document, schema=schema)

    schema["field1"]["allow_unknown"] = {"type": "string"}
    assert_fail(document=document, schema=schema)
Beispiel #10
0
def test_nested_readonly_field_with_default_value():
    schema = {
        'some_field': {
            'type': 'dict',
            'schema': {
                'created': {
                    'type': 'string',
                    'readonly': True,
                    'default': 'today'
                },
                'modified': {
                    'type': 'string',
                    'readonly': True,
                    'default_setter': lambda d: d['created']
                }
            }
        }
    }
    assert_success({'some_field': {}}, schema)
    expected_errors = [
        (('some_field', 'created'),
         ('some_field', 'schema', 'created', 'readonly'),
         errors.READONLY_FIELD,
         schema['some_field']['schema']['created']['readonly']),
        (('some_field', 'modified'),
         ('some_field', 'schema', 'modified', 'readonly'),
         errors.READONLY_FIELD,
         schema['some_field']['schema']['modified']['readonly'])]
    assert_fail({'some_field': {'created': 'tomorrow', 'modified': 'now'}},
                schema, errors=expected_errors)
    assert_fail({'some_field': {'created': 'today', 'modified': 'today'}},
                schema, errors=expected_errors)
Beispiel #11
0
def test_unknown_keys_list_of_dicts(validator):
    # test that allow_unknown is honored even for subdicts in lists.
    # https://github.com/pyeve/cerberus/issues/67.
    validator.allow_unknown = True
    document = {'a_list_of_dicts': [{'sku': 'YZ069', 'price': 25,
                                     'extra': True}]}
    assert_success(document, validator=validator)
def test_allow_unknown_with_of_rules():
    # https://github.com/pyeve/cerberus/issues/251
    schema = {
        'test': {
            'oneof': [
                {
                    'type': 'dict',
                    'allow_unknown': True,
                    'schema': {'known': {'type': 'string'}},
                },
                {'type': 'dict', 'schema': {'known': {'type': 'string'}}},
            ]
        }
    }
    # check regression and that allow unknown does not cause any different
    # than expected behaviour for one-of.
    document = {'test': {'known': 's'}}
    assert_fail(
        document,
        schema,
        error=('test', ('test', 'oneof'), errors.ONEOF, schema['test']['oneof']),
    )
    # check that allow_unknown is actually applied
    document = {'test': {'known': 's', 'unknown': 'asd'}}
    assert_success(document, schema)
Beispiel #13
0
def test_dependencies_on_boolean_field_with_value_in_list():
    # https://github.com/pyeve/cerberus/issues/138
    schema = {'deleted': {'type': 'boolean'},
              'text': {'dependencies': {'deleted': [False]}}}

    assert_success({'text': 'foo', 'deleted': False}, schema)
    assert_fail({'text': 'foo', 'deleted': True}, schema)
    assert_fail({'text': 'foo'}, schema)
def test_regex(validator):
    field = "a_regex_email"
    assert_success({field: "*****@*****.**"}, validator=validator)
    assert_fail(
        {field: "invalid"},
        update=True,
        error=(field, (field, "regex"), errors.REGEX_MISMATCH, "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"),
    )
def test_dependencies_list_with_subodcuments_fields():
    schema = {
        "test_field": {"dependencies": ["a_dict.foo", "a_dict.bar"]},
        "a_dict": {"type": "dict", "schema": {"foo": {"type": "string"}, "bar": {"type": "string"}}},
    }
    assert_success({"test_field": "foobar", "a_dict": {"foo": "foo", "bar": "bar"}}, schema)
    assert_fail({"test_field": "foobar", "a_dict": {}}, schema)
    assert_fail({"test_field": "foobar", "a_dict": {"foo": "foo"}}, schema)
Beispiel #16
0
def test_normalization_with_rules_set():
    # https://github.com/pyeve/cerberus/issues/283
    rules_set_registry.add('foo', {'default': 42})
    assert_normalized({}, {'bar': 42}, {'bar': 'foo'})
    rules_set_registry.add('foo', {'default_setter': lambda _: 42})
    assert_normalized({}, {'bar': 42}, {'bar': 'foo'})
    rules_set_registry.add('foo', {'type': 'integer', 'nullable': True})
    assert_success({'bar': None}, {'bar': 'foo'})
Beispiel #17
0
def test_unicode_allowed_py3():
    """ All strings are unicode in Python 3.x. Input doc and schema
    have equal strings and validation yield success."""

    # issue 280
    doc = {'letters': u'♄εℓł☺'}
    schema = {'letters': {'type': 'string', 'allowed': ['♄εℓł☺']}}
    assert_success(doc, schema)
def test_issue_107(validator):
    schema = {"info": {"type": "dict", "schema": {"name": {"type": "string", "required": True}}}}
    document = {"info": {"name": "my name"}}
    assert_success(document, schema, validator=validator)

    v = Validator(schema)
    assert_success(document, schema, v)
    # it once was observed that this behaves other than the previous line
    assert v.validate(document)
Beispiel #19
0
def test_dependencies_list():
    schema = {
        'test_field': {'dependencies': ['foo', 'bar']},
        'foo': {'type': 'string'},
        'bar': {'type': 'string'}
    }
    assert_success({'test_field': 'foobar', 'foo': 'bar', 'bar': 'foo'},
                   schema)
    assert_fail({'test_field': 'foobar', 'foo': 'bar'}, schema)
def test_required_excludes():
    schema = {
        "this_field": {"type": "dict", "excludes": "that_field", "required": True},
        "that_field": {"type": "dict", "excludes": "this_field", "required": True},
    }
    assert_success({"this_field": {}}, schema, update=False)
    assert_success({"that_field": {}}, schema, update=False)
    assert_fail({}, schema)
    assert_fail({"that_field": {}, "this_field": {}}, schema)
Beispiel #21
0
def test_a_dict():
    assert_success({'a_dict': {'address': 'i live here',
                               'city': 'in my own town'}})
    assert_fail({'a_dict': {'address': 8545}}, errors=[
        (('a_dict', 'address'), ('a_dict', 'schema', 'address', 'type'),
         errors.BAD_TYPE, 'string'),
        (('a_dict', 'city'), ('a_dict', 'schema', 'city', 'required'),
         errors.REQUIRED_FIELD, True)
    ])
def test_a_dict():
    assert_success({"a_dict": {"address": "i live here", "city": "in my own town"}})
    assert_fail(
        {"a_dict": {"address": 8545}},
        errors=[
            (("a_dict", "address"), ("a_dict", "schema", "address", "type"), errors.BAD_TYPE, "string"),
            (("a_dict", "city"), ("a_dict", "schema", "city", "required"), errors.REQUIRED_FIELD, True),
        ],
    )
Beispiel #23
0
def test_a_list_of_dicts():
    assert_success(
        {
            'a_list_of_dicts': [
                {'sku': 'AK345', 'price': 100},
                {'sku': 'YZ069', 'price': 25}
            ]
        }
    )
Beispiel #24
0
def test_oneof_schema():
    schema = {'oneof_schema': {'type': 'dict',
                               'oneof_schema':
                                   [{'digits': {'type': 'integer',
                                                'min': 0, 'max': 99}},
                                    {'text': {'type': 'string',
                                              'regex': '^[0-9]{2}$'}}]}}
    assert_success({'oneof_schema': {'digits': 19}}, schema)
    assert_success({'oneof_schema': {'text': '84'}}, schema)
    assert_fail({'oneof_schema': {'digits': 19, 'text': '84'}}, schema)
Beispiel #25
0
def test_root_relative_dependencies():
    # https://github.com/pyeve/cerberus/issues/288
    schema = {'package': {'allow_unknown': True,
                          'schema': {'version': {'dependencies': '^repo'}}},
              'repo': {}}
    assert_fail({'package': {'repo': 'somewhere', 'version': 0}}, schema,
                error=(('package', 'version'),
                       ('package', 'schema', 'version', 'dependencies'),
                       errors.DEPENDENCIES_FIELD, '^repo', ('^repo',)))
    assert_success({'repo': 'somewhere', 'package': {'version': 1}}, schema)
Beispiel #26
0
def test_issue_107(validator):
    schema = {'info': {'type': 'dict',
                       'schema': {'name': {'type': 'string',
                                           'required': True}}}}
    document = {'info': {'name': 'my name'}}
    assert_success(document, schema, validator=validator)

    v = Validator(schema)
    assert_success(document, schema, v)
    # it once was observed that this behaves other than the previous line
    assert v.validate(document)
Beispiel #27
0
def test_required_excludes():
    schema = {'this_field': {'type': 'dict',
                             'excludes': 'that_field',
                             'required': True},
              'that_field': {'type': 'dict',
                             'excludes': 'this_field',
                             'required': True}}
    assert_success({'this_field': {}}, schema, update=False)
    assert_success({'that_field': {}}, schema, update=False)
    assert_fail({}, schema)
    assert_fail({'that_field': {}, 'this_field': {}}, schema)
def test_unknown_keys_retain_custom_rules():
    # test that allow_unknown schema respect custom validation rules.
    # https://github.com/nicolaiarocci/cerberus/issues/#66.
    class CustomValidator(Validator):
        def _validate_type_foo(self, value):
            if value == "foo":
                return True

    validator = CustomValidator({})
    validator.allow_unknown = {"type": "foo"}
    assert_success(document={"fred": "foo", "barney": "foo"}, validator=validator)
def test_issue_265():
    class MyValidator(Validator):
        def _validator_oddity(self, field, value):
            if not value & 1:
                self._error(field, "Must be an odd number")

    v = MyValidator(schema={'amount': {'validator': 'oddity'}})
    assert_success(document={'amount': 1}, validator=v)
    assert_fail(document={'amount': 2}, validator=v,
                error=('amount', (), errors.CUSTOM, None,
                       ('Must be an odd number',)))
Beispiel #30
0
def test_anyof_schema(validator):
    # test that a list of schemas can be specified.

    valid_parts = [{'schema': {'model number': {'type': 'string'},
                               'count': {'type': 'integer'}}},
                   {'schema': {'serial number': {'type': 'string'},
                               'count': {'type': 'integer'}}}]
    valid_item = {'type': ['dict', 'string'], 'anyof': valid_parts}
    schema = {'parts': {'type': 'list', 'schema': valid_item}}
    document = {'parts': [{'model number': 'MX-009', 'count': 100},
                          {'serial number': '898-001'},
                          'misc']}

    # document is valid. each entry in 'parts' matches a type or schema
    assert_success(document, schema, validator=validator)

    document['parts'].append({'product name': "Monitors", 'count': 18})
    # document is invalid. 'product name' does not match any valid schemas
    assert_fail(document, schema, validator=validator)

    document['parts'].pop()
    # document is valid again
    assert_success(document, schema, validator=validator)

    document['parts'].append({'product name': "Monitors", 'count': 18})
    document['parts'].append(10)
    # and invalid. numbers are not allowed.

    exp_child_errors = [
        (('parts', 3), ('parts', 'schema', 'anyof'), errors.ANYOF,
         valid_parts),
        (('parts', 4), ('parts', 'schema', 'type'), errors.BAD_TYPE,
         ['dict', 'string'])
    ]

    _errors = assert_fail(document, schema, validator=validator,
                          error=('parts', ('parts', 'schema'),
                                 errors.SEQUENCE_SCHEMA, valid_item),
                          child_errors=exp_child_errors)
    assert_not_has_error(_errors, ('parts', 4), ('parts', 'schema', 'anyof'),
                         errors.ANYOF, valid_parts)

    v_errors = validator.errors
    assert 'parts' in v_errors
    assert 3 in v_errors['parts'][-1]
    assert 'anyof' in v_errors['parts'][-1][3][-1]
    assert v_errors['parts'][-1][3][-1]['anyof'][0] == "no definitions validate"
    scope = v_errors['parts'][-1][3][-1]['anyof'][-1]
    assert 'anyof definition 0' in scope
    assert 'anyof definition 1' in scope
    assert scope['anyof definition 0'] == ["unknown field"]
    assert scope['anyof definition 1'] == ["unknown field"]
    assert v_errors['parts'][-1][4] == ["must be of ['dict', 'string'] type"]
Beispiel #31
0
def test_nested_unknown_keys():
    schema = {
        'field1': {
            'type': 'dict',
            'allow_unknown': True,
            'schema': {'nested1': {'type': 'string'}}
        }
    }
    document = {
        'field1': {
            'nested1': 'foo',
            'arb1': 'bar',
            'arb2': 42
        }
    }
    assert_success(document=document, schema=schema)

    schema['field1']['allow_unknown'] = {'type': 'string'}
    assert_fail(document=document, schema=schema)
Beispiel #32
0
def test_a_dict_with_valueschema(validator):
    assert_success({'a_dict_with_valueschema':
                   {'an integer': 99, 'another integer': 100}})

    error = (
        'a_dict_with_valueschema', ('a_dict_with_valueschema', 'valueschema'),
        errors.VALUESCHEMA, {'type': 'integer'})
    child_errors = [
        (('a_dict_with_valueschema', 'a string'),
         ('a_dict_with_valueschema', 'valueschema', 'type'),
         errors.BAD_TYPE, 'integer')]

    assert_fail({'a_dict_with_valueschema': {'a string': '99'}},
                validator=validator, error=error, child_errors=child_errors)

    assert 'valueschema' in \
           validator.schema_error_tree['a_dict_with_valueschema']
    v = validator.schema_error_tree
    assert len(v['a_dict_with_valueschema']['valueschema'].descendants) == 1
Beispiel #33
0
def test_allof():
    # prop1 has to be a float between 0 and 10
    schema = {'prop1': {'allof': [
        {'type': 'float'}, {'min': 0}, {'max': 10}]}}
    doc = {'prop1': -1}
    assert_fail(doc, schema)
    doc = {'prop1': 5}
    assert_success(doc, schema)
    doc = {'prop1': 11}
    assert_fail(doc, schema)

    # prop1 has to be a float and an integer
    schema = {'prop1': {'allof': [{'type': 'float'}, {'type': 'integer'}]}}
    doc = {'prop1': 11}
    assert_success(doc, schema)
    doc = {'prop1': 11.5}
    assert_fail(doc, schema)
    doc = {'prop1': '11'}
    assert_fail(doc, schema)
Beispiel #34
0
def test_one_of_two_types(validator):
    field = 'one_or_more_strings'
    assert_success({field: 'foo'})
    assert_success({field: ['foo', 'bar']})
    exp_child_errors = [
        ((field, 1), (field, 'itemsrules', 'type'), errors.TYPE, ('string',))
    ]
    assert_fail(
        {field: ['foo', 23]},
        validator=validator,
        error=(field, (field, 'itemsrules'), errors.ITEMSRULES, {'type': ('string',)}),
        child_errors=exp_child_errors,
    )
    assert_fail(
        {field: 23}, error=((field,), (field, 'type'), errors.TYPE, ('string', 'list'))
    )
    assert validator.errors == {
        field: [{1: ["must be one of these types: ('string',)"]}]
    }
Beispiel #35
0
def test_issue_107(validator):
    # https://github.com/pyeve/cerberus/issues/107
    schema = {
        'info': {
            'type': 'dict',
            'schema': {
                'name': {
                    'type': 'string',
                    'required': True
                }
            },
        }
    }
    document = {'info': {'name': 'my name'}}
    assert_success(document, schema, validator=validator)

    v = Validator(schema)
    assert_success(document, schema, v)
    # it once was observed that this behaves other than the previous line
    assert v.validate(document)
Beispiel #36
0
def test_dependencies_list_with_required_field():
    schema = {
        'test_field': {'required': True, 'dependencies': ['foo', 'bar']},
        'foo': {'type': 'string'},
        'bar': {'type': 'string'}
    }
    # False: all dependencies missing
    assert_fail({'test_field': 'foobar'}, schema)
    # False: one of dependencies missing
    assert_fail({'test_field': 'foobar', 'foo': 'bar'}, schema)
    # False: one of dependencies missing
    assert_fail({'test_field': 'foobar', 'bar': 'foo'}, schema)
    # False: dependencies are validated and field is required
    assert_fail({'foo': 'bar', 'bar': 'foo'}, schema)
    # False: All dependencies are optional but field is still required
    assert_fail({}, schema)
    # True: dependency missing
    assert_fail({'foo': 'bar'}, schema)
    # True: dependencies are validated but field is not required
    schema['test_field']['required'] = False
    assert_success({'foo': 'bar', 'bar': 'foo'}, schema)
Beispiel #37
0
def test_contextual_data_preservation():
    class InheritedValidator(cerberus.Validator):
        def __init__(self, *args, **kwargs):
            if 'working_dir' in kwargs:
                self.working_dir = kwargs['working_dir']
            super(InheritedValidator, self).__init__(*args, **kwargs)

        def _validate_type_test(self, value):
            if self.working_dir:
                return True

    assert 'test' in InheritedValidator.types
    v = InheritedValidator(
        {'test': {
            'type': 'list',
            'schema': {
                'type': 'test'
            }
        }},
        working_dir='/tmp')
    assert_success({'test': ['foo']}, validator=v)
Beispiel #38
0
def test_dependencies_relative_to_document_root():
    # https://github.com/pyeve/cerberus/issues/288
    subschema = {'version': {'dependencies': ('^repo',)}}
    schema = {'package': {'allow_unknown': True, 'schema': subschema}, 'repo': {}}

    assert_success({'repo': 'somewhere', 'package': {'version': 1}}, schema)

    assert_fail(
        {'package': {'repo': 'somewhere', 'version': 0}},
        schema,
        error=('package', ('package', 'schema'), errors.SCHEMA, subschema),
        child_errors=[
            (
                ('package', 'version'),
                ('package', 'schema', 'version', 'dependencies'),
                errors.DEPENDENCIES_FIELD,
                ('^repo',),
                ('^repo',),
            )
        ],
    )
Beispiel #39
0
def test_nullable_field():
    assert_success({'a_nullable_integer': None})
    assert_success({'a_nullable_integer': 3})
    assert_success({'a_nullable_field_without_type': None})
    assert_fail({'a_nullable_integer': "foo"})
    assert_fail({'an_integer': None})
    assert_fail({'a_not_nullable_field_without_type': None})
Beispiel #40
0
def test_contextual_data_preservation():
    class InheritedValidator(cerberus.Validator):
        def __init__(self, *args, **kwargs):
            if 'working_dir' in kwargs:
                self.working_dir = kwargs['working_dir']
            super().__init__(*args, **kwargs)

        def _check_with_test(self, field, value):
            if self.working_dir:
                return True

    assert 'test' in InheritedValidator.checkers
    v = InheritedValidator(
        {'test': {
            'type': 'list',
            'itemsrules': {
                'check_with': 'test'
            }
        }},
        working_dir='/tmp',
    )
    assert_success({'test': ['foo']}, validator=v)
Beispiel #41
0
def test_271_normalising_tuples():
    # https://github.com/pyeve/cerberus/issues/271
    schema = {
        'my_field': {
            'type': 'list',
            'schema': {
                'type': ('string', 'number', 'dict')
            }
        }
    }
    document = {
        'my_field': ('foo', 'bar', 42, 'albert', 'kandinsky', {
            'items': 23
        })
    }
    assert_success(document, schema)

    normalized = Validator(schema).normalized(document)
    assert normalized['my_field'] == ('foo', 'bar', 42, 'albert', 'kandinsky',
                                      {
                                          'items': 23
                                      })
Beispiel #42
0
def test_excludes():
    schema = {'this_field': {'type': 'dict',
                             'excludes': 'that_field'},
              'that_field': {'type': 'dict'}}
    assert_success({'this_field': {}}, schema)
    assert_success({'that_field': {}}, schema)
    assert_success({}, schema)
    assert_fail({'that_field': {}, 'this_field': {}}, schema)
Beispiel #43
0
def test_require_all_and_exclude():
    schema = {
        'foo': {'type': 'string', 'excludes': 'bar'},
        'bar': {'type': 'string', 'excludes': 'foo'},
    }
    validator = Validator(require_all=True)
    assert_fail(
        {},
        schema,
        validator,
        errors=[
            ('foo', '__require_all__', errors.REQUIRED_FIELD, True),
            ('bar', '__require_all__', errors.REQUIRED_FIELD, True),
        ],
    )
    assert_success({'foo': 'value'}, schema, validator)
    assert_success({'bar': 'value'}, schema, validator)
    assert_fail({'foo': 'value', 'bar': 'value'}, schema, validator)
    validator.require_all = False
    assert_success({}, schema, validator)
    assert_success({'foo': 'value'}, schema, validator)
    assert_success({'bar': 'value'}, schema, validator)
    assert_fail({'foo': 'value', 'bar': 'value'}, schema, validator)
Beispiel #44
0
def test_dependencies_dict_with_subodcuments_fields():
    schema = {
        'test_field': {'dependencies': {'a_dict.foo': ['foo', 'bar'],
                                        'a_dict.bar': 'bar'}},
        'a_dict': {
            'type': 'dict',
            'schema': {
                'foo': {'type': 'string'},
                'bar': {'type': 'string'}
            }
        }
    }
    assert_success({'test_field': 'foobar',
                    'a_dict': {'foo': 'foo', 'bar': 'bar'}}, schema)
    assert_success({'test_field': 'foobar',
                    'a_dict': {'foo': 'bar', 'bar': 'bar'}}, schema)
    assert_fail({'test_field': 'foobar', 'a_dict': {}}, schema)
    assert_fail({'test_field': 'foobar',
                 'a_dict': {'foo': 'foo', 'bar': 'foo'}}, schema)
    assert_fail({'test_field': 'foobar', 'a_dict': {'bar': 'foo'}},
                schema)
    assert_fail({'test_field': 'foobar', 'a_dict': {'bar': 'bar'}},
                schema)
Beispiel #45
0
def test_noneof():
    # prop1 can not be:
    # - greater than 10
    # - greater than 0
    # - equal to -5, 5, or 15

    schema = {'prop1': {'type': 'integer', 'noneof': [
        {'min': 0},
        {'min': 10},
        {'allowed': [-5, 5, 15]}]}}

    # document is valid
    doc = {'prop1': -1}
    assert_success(doc, schema)

    # document is not valid
    # prop1 is equal to -5
    doc = {'prop1': -5}
    assert_fail(doc, schema)

    # document is not valid
    # prop1 greater than 0
    doc = {'prop1': 1}
    assert_fail(doc, schema)

    # document is not valid
    doc = {'prop1': 5}
    assert_fail(doc, schema)

    # document is not valid
    doc = {'prop1': 11}
    assert_fail(doc, schema)

    # document is not valid
    # and equal to 15
    doc = {'prop1': 15}
    assert_fail(doc, schema)
Beispiel #46
0
def test_allow_unknown_with_of_rules():
    # https://github.com/pyeve/cerberus/issues/251
    schema = {
        'test': {
            'oneof': [
                {
                    'type': 'dict',
                    'allow_unknown': True,
                    'schema': {
                        'known': {
                            'type': 'string'
                        }
                    },
                },
                {
                    'type': 'dict',
                    'schema': {
                        'known': {
                            'type': 'string'
                        }
                    }
                },
            ]
        }
    }
    # check regression and that allow unknown does not cause any different
    # than expected behaviour for one-of.
    document = {'test': {'known': 's'}}
    assert_fail(
        document,
        schema,
        error=('test', ('test', 'oneof'), errors.ONEOF,
               schema['test']['oneof']),
    )
    # check that allow_unknown is actually applied
    document = {'test': {'known': 's', 'unknown': 'asd'}}
    assert_success(document, schema)
Beispiel #47
0
def test_check_with_rule(validator):
    def check_with_name(field, value, error):
        if not value.islower():
            error(field, 'must be lowercase')

    validator.schema = {
        'name': {
            'check_with': check_with_name
        },
        'age': {
            'type': 'integer'
        },
    }

    assert_fail(
        {
            'name': 'ItsMe',
            'age': 2
        },
        validator=validator,
        error=('name', (), errors.CUSTOM, None, ('must be lowercase', )),
    )
    assert validator.errors == {'name': ['must be lowercase']}
    assert_success({'name': 'itsme', 'age': 2}, validator=validator)
Beispiel #48
0
def test_readonly_field_with_default_value():
    schema = {
        'created': {
            'type': 'string',
            'readonly': True,
            'default': 'today'
        },
        'modified': {
            'type': 'string',
            'readonly': True,
            'default_setter': lambda d: d['created']
        }
    }
    assert_success({}, schema)
    expected_errors = [('created', ('created', 'readonly'),
                        errors.READONLY_FIELD,
                        schema['created']['readonly']),
                       ('modified', ('modified', 'readonly'),
                        errors.READONLY_FIELD,
                        schema['modified']['readonly'])]
    assert_fail({'created': 'tomorrow', 'modified': 'today'},
                schema, errors=expected_errors)
    assert_fail({'created': 'today', 'modified': 'today'},
                schema, errors=expected_errors)
Beispiel #49
0
def test_anyof():
    # prop1 must be either a number between 0 and 10
    schema = {'prop1': {'min': 0, 'max': 10}}
    doc = {'prop1': 5}

    assert_success(doc, schema)

    # prop1 must be either a number between 0 and 10 or 100 and 110
    schema = {'prop1': {'anyof':
                        [{'min': 0, 'max': 10}, {'min': 100, 'max': 110}]}}
    doc = {'prop1': 105}

    assert_success(doc, schema)

    # prop1 must be either a number between 0 and 10 or 100 and 110
    schema = {'prop1': {'anyof':
                        [{'min': 0, 'max': 10}, {'min': 100, 'max': 110}]}}
    doc = {'prop1': 50}

    assert_fail(doc, schema)

    # prop1 must be an integer that is either be
    # greater than or equal to 0, or greater than or equal to 10
    schema = {'prop1': {'type': 'integer',
                        'anyof': [{'min': 0}, {'min': 10}]}}
    assert_success({'prop1': 10}, schema)
    # test that intermediate schemas do not sustain
    assert 'type' not in schema['prop1']['anyof'][0]
    assert 'type' not in schema['prop1']['anyof'][1]
    assert 'allow_unknown' not in schema['prop1']['anyof'][0]
    assert 'allow_unknown' not in schema['prop1']['anyof'][1]
    assert_success({'prop1': 5}, schema)

    exp_child_errors = [
        (('prop1',), ('prop1', 'anyof', 0, 'min'), errors.MIN_VALUE, 0),
        (('prop1',), ('prop1', 'anyof', 1, 'min'), errors.MIN_VALUE, 10)
    ]
    assert_fail({'prop1': -1}, schema,
                error=(('prop1',), ('prop1', 'anyof'), errors.ANYOF,
                       [{'min': 0}, {'min': 10}]),
                child_errors=exp_child_errors)
    doc = {'prop1': 5.5}
    assert_fail(doc, schema)
    doc = {'prop1': '5.5'}
    assert_fail(doc, schema)
Beispiel #50
0
def test_multiples_exclusions():
    schema = {'this_field': {'type': 'dict',
                             'excludes': ['that_field', 'bazo_field']},
              'that_field': {'type': 'dict',
                             'excludes': 'this_field'},
              'bazo_field': {'type': 'dict'}}
    assert_success({'this_field': {}}, schema)
    assert_success({'that_field': {}}, schema)
    assert_fail({'this_field': {}, 'that_field': {}}, schema)
    assert_fail({'this_field': {}, 'bazo_field': {}}, schema)
    assert_fail({'that_field': {}, 'this_field': {}, 'bazo_field': {}}, schema)
    assert_success({'that_field': {}, 'bazo_field': {}}, schema)
Beispiel #51
0
def test_require_all_simple():
    schema = {'foo': {'type': 'string'}}
    validator = Validator(require_all=True)
    assert_fail(
        {},
        schema,
        validator,
        error=('foo', '__require_all__', errors.REQUIRED_FIELD, True),
    )
    assert_success({'foo': 'bar'}, schema, validator)
    validator.require_all = False
    assert_success({}, schema, validator)
    assert_success({'foo': 'bar'}, schema, validator)
Beispiel #52
0
def test_anyof_2():
    # these two schema should be the same
    schema1 = {'prop': {'anyof': [{'type': 'dict',
                                   'schema': {
                                       'val': {'type': 'integer'}}},
                                  {'type': 'dict',
                                   'schema': {
                                       'val': {'type': 'string'}}}]}}
    schema2 = {'prop': {'type': 'dict', 'anyof': [
        {'schema': {'val': {'type': 'integer'}}},
        {'schema': {'val': {'type': 'string'}}}]}}

    doc = {'prop': {'val': 0}}
    assert_success(doc, schema1)
    assert_success(doc, schema2)

    doc = {'prop': {'val': '0'}}
    assert_success(doc, schema1)
    assert_success(doc, schema2)

    doc = {'prop': {'val': 1.1}}
    assert_fail(doc, schema1)
    assert_fail(doc, schema2)
Beispiel #53
0
def test_anyof_allof():
    # prop1 can be any number outside of [0-10]
    schema = {'prop1': {'allof': [{'anyof': [{'type': 'float'},
                                             {'type': 'integer'}]},
                                  {'anyof': [{'min': 10},
                                             {'max': 0}]}
                                  ]}}

    doc = {'prop1': 11}
    assert_success(doc, schema)
    doc = {'prop1': -1}
    assert_success(doc, schema)
    doc = {'prop1': 5}
    assert_fail(doc, schema)

    doc = {'prop1': 11.5}
    assert_success(doc, schema)
    doc = {'prop1': -1.5}
    assert_success(doc, schema)
    doc = {'prop1': 5.5}
    assert_fail(doc, schema)

    doc = {'prop1': '5.5'}
    assert_fail(doc, schema)
Beispiel #54
0
def test_require_all_override_by_subdoc_require_all(validator_require_all,
                                                    sub_doc_require_all):
    sub_schema = {"bar": {"type": "string"}}
    schema = {
        "foo": {
            "type": "dict",
            "require_all": sub_doc_require_all,
            "schema": sub_schema,
        }
    }
    validator = Validator(require_all=validator_require_all)

    assert_success({"foo": {"bar": "baz"}}, schema, validator)
    if validator_require_all:
        assert_fail({}, schema, validator)
    else:
        assert_success({}, schema, validator)
    if sub_doc_require_all:
        assert_fail({"foo": {}}, schema, validator)
    else:
        assert_success({"foo": {}}, schema, validator)
Beispiel #55
0
def test_expansion_with_unvalidated_schema():
    validator = UnconcernedValidator(
        {"field": {'allof_regex': ['^Aladdin .*', '.* Sane$']}}
    )
    assert_success(document={"field": "Aladdin Sane"}, validator=validator)
Beispiel #56
0
def test_schema_registry_simple():
    schema_registry.add('foo', {'bar': {'type': 'string'}})
    schema = {'a': {'schema': 'foo'}, 'b': {'schema': 'foo'}}
    document = {'a': {'bar': 'a'}, 'b': {'bar': 'b'}}
    assert_success(document, schema)
Beispiel #57
0
def test_rules_registry_with_anyof_type():
    rules_set_registry.add('string_or_integer',
                           {'anyof_type': ['string', 'integer']})
    schema = {'soi': 'string_or_integer'}
    assert_success({'soi': 'hello'}, schema)
Beispiel #58
0
def test_recursion():
    rules_set_registry.add('self', {'type': 'dict', 'allow_unknown': 'self'})
    v = Validator(allow_unknown='self')
    assert_success({0: {1: {2: {}}}}, {}, v)
Beispiel #59
0
def test_allow_unknown_as_reference():
    rules_set_registry.add('foo', {'type': 'number'})
    v = Validator(allow_unknown='foo')
    assert_success({0: 1}, {}, v)
    assert_fail({0: 'one'}, {}, v)
Beispiel #60
0
def test_rules_set_simple():
    rules_set_registry.add('foo', {'type': 'integer'})
    assert_success({'bar': 1}, {'bar': 'foo'})
    assert_fail({'bar': 'one'}, {'bar': 'foo'})