Test validation of implicit and explicit None values. # For consistency with pydantic, validators are not to be called on # arguments that are not explicitly provided. https://github.com/tiangolo/sqlmodel/issues/230 https://github.com/samuelcolvin/pydantic/issues/1223
(clear_sqlmodel)
| 4 | |
| 5 | |
| 6 | def test_validation_pydantic_v2(clear_sqlmodel): |
| 7 | """Test validation of implicit and explicit None values. |
| 8 | |
| 9 | # For consistency with pydantic, validators are not to be called on |
| 10 | # arguments that are not explicitly provided. |
| 11 | |
| 12 | https://github.com/tiangolo/sqlmodel/issues/230 |
| 13 | https://github.com/samuelcolvin/pydantic/issues/1223 |
| 14 | |
| 15 | """ |
| 16 | from pydantic import field_validator |
| 17 | |
| 18 | class Hero(SQLModel): |
| 19 | name: str | None = None |
| 20 | secret_name: str | None = None |
| 21 | age: int | None = None |
| 22 | |
| 23 | @field_validator("name", "secret_name", "age") |
| 24 | def reject_none(cls, v): |
| 25 | assert v is not None |
| 26 | return v |
| 27 | |
| 28 | Hero.model_validate({"age": 25}) |
| 29 | |
| 30 | with pytest.raises(ValidationError): |
| 31 | Hero.model_validate({"name": None, "age": 25}) |
nothing calls this directly
no test coverage detected
searching dependent graphs…