Treat None as absent in model constraint validation#457
Open
Seth Fitzsimmons (mojodna) wants to merge 1 commit intodevfrom
Open
Treat None as absent in model constraint validation#457Seth Fitzsimmons (mojodna) wants to merge 1 commit intodevfrom
Seth Fitzsimmons (mojodna) wants to merge 1 commit intodevfrom
Conversation
require_any_of, require_if, and forbid_if now treat None as "not
present" for constraint purposes. Fields must be both in
model_fields_set and non-null to satisfy (or violate) a constraint.
- require_any_of: None no longer satisfies "at least one must be set"
- require_if: None no longer satisfies "must be set when condition holds"
- forbid_if: None no longer violates "must not be set when condition holds"
JSON Schema generation updated to emit {"not": {"type": "null"}}
property constraints alongside "required" assertions, via a shared
required_non_null helper in _json_schema.py. Shared predicate
_field_has_non_null_value extracted onto OptionalFieldGroupConstraint
so the check lives in one place. Also fixes a stray backtick in the
require_if error message.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
require_any_of,require_if, andforbid_ifnow treatNoneas "not present" for constraint purposes — fields must be both inmodel_fields_setand non-null to satisfy (or violate) a constraint{"not": {"type": "null"}}property constraints alongside"required"assertions, keeping Python and JSON Schema validation alignedrequired_non_nullhelper in_json_schema.pyand_field_has_non_null_valuepredicate onOptionalFieldGroupConstraintso the logic lives in one placeFixes #456
Test plan
make checkpassesrequire_any_of:TestModel(foo=None, bar=None)raisesValidationError;TestModel(foo=42, bar=None)still passesrequire_if: fields explicitly set toNoneare treated as missing when condition is trueforbid_if: fields explicitly set toNonedo not violate the prohibition{"not": {"type": "null"}}inanyOf,then, andthen.notclauses