JSON Schema验证失败:格式'email'拒绝带有加号或国际字符的有效电子邮件
JSON Schema validation fails: format 'email' rejects valid emails with plus signs or international characters
ID: data/json-schema-format-email-validation-failure
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| JSON Schema Draft-07 | active | — | — | — |
| Ajv 8.12.0 | active | — | — | — |
| Python jsonschema 4.20.0 | active | — | — | — |
| OpenAPI 3.1.0 | active | — | — | — |
根因分析
JSON Schema内置的'email'格式验证使用不支持现代电子邮件功能(如加号地址、国际化域名)的正则表达式,导致误报。
English
JSON Schema's built-in 'email' format validation uses a regex that does not support modern email features (e.g., plus addressing, internationalized domain names), causing false negatives.
官方文档
https://json-schema.org/understanding-json-schema/reference/string.html#format解决方案
-
Use a custom format validator that follows RFC 5321/5322. In Python with jsonschema, register a custom validator: `import re; email_regex = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'; jsonschema.validators.create(meta_schema=..., validators={'format': lambda v, f, s: ...})` -
In Ajv (JavaScript), use `ajv.addFormat('email', /^[^\s@]+@[^\s@]+\.[^\s@]+$/)` to override the default email format with a more permissive regex -
Switch to a validation library that supports RFC-compliant email validation, such as 'email-validator' in Python, and apply it as a custom keyword in the schema
无效尝试
常见但无效的做法:
-
70% 失败
Writing a custom regex that correctly validates all valid email addresses is extremely complex and often introduces new bugs (e.g., rejecting valid addresses or allowing invalid ones).
-
80% 失败
This disables all email validation, allowing any string to pass, which defeats the purpose of schema validation and may lead to downstream errors.
-
60% 失败
Not all validators support 'idn-email', and it still may reject plus signs or other common email features.