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

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

项目:umongo    作者:Scille    | 项目源码 | 文件源码
def schema_validator_check_unknown_fields(self, data, original_data):
    """
    Schema validator, raise ValidationError for unknown fields in a
    marshmallow schema.

    example::

        class MySchema(marshsmallow.Schema):
            # method's name is not important
            __check_unknown_fields = validates_schema(pass_original=True)(
                schema_validator_check_unknown_fields)

            # Define the rest of your schema
            ...

    ..note:: Unknown fields with `missing` value will be ignored
    """
    # Just skip if dummy data have been passed to the schema
    if not isinstance(original_data, dict):
        return
    loadable_fields = [k for k, v in self.fields.items() if not v.dump_only]
    unknown_fields = {key for key, value in original_data.items()
                      if value is not missing and key not in loadable_fields}
    if unknown_fields:
        raise ValidationError([_('Unknown field name {field}.').format(field=field)
                               for field in unknown_fields])
项目:umongo    作者:Scille    | 项目源码 | 文件源码
def test_marshmallow_schema_helpers(self):

        class CheckUnknownSchema(marshmallow.Schema):
            __check_unknown_fields = marshmallow.validates_schema(
                pass_original=True)(schema_validator_check_unknown_fields)
            a = marshmallow.fields.Int()

        _, errors = CheckUnknownSchema().load({'a': 1, 'dummy': 2})
        assert errors == {'_schema': ['Unknown field name dummy.']}

        data, errors = CheckUnknownSchema().load({'a': 1})
        assert not errors
        assert data == {'a': 1}

        @self.instance.register
        class Doc(Document):
            a = fields.IntField()

            @property
            def prop(self):
                return "I'm a property !"

        class VanillaSchema(marshmallow.Schema):
            a = marshmallow.fields.Int()

        class CustomGetAttributeSchema(VanillaSchema):
            get_attribute = schema_from_umongo_get_attribute

        data, errors = VanillaSchema().dump(Doc())
        assert not errors
        assert data == {'a': None}

        data, errors = CustomGetAttributeSchema().dump(Doc())
        assert not errors
        assert data == {}

        data, errors = CustomGetAttributeSchema().dump(Doc(a=1))
        assert not errors
        assert data == {'a': 1}

        class MySchemaFromUmongo(SchemaFromUmongo):
            a = marshmallow.fields.Int()
            prop = marshmallow.fields.String(dump_only=True)

        data, errors = MySchemaFromUmongo().dump(Doc())
        assert not errors
        assert data == {'prop': "I'm a property !"}

        _, errors = MySchemaFromUmongo().load({'a': 1, 'dummy': 2})
        assert errors == {'_schema': ['Unknown field name dummy.']}

        _, errors = MySchemaFromUmongo().load({'a': 1, 'prop': '2'})
        assert errors == {'_schema': ['Unknown field name prop.']}