Skip to content

Treat None as absent in model constraint validation#457

Open
Seth Fitzsimmons (mojodna) wants to merge 1 commit intodevfrom
fix-require_any_of_with_none
Open

Treat None as absent in model constraint validation#457
Seth Fitzsimmons (mojodna) wants to merge 1 commit intodevfrom
fix-require_any_of_with_none

Conversation

@mojodna
Copy link
Collaborator

@mojodna Seth Fitzsimmons (mojodna) commented Mar 3, 2026

Summary

  • 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
  • JSON Schema generation emits {"not": {"type": "null"}} property constraints alongside "required" assertions, keeping Python and JSON Schema validation aligned
  • Shared required_non_null helper in _json_schema.py and _field_has_non_null_value predicate on OptionalFieldGroupConstraint so the logic lives in one place

Fixes #456

Test plan

  • make check passes
  • require_any_of: TestModel(foo=None, bar=None) raises ValidationError; TestModel(foo=42, bar=None) still passes
  • require_if: fields explicitly set to None are treated as missing when condition is true
  • forbid_if: fields explicitly set to None do not violate the prohibition
  • JSON Schema output includes {"not": {"type": "null"}} in anyOf, then, and then.not clauses
  • Baseline schemas for transportation and divisions themes regenerated

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

require_any_of and related model constraints accept explicitly-set None as satisfying the requirement

1 participant