mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
f46dc0c56f
* timedelta json encoding, altnative to #220 * history and tests * fix comments and tests * docs for json_encoders * tests plain timedelta object parsing * uprev * tweak docs
27 lines
452 B
Python
27 lines
452 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.errors())
|
|
|
|
"""
|
|
[
|
|
{
|
|
'loc': ('foo',),
|
|
'msg': 'value must be "bar"',
|
|
'type': 'value_error',
|
|
},
|
|
]
|
|
"""
|