Python marshmallow 模块,validates() 实例源码

我们从Python开源项目中,提取了以下1个代码示例,用于说明如何使用marshmallow.validates()

项目:dynamorm    作者:NerdWalletOSS    | 项目源码 | 文件源码
def test_explicit_schema_parents():
    """Inner Schema classes should be able to have explicit parents"""
    class SuperMixin(object):
        bbq = String()

    if 'marshmallow' in (os.getenv('SERIALIZATION_PKG') or ''):
        class Mixin(SuperMixin):
            is_mixin = True
            bar = String()

            @validates('bar')
            def validate_bar(self, value):
                if value != 'bar':
                    raise SchemaValidationError('bar must be bar')
    else:
        class Mixin(SuperMixin):
            is_mixin = True
            bar = String()

            def validate_bar(self, data, value):
                if value != 'bar':
                    raise SchemaValidationError('bar must be bar')

    class Model(DynaModel):
        class Table:
            name = 'table'
            hash_key = 'foo'
            read = 1
            write = 1

        class Schema(Mixin):
            foo = Number(required=True)
            baz = String(required=True)

    assert Model.Schema.is_mixin is True
    assert list(sorted(Model.Schema.dynamorm_fields().keys())) == ['bar', 'baz', 'bbq', 'foo']

    with pytest.raises(ValidationError):
        Model(foo='foo', baz='baz', bar='not bar')