| 190 | |
| 191 | |
| 192 | def test_simple(): |
| 193 | class Model(BaseModel): |
| 194 | a: str |
| 195 | |
| 196 | @field_validator('a') |
| 197 | @classmethod |
| 198 | def check_a(cls, v: Any): |
| 199 | if 'foobar' not in v: |
| 200 | raise ValueError('"foobar" not found in a') |
| 201 | return v |
| 202 | |
| 203 | assert Model(a='this is foobar good').a == 'this is foobar good' |
| 204 | |
| 205 | with pytest.raises(ValidationError) as exc_info: |
| 206 | Model(a='snap') |
| 207 | assert exc_info.value.errors(include_url=False) == [ |
| 208 | { |
| 209 | 'ctx': {'error': HasRepr(repr(ValueError('"foobar" not found in a')))}, |
| 210 | 'input': 'snap', |
| 211 | 'loc': ('a',), |
| 212 | 'msg': 'Value error, "foobar" not found in a', |
| 213 | 'type': 'value_error', |
| 214 | } |
| 215 | ] |
| 216 | |
| 217 | |
| 218 | def test_int_validation(): |