看看
https://github.com/marshmallow-code/marshmallow/blob/dev/src/marshmallow/fields.py
Field 类定义了“required”、“null”和“validator_failed”
class Field(FieldABC):
...
default_error_messages = {
"required": "Missing data for required field.",
"null": "Field may not be null.",
"validator_failed": "Invalid value.",
}
...
特定的字段实现继承了这些消息并可能扩展列表。例如,电子邮件定义附加错误消息“无效”
class Email(String):
...
default_error_messages = {"invalid": "Not a valid email address."}
...
注意:看起来电子邮件和其他字段会覆盖 default_error_messages(而不是扩展它们),但实际上错误消息是“收集的”显式的,包括父类
class Field(FieldABC):
...
# Collect default error message from self and parent classes
messages = {} # type: typing.Dict[str, str]
for cls in reversed(self.__class__.__mro__):
messages.update(getattr(cls, "default_error_messages", {}))
messages.update(error_messages or {})
self.error_messages = messages
...