MCPcopy
hub / github.com/pydantic/pydantic / test_annotated_validator_wrap

Function test_annotated_validator_wrap

tests/test_validators.py:100–128  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

98
99
100def test_annotated_validator_wrap() -> None:
101 def sixties_validator(val: Any, handler: ValidatorFunctionWrapHandler, info: ValidationInfo) -> date:
102 if val == 'epoch':
103 return date.fromtimestamp(0)
104 newval = handler(val)
105 if not date.fromisoformat('1960-01-01') <= newval < date.fromisoformat('1970-01-01'):
106 raise ValueError(f'{val} is not in the sixties!')
107 return newval
108
109 SixtiesDateTime = Annotated[date, WrapValidator(sixties_validator)]
110
111 class Model(BaseModel):
112 x: SixtiesDateTime
113
114 assert Model(x='epoch').x == date.fromtimestamp(0)
115 assert Model(x='1962-01-13').x == date(year=1962, month=1, day=13)
116 assert Model(x=datetime(year=1962, month=1, day=13)).x == date(year=1962, month=1, day=13)
117
118 with pytest.raises(ValidationError) as exc_info:
119 Model(x=date(year=1970, month=4, day=17))
120 assert exc_info.value.errors(include_url=False) == [
121 {
122 'ctx': {'error': HasRepr(repr(ValueError('1970-04-17 is not in the sixties!')))},
123 'input': date(1970, 4, 17),
124 'loc': ('x',),
125 'msg': 'Value error, 1970-04-17 is not in the sixties!',
126 'type': 'value_error',
127 }
128 ]
129
130
131def test_annotated_validator_nested() -> None:

Callers

nothing calls this directly

Calls 3

WrapValidatorClass · 0.90
ModelClass · 0.70
errorsMethod · 0.45

Tested by

no test coverage detected