Jim*_* Jo 8 python marshmallow
我想指定一个棉花糖模式。对于我的一个字段,我希望对其进行验证,但它可以是字符串或字符串列表。我尝试过原始字段类型,但这允许一切通过。有没有办法只验证我想要的两种类型?
就像是,
value = fields.Str() or fields.List()
Run Code Online (Sandbox Code Playgroud)
我今天遇到了同样的问题,我想出了这个解决方案:
class ValueField(fields.Field):
def _deserialize(self, value, attr, data, **kwargs):
if isinstance(value, str) or isinstance(value, list):
return value
else:
raise ValidationError('Field should be str or list')
class Foo(Schema):
value = ValueField()
other_field = fields.Integer()
Run Code Online (Sandbox Code Playgroud)
您可以创建自定义字段并重载该_deserialize方法,以便它验证代码是否isinstance符合所需类型。我希望它对你有用。
foo.load({'value': 'asdf', 'other_field': 1})
>>> {'other_field': 1, 'value': 'asdf'}
foo.load({'value': ['asdf'], 'other_field': 1})
>>> {'other_field': 1, 'value': ['asdf']}
foo.load({'value': 1, 'other_field': 1})
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Users/webinterpret/Envs/gl-gs-onboarding-api/lib/python3.7/site-packages/marshmallow/schema.py", line 723, in load
data, many=many, partial=partial, unknown=unknown, postprocess=True
File "/Users/webinterpret/Envs/gl-gs-onboarding-api/lib/python3.7/site-packages/marshmallow/schema.py", line 904, in _do_load
raise exc
marshmallow.exceptions.ValidationError: {'value': ['Field should be str or list']}
Run Code Online (Sandbox Code Playgroud)
映射的解决方案,类似于上面的:
from typing import List, Mapping, Any
from marshmallow import Schema, fields
from marshmallow.exceptions import ValidationError
class UnionField(fields.Field):
"""Field that deserializes multi-type input data to app-level objects."""
def __init__(self, val_types: List[fields.Field]):
self.valid_types = val_types
super().__init__()
def _deserialize(
self, value: Any, attr: str = None, data: Mapping[str, Any] = None, **kwargs
):
"""
_deserialize defines a custom Marshmallow Schema Field that takes in mutli-type input data to
app-level objects.
Parameters
----------
value : {Any}
The value to be deserialized.
Keyword Parameters
----------
attr : {str} [Optional]
The attribute/key in data to be deserialized. (default: {None})
data : {Optional[Mapping[str, Any]]}
The raw input data passed to the Schema.load. (default: {None})
Raises
----------
ValidationError : Exception
Raised when the validation fails on a field or schema.
"""
errors = []
# iterate through the types being passed into UnionField via val_types
for field in self.valid_types:
try:
# inherit deserialize method from Fields class
return field.deserialize(value, attr, data, **kwargs)
# if error, add error message to error list
except ValidationError as error:
errors.append(error.messages)
raise ValidationError(errors)
Run Code Online (Sandbox Code Playgroud)
使用:
class SampleSchema(Schema):
ex_attr = fields.Dict(keys=fields.Str(), values=UnionField([fields.Str(), fields.Number()]))
Run Code Online (Sandbox Code Playgroud)
图片来源:安娜·K
| 归档时间: |
|
| 查看次数: |
13089 次 |
| 最近记录: |