# pydantic_core._pydantic_core.ValidationError：User 出现 2 个验证错误
foo
  不允许额外输入 [type=extra_forbidden, input_value='bar', input_type=str]

- **ID:** `python/pydantic-extra-forbidden`
- **领域:** python
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

model_config 设置了 extra='forbid'。传入的字典包含模型未声明的键。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   class User(BaseModel):
    model_config = ConfigDict(extra='forbid')
    foo: str | None = None
   ```
2. **** (85% 成功率)
   ```
   class User(BaseModel):
    model_config = ConfigDict(extra='allow')
   ```

## 无效尝试

- **** — Silently drops fields users expect to be persisted, causing data loss bugs. (55% 失败率)
- **** — Hides schema drift; clients keep sending wrong fields and you never notice. (50% 失败率)
- **** — Pydantic v2 does not accept **kwargs in BaseModel subclasses; raises PydanticUserError. (70% 失败率)
