mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
f0f9de5f96
* improve docs on error handling * change ValidationError signature * cleanup * rename _raw_errors > raw_errors * improve _display_error_type_and_ctx
26 lines
432 B
Python
26 lines
432 B
Python
from pydantic import BaseModel, ValidationError, validator
|
|
|
|
class Model(BaseModel):
|
|
foo: str
|
|
|
|
@validator('foo')
|
|
def name_must_contain_space(cls, v):
|
|
if v != 'bar':
|
|
raise ValueError('value must be "bar"')
|
|
|
|
return v
|
|
|
|
try:
|
|
Model(foo='ber')
|
|
except ValidationError as e:
|
|
print(e.json())
|
|
"""
|
|
[
|
|
{
|
|
"loc": ["foo"],
|
|
"msg": "value must be \"bar\"",
|
|
"type": "value_error"
|
|
}
|
|
]
|
|
"""
|