# pydantic_core._pydantic_core.ValidationError: Endpoint 模型验证错误：URL scheme 必须是 'http' 或 'https'

- **ID:** `python/pydantic-validation-error-url-scheme`
- **领域:** python
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

AnyHttpUrl / HttpUrl 类型拒绝非 http(s) 协议。传入 'ftp://'、'file://' 或 'ws://' 会触发 url_scheme 错误。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 2.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   from pydantic import BaseModel, AnyUrl

class Endpoint(BaseModel):
    url: AnyUrl
   ```
2. **** (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
   ```

## 无效尝试

- **** — Produces invalid URLs like 'http://ftp://example.com' which fail further validation. (90% 失败率)
- **** — Loses URL validation entirely; invalid URLs slip through and break HTTP clients downstream. (70% 失败率)
