()
| 129 | |
| 130 | |
| 131 | def 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 | |
| 158 | def test_annotated_validator_runs_before_field_validators() -> None: |
nothing calls this directly
no test coverage detected