其他分享
首页 > 其他分享> > pydantic验证器

pydantic验证器

作者:互联网

1.装饰器实现验证器

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):
        # values:包含任何先前验证的字段的名称到值映射的字典
        if 'password1' in values and v != values['password1']:
            raise ValueError('passwords do not match')
        return v


user = UserModel(
    name='samuel colvin',
    username='scolvin',
    password1='zxcvbn',
    password2='zxcvbn',
)
print(user)
# name='Samuel Colvin' username='scolvin' password1='zxcvbn' password2='zxcvbn'

try:
    UserModel(
        name='samuel',
        username='scolvin',
        password1='zxcvbn',
        password2='zxcvbn2',
    )
except ValidationError as e:
    print(e)
    """
    2 validation errors for UserModel
    name
      must contain a space (type=value_error)
    password2
      passwords do not match (type=value_error)
    """

2.验证器的复杂校验

from typing import List
from pydantic import BaseModel, ValidationError, validator


class DemoModel(BaseModel):
    square_numbers: List[int] = []
    cube_numbers: List[int] = []

    # 1.可以使用'*'来校验所有字段
    # 2.pre=True,可以在验证器前对数据进行预处理,此处如果不处理字符串,下面的验证器在对字段值求和时就会报错
    @validator('*', pre=True)
    def split_str(cls, v):
        if isinstance(v, str):
            return v.split('|')
        return v

    # 3.没有初始化的字段,验证器不会执行
    @validator('cube_numbers', 'square_numbers')
    def check_sum(cls, v):
        if sum(v) > 42:
            raise ValueError('sum of numbers greater than 42')
        return v

    # 4.each_item=True可以实现对字段值的遍历
    @validator('square_numbers', each_item=True)
    def check_squares(cls, v):
        print(v)
        assert v >= 1, f'{v} is not a square number'
        return v


print(DemoModel(square_numbers="1|2|3"))
# print(DemoModel(square_numbers=[1, 4, 9]))

3.子类验证器和each_item

from typing import List
from pydantic import BaseModel, ValidationError, validator


class ParentModel(BaseModel):
    names: List[str]


class ChildModel(ParentModel):
    
    # 每个元素必须为空字符串
    @validator('names', each_item=True)
    def check_names_not_empty(cls, v):
        assert v != '', 'Empty strings are not allowed.'
        return v


# This will NOT raise a ValidationError because the validator was not called
try:
    child = ChildModel(names=['Alice', 'Bob', 'Eve', ''])
except ValidationError as e:
    print(e)
else:
    print('No ValidationError caught.')  

# No ValidationError caught.
class ChildModel2(ParentModel):
    @validator('names')
    def check_names_not_empty(cls, v):
        for name in v:
            assert name != '', 'Empty strings are not allowed.'
        return v


try:
    child = ChildModel2(names=['Alice', 'Bob', 'Eve', ''])
except ValidationError as e:
    print(e)
    """
    1 validation error for ChildModel2
    names
      Empty strings are not allowed. (type=assertion_error)
    """

4.始终验证

from datetime import datetime

from pydantic import BaseModel, validator


class DemoModel(BaseModel):
    ts: datetime = None

    @validator('ts', pre=True, always=True)
    def set_ts_now(cls, v):
        return v or datetime.now()


print(DemoModel())
# ts=datetime.datetime(2022, 6, 15, 18, 25, 41, 389042)

5.重用验证器

from pydantic import BaseModel, validator


def normalize(name: str) -> str:
    return ' '.join((word.capitalize()) for word in name.split(' '))


class Producer(BaseModel):
    name: str

    # 参数需要和model中需要校验的字段保持一致
    # 参数需要和验证器normalize中的形参保持一致
    # 返回值必须提供变量进行接收
    _normalize_p_name = validator('name', allow_reuse=True)(normalize)


class Consumer(BaseModel):
    name: str

    # validators
    _normalize_c_name = validator('name', allow_reuse=True)(normalize)


jane_doe = Producer(name='JaNe DOE')
print(jane_doe.name)
john_doe = Consumer(name='joHN dOe')
print(john_doe.name)
assert jane_doe.name == 'Jane Doe'
assert john_doe.name == 'John Doe'

6.根验证器

from pydantic import BaseModel, ValidationError, root_validator


class UserModel(BaseModel):
    username: str
    password1: str
    password2: str

    @root_validator(pre=True)
    def check_card_number_omitted(cls, values):
        assert 'card_number' not in values, 'card_number should not be included'
        return values

    @root_validator
    def check_passwords_match(cls, values):
        pw1, pw2 = values.get('password1'), values.get('password2')
        if pw1 is not None and pw2 is not None and pw1 != pw2:
            raise ValueError('passwords do not match')
        return values


print(UserModel(username='scolvin', password1='zxcvbn', password2='zxcvbn'))

标签:name,验证,validator,str,BaseModel,True,pydantic
来源: https://www.cnblogs.com/puffer/p/16379513.html