MCPcopy
hub / github.com/pydantic/pydantic / test_annotated_validator_nested

Function test_annotated_validator_nested

tests/test_validators.py:131–155  ·  tests/test_validators.py::test_annotated_validator_nested
()

Source from the content-addressed store, hash-verified

129
130
131def test_annotated_validator_nested() -> None:
132 MyInt = Annotated[int, AfterValidator(lambda x: x if x != -1 else 0)]
133
134 def non_decreasing_list(data: list[int]) -> list[int]:
135 for prev, cur in zip(data, data[1:]):
136 assert cur >= prev
137 return data
138
139 class Model(BaseModel):
140 x: Annotated[list[MyInt], AfterValidator(non_decreasing_list)]
141
142 assert Model(x=[0, -1, 2]).x == [0, 0, 2]
143
144 with pytest.raises(ValidationError) as exc_info:
145 Model(x=[0, -1, -2])
146
147 assert exc_info.value.errors(include_url=False) == [
148 {
149 &class="cm">#x27;ctxclass="st">': {'errorclass="st">': HasRepr(repr(AssertionError('assert -2 >= 0')))},
150 &class="cm">#x27;input': [0, -1, -2],
151 &class="cm">#x27;locclass="st">': ('x',),
152 &class="cm">#x27;msgclass="st">': 'Assertion failed, assert -2 >= 0',
153 &class="cm">#x27;typeclass="st">': 'assertion_error',
154 }
155 ]
156
157
158def test_annotated_validator_runs_before_field_validators() -> None:

Callers

nothing calls this directly

Calls 3

AfterValidatorClass · 0.90
ModelClass · 0.70
errorsMethod · 0.45

Tested by

no test coverage detected