mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
Support assert statements inside validators (#653)
* Support assert statements inside validators * Add a validator example that uses assert * Add warning about consequences of using -O optimization flag * Fix a typo * Fix incomplete validator * Extend exception name generation * Improve tests * Clarify pytest behaviour * handle assertion error name, fix build * Address feedback * docs cleanup * Incorporate feedback * fix quotes
This commit is contained in:
committed by
Samuel Colvin
parent
321cde0c88
commit
f41d5dca3c
@@ -3,6 +3,7 @@ from pydantic import BaseModel, ValidationError, validator
|
||||
|
||||
class UserModel(BaseModel):
|
||||
name: str
|
||||
username: str
|
||||
password1: str
|
||||
password2: str
|
||||
|
||||
@@ -18,6 +19,11 @@ class UserModel(BaseModel):
|
||||
raise ValueError('passwords do not match')
|
||||
return v
|
||||
|
||||
@validator('username')
|
||||
def username_alphanumeric(cls, v):
|
||||
assert v.isalpha(), 'must be alphanumeric'
|
||||
return v
|
||||
|
||||
|
||||
print(UserModel(name='samuel colvin', password1='zxcvbn', password2='zxcvbn'))
|
||||
# > UserModel name='Samuel Colvin' password1='zxcvbn' password2='zxcvbn'
|
||||
|
||||
Reference in New Issue
Block a user