Skip to content

Commit 4c007cb

Browse files
authored
Update custom validation error messages (#566)
1 parent c9e9a45 commit 4c007cb

File tree

1 file changed

+96
-97
lines changed

1 file changed

+96
-97
lines changed

backend/common/schema.py

Lines changed: 96 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -7,106 +7,105 @@
77

88
from backend.core.conf import settings
99

10-
# 自定义验证错误信息
11-
# 不包含验证预期内容(也就是输入内容),受支持的预期内容字段参考以下链接
10+
# 自定义验证错误信息,参考:
1211
# https://github.com/pydantic/pydantic-core/blob/a5cb7382643415b716b1a7a5392914e50f726528/tests/test_errors.py#L266
13-
# 替换预期内容字段方式,参考以下链接
1412
# https://github.com/pydantic/pydantic/blob/caa78016433ec9b16a973f92f187a7b6bfde6cb5/docs/errors/errors.md?plain=1#L232
1513
CUSTOM_VALIDATION_ERROR_MESSAGES = {
16-
'arguments_type': '参数类型输入错误',
17-
'assertion_error': '断言执行错误',
18-
'bool_parsing': '布尔值输入解析错误',
19-
'bool_type': '布尔值类型输入错误',
20-
'bytes_too_long': '字节长度输入过长',
21-
'bytes_too_short': '字节长度输入过短',
22-
'bytes_type': '字节类型输入错误',
23-
'callable_type': '可调用对象类型输入错误',
24-
'dataclass_exact_type': '数据类实例类型输入错误',
25-
'dataclass_type': '数据类类型输入错误',
26-
'date_from_datetime_inexact': '日期分量输入非零',
27-
'date_from_datetime_parsing': '日期输入解析错误',
28-
'date_future': '日期输入非将来时',
29-
'date_parsing': '日期输入验证错误',
30-
'date_past': '日期输入非过去时',
31-
'date_type': '日期类型输入错误',
32-
'datetime_future': '日期时间输入非将来时间',
33-
'datetime_object_invalid': '日期时间输入对象无效',
34-
'datetime_parsing': '日期时间输入解析错误',
35-
'datetime_past': '日期时间输入非过去时间',
36-
'datetime_type': '日期时间类型输入错误',
37-
'decimal_max_digits': '小数位数输入过多',
38-
'decimal_max_places': '小数位数输入错误',
39-
'decimal_parsing': '小数输入解析错误',
40-
'decimal_type': '小数类型输入错误',
41-
'decimal_whole_digits': '小数位数输入错误',
42-
'dict_type': '字典类型输入错误',
43-
'enum': '枚举成员输入错误,允许 {expected}',
44-
'extra_forbidden': '禁止额外字段输入',
45-
'finite_number': '有限值输入错误',
46-
'float_parsing': '浮点数输入解析错误',
47-
'float_type': '浮点数类型输入错误',
48-
'frozen_field': '冻结字段输入错误',
49-
'frozen_instance': '冻结实例禁止修改',
50-
'frozen_set_type': '冻结类型禁止输入',
51-
'get_attribute_error': '获取属性错误',
52-
'greater_than': '输入值过大',
53-
'greater_than_equal': '输入值过大或相等',
54-
'int_from_float': '整数类型输入错误',
55-
'int_parsing': '整数输入解析错误',
56-
'int_parsing_size': '整数输入解析长度错误',
57-
'int_type': '整数类型输入错误',
58-
'invalid_key': '输入无效键值',
59-
'is_instance_of': '类型实例输入错误',
60-
'is_subclass_of': '类型子类输入错误',
61-
'iterable_type': '可迭代类型输入错误',
62-
'iteration_error': '迭代值输入错误',
63-
'json_invalid': 'JSON 字符串输入错误',
64-
'json_type': 'JSON 类型输入错误',
65-
'less_than': '输入值过小',
66-
'less_than_equal': '输入值过小或相等',
67-
'list_type': '列表类型输入错误',
68-
'literal_error': '字面值输入错误',
69-
'mapping_type': '映射类型输入错误',
70-
'missing': '缺少必填字段',
71-
'missing_argument': '缺少参数',
72-
'missing_keyword_only_argument': '缺少关键字参数',
73-
'missing_positional_only_argument': '缺少位置参数',
74-
'model_attributes_type': '模型属性类型输入错误',
75-
'model_type': '模型实例输入错误',
76-
'multiple_argument_values': '参数值输入过多',
77-
'multiple_of': '输入值非倍数',
78-
'no_such_attribute': '分配无效属性值',
79-
'none_required': '输入值必须为 None',
80-
'recursion_loop': '输入循环赋值',
81-
'set_type': '集合类型输入错误',
82-
'string_pattern_mismatch': '字符串约束模式输入不匹配',
83-
'string_sub_type': '字符串子类型(非严格实例)输入错误',
84-
'string_too_long': '字符串输入过长',
85-
'string_too_short': '字符串输入过短',
86-
'string_type': '字符串类型输入错误',
87-
'string_unicode': '字符串输入非 Unicode',
88-
'time_delta_parsing': '时间差输入解析错误',
89-
'time_delta_type': '时间差类型输入错误',
90-
'time_parsing': '时间输入解析错误',
91-
'time_type': '时间类型输入错误',
92-
'timezone_aware': '缺少时区输入信息',
93-
'timezone_naive': '禁止时区输入信息',
94-
'too_long': '输入过长',
95-
'too_short': '输入过短',
96-
'tuple_type': '元组类型输入错误',
97-
'unexpected_keyword_argument': '输入意外关键字参数',
98-
'unexpected_positional_argument': '输入意外位置参数',
99-
'union_tag_invalid': '联合类型字面值输入错误',
100-
'union_tag_not_found': '联合类型参数输入未找到',
101-
'url_parsing': 'URL 输入解析错误',
102-
'url_scheme': 'URL 输入方案错误',
103-
'url_syntax_violation': 'URL 输入语法错误',
104-
'url_too_long': 'URL 输入过长',
105-
'url_type': 'URL 类型输入错误',
106-
'uuid_parsing': 'UUID 输入解析错误',
107-
'uuid_type': 'UUID 类型输入错误',
108-
'uuid_version': 'UUID 版本类型输入错误',
109-
'value_error': '值输入错误',
14+
'no_such_attribute': "对象没有属性 '{attribute}'",
15+
'json_invalid': '无效的 JSON: {error}',
16+
'json_type': 'JSON 输入应为字符串、字节或字节数组',
17+
'recursion_loop': '递归错误 - 检测到循环引用',
18+
'model_type': '输入应为有效的字典或 {class_name} 的实例',
19+
'model_attributes_type': '输入应为有效的字典或可提取字段的对象',
20+
'dataclass_exact_type': '输入应为 {class_name} 的实例',
21+
'dataclass_type': '输入应为字典或 {class_name} 的实例',
22+
'missing': '字段为必填项',
23+
'frozen_field': '字段已冻结',
24+
'frozen_instance': '实例已冻结',
25+
'extra_forbidden': '不允许额外的输入',
26+
'invalid_key': '键应为字符串',
27+
'get_attribute_error': '提取属性时出错: {error}',
28+
'none_required': '输入应为 None',
29+
'enum': '输入应为 {expected}',
30+
'greater_than': '输入应大于 {gt}',
31+
'greater_than_equal': '输入应大于或等于 {ge}',
32+
'less_than': '输入应小于 {lt}',
33+
'less_than_equal': '输入应小于或等于 {le}',
34+
'finite_number': '输入应为有限数字',
35+
'too_short': '{field_type} 在验证后应至少有 {min_length} 个项目,而不是 {actual_length}',
36+
'too_long': '{field_type} 在验证后最多应有 {max_length} 个项目,而不是 {actual_length}',
37+
'string_type': '输入应为有效的字符串',
38+
'string_sub_type': '输入应为字符串,而不是 str 子类的实例',
39+
'string_unicode': '输入应为有效的字符串,无法将原始数据解析为 Unicode 字符串',
40+
'string_pattern_mismatch': "字符串应匹配模式 '{pattern}'",
41+
'string_too_short': '字符串应至少有 {min_length} 个字符',
42+
'string_too_long': '字符串最多应有 {max_length} 个字符',
43+
'dict_type': '输入应为有效的字典',
44+
'mapping_type': '输入应为有效的映射,错误: {error}',
45+
'iterable_type': '输入应为可迭代对象',
46+
'iteration_error': '迭代对象时出错,错误: {error}',
47+
'list_type': '输入应为有效的列表',
48+
'tuple_type': '输入应为有效的元组',
49+
'set_type': '输入应为有效的集合',
50+
'bool_type': '输入应为有效的布尔值',
51+
'bool_parsing': '输入应为有效的布尔值,无法解释输入',
52+
'int_type': '输入应为有效的整数',
53+
'int_parsing': '输入应为有效的整数,无法将字符串解析为整数',
54+
'int_parsing_size': '无法将输入字符串解析为整数,超出最大大小',
55+
'int_from_float': '输入应为有效的整数,得到一个带有小数部分的数字',
56+
'multiple_of': '输入应为 {multiple_of} 的倍数',
57+
'float_type': '输入应为有效的数字',
58+
'float_parsing': '输入应为有效的数字,无法将字符串解析为数字',
59+
'bytes_type': '输入应为有效的字节',
60+
'bytes_too_short': '数据应至少有 {min_length} 个字节',
61+
'bytes_too_long': '数据最多应有 {max_length} 个字节',
62+
'value_error': '值错误,{error}',
63+
'assertion_error': '断言失败,{error}',
64+
'literal_error': '输入应为 {expected}',
65+
'date_type': '输入应为有效的日期',
66+
'date_parsing': '输入应为 YYYY-MM-DD 格式的有效日期,{error}',
67+
'date_from_datetime_parsing': '输入应为有效的日期或日期时间,{error}',
68+
'date_from_datetime_inexact': '提供给日期的日期时间应具有零时间 - 例如为精确日期',
69+
'date_past': '日期应为过去的时间',
70+
'date_future': '日期应为未来的时间',
71+
'time_type': '输入应为有效的时间',
72+
'time_parsing': '输入应为有效的时间格式,{error}',
73+
'datetime_type': '输入应为有效的日期时间',
74+
'datetime_parsing': '输入应为有效的日期时间,{error}',
75+
'datetime_object_invalid': '无效的日期时间对象,得到 {error}',
76+
'datetime_past': '输入应为过去的时间',
77+
'datetime_future': '输入应为未来的时间',
78+
'timezone_naive': '输入不应包含时区信息',
79+
'timezone_aware': '输入应包含时区信息',
80+
'timezone_offset': '需要时区偏移为 {tz_expected},实际得到 {tz_actual}',
81+
'time_delta_type': '输入应为有效的时间差',
82+
'time_delta_parsing': '输入应为有效的时间差,{error}',
83+
'frozen_set_type': '输入应为有效的冻结集合',
84+
'is_instance_of': '输入应为 {class} 的实例',
85+
'is_subclass_of': '输入应为 {class} 的子类',
86+
'callable_type': '输入应为可调用对象',
87+
'union_tag_invalid': "使用 {discriminator} 找到的输入标签 '{tag}' 与任何预期标签不匹配: {expected_tags}",
88+
'union_tag_not_found': '无法使用区分器 {discriminator} 提取标签',
89+
'arguments_type': '参数必须是元组、列表或字典',
90+
'missing_argument': '缺少必需参数',
91+
'unexpected_keyword_argument': '意外的关键字参数',
92+
'missing_keyword_only_argument': '缺少必需的关键字专用参数',
93+
'unexpected_positional_argument': '意外的位置参数',
94+
'missing_positional_only_argument': '缺少必需的位置专用参数',
95+
'multiple_argument_values': '为参数提供了多个值',
96+
'url_type': 'URL 输入应为字符串或 URL',
97+
'url_parsing': '输入应为有效的 URL,{error}',
98+
'url_syntax_violation': '输入违反了严格的 URL 语法规则,{error}',
99+
'url_too_long': 'URL 最多应有 {max_length} 个字符',
100+
'url_scheme': 'URL 方案应为 {expected_schemes}',
101+
'uuid_type': 'UUID 输入应为字符串、字节或 UUID 对象',
102+
'uuid_parsing': '输入应为有效的 UUID,{error}',
103+
'uuid_version': '预期 UUID 版本为 {expected_version}',
104+
'decimal_type': '十进制输入应为整数、浮点数、字符串或 Decimal 对象',
105+
'decimal_parsing': '输入应为有效的十进制数',
106+
'decimal_max_digits': '十进制输入总共应不超过 {max_digits} 位数字',
107+
'decimal_max_places': '十进制输入应不超过 {decimal_places} 位小数',
108+
'decimal_whole_digits': '十进制输入在小数点前应不超过 {whole_digits} 位数字',
110109
}
111110

112111
CustomPhoneNumber = Annotated[str, Field(pattern=r'^1[3-9]\d{9}$')]

0 commit comments

Comments
 (0)