3

我正在尝试编写pydantic验证器,但我似乎无法理解如何使用kwargs文档中提到的参数。我想传递条件参数进行验证。这是一个玩具示例:

from pydantic import validator
import pydantic.dataclasses as pyd_dc


@pyd_dc.dataclass
class Point_t:
    x: int = 0
    y: int = 1

    @validator("y")
    def quadrant(cls, val, values, **kwargs):
        pt = x, y = values.get("x", None), val
        if x is None:
            raise ValueError(f"invalid point: {x}, {y}")

        signs = kwargs.get("signs", None)
        if signs is None:
            raise ValueError("'signs' parameter missing")

        if all(c * s >= 0 for c, s in zip(pt, signs) if s != 0):
            return val

        raise ValueError(f"{pt} not in quadrant: {signs}")

这似乎不起作用,实例化一个Point_t对象会导致验证错误:

ValidationError: 1 validation error for Point_t
y
  'signs' parameter missing (type=value_error)

如何传递signs上面示例中的参数?如果没有办法,允许有什么意义**kwargs?我错过了什么?

4

2 回答 2

5

因为Pydantic.validators是这里的类方法

允许 **kwargs 有什么意义?

由于验证器是“类方法”,这里的完整签名等于 (cls, value, *, values, config, field)

换句话说,def quadrant(..., **kwargs):等于config, field

如何将 kwargs 传递给 pydantic 验证器

1)创建自定义配置并从配置中读取

2) 字段破解

class Point_t:
    signs = dict
    x: int = 0

    def quadrant(cls, val, values, **kwargs):
        signs = values[signs]
        if signs is None:
            raise ValueError("'signs' parameter missing")

        if all(c * s >= 0 for c, s in zip(pt, signs) if s != 0):
            return val

于 2020-04-01T03:48:16.377 回答
0

Config在 pydantic 模型上解决了这个问题。这在创建可重用验证器时尤其重要。

from pydantic import validator
import pydantic.dataclasses as pyd_dc


def name_must_match(cls, v: str) -> str:
    """
    Throw an error if the provided value does not match
    the class name (`cls.Config.name`)

    Note: for this validator to work, provided pydantic model (`cls`)
    must have a Config attribute, with a `name` attribute.
    """
    if v != cls.Config.name:
        raise ValueError('value does not equal class name')
    return v

# Now to use the validator

@pyd_dc.dataclass
class Point:
    """Some pydantic model"""

    class Config:
        """Store extra info here"""
        name = 'foo'

    # define your fields
    name: str = 'foo'
    x: int = 0
    y: int = 1
    
    # Fields with underscores can be hidden from json encoder
    _name =  validator('name',
                        allow_reuse=True,
                        pre=True,
                        always=True
                        )(name_must_match)

# To test it out
>> Point(name='foo', x=1, y=2)
Point(name='foo', x=1, y=2)

>> Point(name='bar', x=3, y=4)
pydantic.error_wrappers.ValidationError: 1 validation error for Point
name
  value does not equal class name (type=value_error)
于 2021-08-24T15:06:05.080 回答