python
data_error
ai_generated
true
pydantic_core._pydantic_core.ValidationError: Endpoint 模型验证错误:URL scheme 必须是 'http' 或 'https'
pydantic_core._pydantic_core.ValidationError: 1 validation error for Endpoint url URL scheme should be 'http' or 'https' [type=url_scheme, input_value='ftp://example.com', input_type=str]
ID: python/pydantic-validation-error-url-scheme
80%修复率
87%置信度
0证据数
2024-08-03首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 2.x | active | — | — | — |
根因分析
AnyHttpUrl / HttpUrl 类型拒绝非 http(s) 协议。传入 'ftp://'、'file://' 或 'ws://' 会触发 url_scheme 错误。
English
AnyHttpUrl / HttpUrl types reject non-http(s) schemes. Passing 'ftp://', 'file://', or 'ws://' triggers url_scheme error.
解决方案
-
95% 成功率
from pydantic import BaseModel, AnyUrl class Endpoint(BaseModel): url: AnyUrl -
88% 成功率
from pydantic import BaseModel, HttpUrl, field_validator class Endpoint(BaseModel): url: HttpUrl @field_validator('url', mode='before') @classmethod def fix_scheme(cls, v): return v.replace('ftp://', 'https://') if isinstance(v, str) else v
无效尝试
常见但无效的做法:
-
90% 失败
Produces invalid URLs like 'http://ftp://example.com' which fail further validation.
-
70% 失败
Loses URL validation entirely; invalid URLs slip through and break HTTP clients downstream.