python data_error ai_generated true

pydantic.errors.EmailError: 值不是有效的电子邮件地址

pydantic.errors.EmailError: value is not a valid email address

ID: python/pydantic-email-format-invalid

其他格式: JSON · Markdown 中文 · English
80%修复率
87%置信度
0证据数
2024-03-01首次发现

版本兼容性

版本状态引入弃用备注
2.x active

根因分析

使用 EmailStr 类型时,输入字符串不符合电子邮件格式(例如缺少 @、域名无效)。

English

Using EmailStr type, the input string does not conform to email format (e.g., missing @, invalid domain).

generic

解决方案

  1. 95% 成功率
    Use EmailStr from pydantic: email: EmailStr
  2. 90% 成功率
    Sanitize input before validation: @field_validator('email', mode='before') def clean(cls, v): return v.strip()

无效尝试

常见但无效的做法:

  1. 60% 失败

    Custom regex may not capture all email edge cases, leading to false positives.

  2. 70% 失败

    Empty string is not a valid email, even if optional.