mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
Better validators (#97)
* working on improved validators * full tests for validators * tweask * tweaking fields.py * adding docs * add history * fix classmethod validators
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import json
|
||||
from typing import List, Set
|
||||
|
||||
from pydantic import BaseModel, ValidationError, validator
|
||||
|
||||
|
||||
class DemoModel(BaseModel):
|
||||
numbers: List[int] = []
|
||||
people: List[str] = []
|
||||
|
||||
@validator('people', 'numbers', pre=True, whole=True)
|
||||
def json_decode(cls, v):
|
||||
if isinstance(v, str):
|
||||
try:
|
||||
return json.loads(v)
|
||||
except ValueError:
|
||||
pass
|
||||
return v
|
||||
|
||||
@validator('numbers')
|
||||
def check_numbers_low(cls, v):
|
||||
if v > 4:
|
||||
raise ValueError(f'number to large {v} > 4')
|
||||
return v
|
||||
|
||||
@validator('numbers', whole=True)
|
||||
def check_sum_numbers_low(cls, v):
|
||||
if sum(v) > 8:
|
||||
raise ValueError(f'sum of numbers greater than 8')
|
||||
return v
|
||||
|
||||
|
||||
print(DemoModel(numbers='[1, 1, 2, 2]'))
|
||||
# > DemoModel numbers=[1, 1, 2, 2] people=[]
|
||||
|
||||
try:
|
||||
DemoModel(numbers='[1, 2, 5]')
|
||||
except ValidationError as e:
|
||||
print(e)
|
||||
"""
|
||||
error validating input
|
||||
numbers:
|
||||
number to large 5 > 4 (error_type=ValueError track=int index=2)
|
||||
"""
|
||||
|
||||
try:
|
||||
DemoModel(numbers=[3, 3, 3])
|
||||
except ValidationError as e:
|
||||
print(e)
|
||||
"""
|
||||
error validating input
|
||||
numbers:
|
||||
sum of numbers greater than 8 (error_type=ValueError track=int)
|
||||
"""
|
||||
Reference in New Issue
Block a user