mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
45 lines
986 B
Python
45 lines
986 B
Python
from pydantic import BaseModel, ValidationError, validator
|
|
|
|
|
|
class UserModel(BaseModel):
|
|
name: str
|
|
username: str
|
|
password1: str
|
|
password2: str
|
|
|
|
@validator('name')
|
|
def name_must_contain_space(cls, v):
|
|
if ' ' not in v:
|
|
raise ValueError('must contain a space')
|
|
return v.title()
|
|
|
|
@validator('password2')
|
|
def passwords_match(cls, v, values, **kwargs):
|
|
if 'password1' in values and v != values['password1']:
|
|
raise ValueError('passwords do not match')
|
|
return v
|
|
|
|
@validator('username')
|
|
def username_alphanumeric(cls, v):
|
|
assert v.isalnum(), 'must be alphanumeric'
|
|
return v
|
|
|
|
|
|
user = UserModel(
|
|
name='samuel colvin',
|
|
username='scolvin',
|
|
password1='zxcvbn',
|
|
password2='zxcvbn',
|
|
)
|
|
print(user)
|
|
|
|
try:
|
|
UserModel(
|
|
name='samuel',
|
|
username='scolvin',
|
|
password1='zxcvbn',
|
|
password2='zxcvbn2',
|
|
)
|
|
except ValidationError as e:
|
|
print(e)
|