Example #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)
Example #2
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')
Example #3
0
def test_ignore_none_values():
    # original commits:
    # 96532fc8efbc0b057dd6cd23d0324c8c5a929456
    # d6422991c41587467673716cb6e4e929fa9d7b77

    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
    assert validator.schema[field].get('required') is True
    _errors = assert_fail(document=document, validator=validator)
    assert_has_error(_errors, field, (field, 'required'), errors.REQUIRED_FIELD, True)
    assert_not_has_error(_errors, field, (field, 'type'), errors.TYPE, 'string')
Example #4
0
def test_coerce_catches_TypeError():
    schema = {'name': {'coerce': str.lower}}
    _errors = assert_fail({'name': 1234}, schema)
    _errors[0].info = ()  # ignore exception message here
    assert_has_error(
        _errors, 'name', ('name', 'coerce'), errors.COERCION_FAILED, str.lower
    )
Example #5
0
def test_coerce_catches_ValueError():
    schema = {'amount': {'coerce': int}}
    _errors = assert_fail({'amount': 'not_a_number'}, schema)
    _errors[0].info = ()  # ignore exception message here
    assert_has_error(
        _errors, 'amount', ('amount', 'coerce'), errors.COERCION_FAILED, int
    )
Example #6
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')
Example #7
0
def test_allow_unknown_with_oneof_rules(validator):
    # https://github.com/nicolaiarocci/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)
Example #8
0
def test_coerce_in_listitems_catches_TypeError():
    schema = {'things': {'type': 'list',
                         'items': [{'coerce': int}, {'coerce': str.lower}]}}
    document = {'things': ['1', 2]}
    _errors = assert_fail(document, schema)
    _errors[0].info = ()  # ignore exception message here
    assert_has_error(_errors, ('things', 1), ('things', 'items', 'coerce'),
                     errors.COERCION_FAILED, str.lower)
def test_coerce_in_listitems_catches_ValueError():
    schema = {'things': {'type': 'list', 'items': [{'coerce': int}, {'coerce': str}]}}
    document = {'things': ['not_a_number', 2]}
    _errors = assert_fail(document, schema)
    _errors[0].info = ()  # ignore exception message here
    assert_has_error(
        _errors,
        ('things', 0),
        ('things', 'items', 'coerce'),
        errors.COERCION_FAILED,
        int,
    )
Example #10
0
def test_allow_unknown_with_oneof_rules(validator):
    # https://github.com/nicolaiarocci/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)
Example #11
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")