From 8b542aaaab791b001359a03a3d3c0b0d37de68f0 Mon Sep 17 00:00:00 2001 From: Wu Clan Date: Thu, 3 Apr 2025 19:30:36 +0800 Subject: [PATCH] Update custom validation error messages --- backend/common/schema.py | 193 +++++++++++++++++++-------------------- 1 file changed, 96 insertions(+), 97 deletions(-) diff --git a/backend/common/schema.py b/backend/common/schema.py index 87ec90b7a..cb944a1e6 100644 --- a/backend/common/schema.py +++ b/backend/common/schema.py @@ -7,106 +7,105 @@ from backend.core.conf import settings -# 自定义验证错误信息 -# 不包含验证预期内容(也就是输入内容),受支持的预期内容字段参考以下链接 +# 自定义验证错误信息,参考: # https://github.com/pydantic/pydantic-core/blob/a5cb7382643415b716b1a7a5392914e50f726528/tests/test_errors.py#L266 -# 替换预期内容字段方式,参考以下链接 # https://github.com/pydantic/pydantic/blob/caa78016433ec9b16a973f92f187a7b6bfde6cb5/docs/errors/errors.md?plain=1#L232 CUSTOM_VALIDATION_ERROR_MESSAGES = { - 'arguments_type': '参数类型输入错误', - 'assertion_error': '断言执行错误', - 'bool_parsing': '布尔值输入解析错误', - 'bool_type': '布尔值类型输入错误', - 'bytes_too_long': '字节长度输入过长', - 'bytes_too_short': '字节长度输入过短', - 'bytes_type': '字节类型输入错误', - 'callable_type': '可调用对象类型输入错误', - 'dataclass_exact_type': '数据类实例类型输入错误', - 'dataclass_type': '数据类类型输入错误', - 'date_from_datetime_inexact': '日期分量输入非零', - 'date_from_datetime_parsing': '日期输入解析错误', - 'date_future': '日期输入非将来时', - 'date_parsing': '日期输入验证错误', - 'date_past': '日期输入非过去时', - 'date_type': '日期类型输入错误', - 'datetime_future': '日期时间输入非将来时间', - 'datetime_object_invalid': '日期时间输入对象无效', - 'datetime_parsing': '日期时间输入解析错误', - 'datetime_past': '日期时间输入非过去时间', - 'datetime_type': '日期时间类型输入错误', - 'decimal_max_digits': '小数位数输入过多', - 'decimal_max_places': '小数位数输入错误', - 'decimal_parsing': '小数输入解析错误', - 'decimal_type': '小数类型输入错误', - 'decimal_whole_digits': '小数位数输入错误', - 'dict_type': '字典类型输入错误', - 'enum': '枚举成员输入错误,允许 {expected}', - 'extra_forbidden': '禁止额外字段输入', - 'finite_number': '有限值输入错误', - 'float_parsing': '浮点数输入解析错误', - 'float_type': '浮点数类型输入错误', - 'frozen_field': '冻结字段输入错误', - 'frozen_instance': '冻结实例禁止修改', - 'frozen_set_type': '冻结类型禁止输入', - 'get_attribute_error': '获取属性错误', - 'greater_than': '输入值过大', - 'greater_than_equal': '输入值过大或相等', - 'int_from_float': '整数类型输入错误', - 'int_parsing': '整数输入解析错误', - 'int_parsing_size': '整数输入解析长度错误', - 'int_type': '整数类型输入错误', - 'invalid_key': '输入无效键值', - 'is_instance_of': '类型实例输入错误', - 'is_subclass_of': '类型子类输入错误', - 'iterable_type': '可迭代类型输入错误', - 'iteration_error': '迭代值输入错误', - 'json_invalid': 'JSON 字符串输入错误', - 'json_type': 'JSON 类型输入错误', - 'less_than': '输入值过小', - 'less_than_equal': '输入值过小或相等', - 'list_type': '列表类型输入错误', - 'literal_error': '字面值输入错误', - 'mapping_type': '映射类型输入错误', - 'missing': '缺少必填字段', - 'missing_argument': '缺少参数', - 'missing_keyword_only_argument': '缺少关键字参数', - 'missing_positional_only_argument': '缺少位置参数', - 'model_attributes_type': '模型属性类型输入错误', - 'model_type': '模型实例输入错误', - 'multiple_argument_values': '参数值输入过多', - 'multiple_of': '输入值非倍数', - 'no_such_attribute': '分配无效属性值', - 'none_required': '输入值必须为 None', - 'recursion_loop': '输入循环赋值', - 'set_type': '集合类型输入错误', - 'string_pattern_mismatch': '字符串约束模式输入不匹配', - 'string_sub_type': '字符串子类型(非严格实例)输入错误', - 'string_too_long': '字符串输入过长', - 'string_too_short': '字符串输入过短', - 'string_type': '字符串类型输入错误', - 'string_unicode': '字符串输入非 Unicode', - 'time_delta_parsing': '时间差输入解析错误', - 'time_delta_type': '时间差类型输入错误', - 'time_parsing': '时间输入解析错误', - 'time_type': '时间类型输入错误', - 'timezone_aware': '缺少时区输入信息', - 'timezone_naive': '禁止时区输入信息', - 'too_long': '输入过长', - 'too_short': '输入过短', - 'tuple_type': '元组类型输入错误', - 'unexpected_keyword_argument': '输入意外关键字参数', - 'unexpected_positional_argument': '输入意外位置参数', - 'union_tag_invalid': '联合类型字面值输入错误', - 'union_tag_not_found': '联合类型参数输入未找到', - 'url_parsing': 'URL 输入解析错误', - 'url_scheme': 'URL 输入方案错误', - 'url_syntax_violation': 'URL 输入语法错误', - 'url_too_long': 'URL 输入过长', - 'url_type': 'URL 类型输入错误', - 'uuid_parsing': 'UUID 输入解析错误', - 'uuid_type': 'UUID 类型输入错误', - 'uuid_version': 'UUID 版本类型输入错误', - 'value_error': '值输入错误', + 'no_such_attribute': "对象没有属性 '{attribute}'", + 'json_invalid': '无效的 JSON: {error}', + 'json_type': 'JSON 输入应为字符串、字节或字节数组', + 'recursion_loop': '递归错误 - 检测到循环引用', + 'model_type': '输入应为有效的字典或 {class_name} 的实例', + 'model_attributes_type': '输入应为有效的字典或可提取字段的对象', + 'dataclass_exact_type': '输入应为 {class_name} 的实例', + 'dataclass_type': '输入应为字典或 {class_name} 的实例', + 'missing': '字段为必填项', + 'frozen_field': '字段已冻结', + 'frozen_instance': '实例已冻结', + 'extra_forbidden': '不允许额外的输入', + 'invalid_key': '键应为字符串', + 'get_attribute_error': '提取属性时出错: {error}', + 'none_required': '输入应为 None', + 'enum': '输入应为 {expected}', + 'greater_than': '输入应大于 {gt}', + 'greater_than_equal': '输入应大于或等于 {ge}', + 'less_than': '输入应小于 {lt}', + 'less_than_equal': '输入应小于或等于 {le}', + 'finite_number': '输入应为有限数字', + 'too_short': '{field_type} 在验证后应至少有 {min_length} 个项目,而不是 {actual_length}', + 'too_long': '{field_type} 在验证后最多应有 {max_length} 个项目,而不是 {actual_length}', + 'string_type': '输入应为有效的字符串', + 'string_sub_type': '输入应为字符串,而不是 str 子类的实例', + 'string_unicode': '输入应为有效的字符串,无法将原始数据解析为 Unicode 字符串', + 'string_pattern_mismatch': "字符串应匹配模式 '{pattern}'", + 'string_too_short': '字符串应至少有 {min_length} 个字符', + 'string_too_long': '字符串最多应有 {max_length} 个字符', + 'dict_type': '输入应为有效的字典', + 'mapping_type': '输入应为有效的映射,错误: {error}', + 'iterable_type': '输入应为可迭代对象', + 'iteration_error': '迭代对象时出错,错误: {error}', + 'list_type': '输入应为有效的列表', + 'tuple_type': '输入应为有效的元组', + 'set_type': '输入应为有效的集合', + 'bool_type': '输入应为有效的布尔值', + 'bool_parsing': '输入应为有效的布尔值,无法解释输入', + 'int_type': '输入应为有效的整数', + 'int_parsing': '输入应为有效的整数,无法将字符串解析为整数', + 'int_parsing_size': '无法将输入字符串解析为整数,超出最大大小', + 'int_from_float': '输入应为有效的整数,得到一个带有小数部分的数字', + 'multiple_of': '输入应为 {multiple_of} 的倍数', + 'float_type': '输入应为有效的数字', + 'float_parsing': '输入应为有效的数字,无法将字符串解析为数字', + 'bytes_type': '输入应为有效的字节', + 'bytes_too_short': '数据应至少有 {min_length} 个字节', + 'bytes_too_long': '数据最多应有 {max_length} 个字节', + 'value_error': '值错误,{error}', + 'assertion_error': '断言失败,{error}', + 'literal_error': '输入应为 {expected}', + 'date_type': '输入应为有效的日期', + 'date_parsing': '输入应为 YYYY-MM-DD 格式的有效日期,{error}', + 'date_from_datetime_parsing': '输入应为有效的日期或日期时间,{error}', + 'date_from_datetime_inexact': '提供给日期的日期时间应具有零时间 - 例如为精确日期', + 'date_past': '日期应为过去的时间', + 'date_future': '日期应为未来的时间', + 'time_type': '输入应为有效的时间', + 'time_parsing': '输入应为有效的时间格式,{error}', + 'datetime_type': '输入应为有效的日期时间', + 'datetime_parsing': '输入应为有效的日期时间,{error}', + 'datetime_object_invalid': '无效的日期时间对象,得到 {error}', + 'datetime_past': '输入应为过去的时间', + 'datetime_future': '输入应为未来的时间', + 'timezone_naive': '输入不应包含时区信息', + 'timezone_aware': '输入应包含时区信息', + 'timezone_offset': '需要时区偏移为 {tz_expected},实际得到 {tz_actual}', + 'time_delta_type': '输入应为有效的时间差', + 'time_delta_parsing': '输入应为有效的时间差,{error}', + 'frozen_set_type': '输入应为有效的冻结集合', + 'is_instance_of': '输入应为 {class} 的实例', + 'is_subclass_of': '输入应为 {class} 的子类', + 'callable_type': '输入应为可调用对象', + 'union_tag_invalid': "使用 {discriminator} 找到的输入标签 '{tag}' 与任何预期标签不匹配: {expected_tags}", + 'union_tag_not_found': '无法使用区分器 {discriminator} 提取标签', + 'arguments_type': '参数必须是元组、列表或字典', + 'missing_argument': '缺少必需参数', + 'unexpected_keyword_argument': '意外的关键字参数', + 'missing_keyword_only_argument': '缺少必需的关键字专用参数', + 'unexpected_positional_argument': '意外的位置参数', + 'missing_positional_only_argument': '缺少必需的位置专用参数', + 'multiple_argument_values': '为参数提供了多个值', + 'url_type': 'URL 输入应为字符串或 URL', + 'url_parsing': '输入应为有效的 URL,{error}', + 'url_syntax_violation': '输入违反了严格的 URL 语法规则,{error}', + 'url_too_long': 'URL 最多应有 {max_length} 个字符', + 'url_scheme': 'URL 方案应为 {expected_schemes}', + 'uuid_type': 'UUID 输入应为字符串、字节或 UUID 对象', + 'uuid_parsing': '输入应为有效的 UUID,{error}', + 'uuid_version': '预期 UUID 版本为 {expected_version}', + 'decimal_type': '十进制输入应为整数、浮点数、字符串或 Decimal 对象', + 'decimal_parsing': '输入应为有效的十进制数', + 'decimal_max_digits': '十进制输入总共应不超过 {max_digits} 位数字', + 'decimal_max_places': '十进制输入应不超过 {decimal_places} 位小数', + 'decimal_whole_digits': '十进制输入在小数点前应不超过 {whole_digits} 位数字', } CustomPhoneNumber = Annotated[str, Field(pattern=r'^1[3-9]\d{9}$')]