top-level marshmallow schema validation

*爱你&永不变心* 提交于 2019-12-05 16:36:16
Maxim Kulkin

You can use validates_schema decorator to run validations on whole object:

class MySchema(marshmallow.Schema):
    # ...

    @marshmallow.validates_schema(skip_on_field_errors=True)
    def validate_object(self, data):
        if data['foo'] < data['bar']:
            raise marshmallow.ValidationError(
                'Value should not be less than bar',
                ['foo']  # name of field to report error for
            )

Although if you want to report multiple errors for different fields independently, Marshmallow at this moment does not support reporting multiple different errors for different fields, and you need to put separate validations into separate methods:

class MySchema(Schema):
    # ...

    @validates_schema
    def validate_foo(self, data):
        pass

    @validates_schema(skip_on_field_errors=True)
    def validate_bar(self, data):
        pass
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!