mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
043d16bcf8
* Update validator function name in examples to show that value must be bar vs must contain a space * Add md file in
19 lines
331 B
Python
19 lines
331 B
Python
from pydantic import BaseModel, ValidationError, validator
|
|
|
|
|
|
class Model(BaseModel):
|
|
foo: str
|
|
|
|
@validator('foo')
|
|
def value_must_equal_bar(cls, v):
|
|
if v != 'bar':
|
|
raise ValueError('value must be "bar"')
|
|
|
|
return v
|
|
|
|
|
|
try:
|
|
Model(foo='ber')
|
|
except ValidationError as e:
|
|
print(e.errors())
|