mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
3f53cb5980
* Update documentation (#162) * More docs about error handling
30 lines
450 B
Python
30 lines
450 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 a "bar"')
|
|
|
|
return v
|
|
|
|
|
|
try:
|
|
Model(foo='ber')
|
|
except ValidationError as e:
|
|
print(e.json())
|
|
"""
|
|
[
|
|
{
|
|
"loc": [
|
|
"foo"
|
|
],
|
|
"msg": "value must be a \"bar\"",
|
|
"type": "value_error"
|
|
}
|
|
]
|
|
"""
|