I'm trying to write pydantic validators, but I can't seem to understand how to make use of the kwargs argument that is mentioned in the docs. I would like to pass conditional parameters for validation. Here's a toy example:
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}")
This doesn't seem to work, instantiating a Point_t object leads to a validation error:
ValidationError: 1 validation error for Point_t
y
'signs' parameter missing (type=value_error)
How do I pass on the signs parameter in the above example? If there's no way, what's the point of allowing for **kwargs? What am I missing?