Skip to content

fix: migrate 5 single-field validators to Pydantic V2 - #309

Open
dulcetberg wants to merge 1 commit into
hotosm:developfrom
dulcetberg:fix/pydantic-v2-remaining-validators
Open

fix: migrate 5 single-field validators to Pydantic V2#309
dulcetberg wants to merge 1 commit into
hotosm:developfrom
dulcetberg:fix/pydantic-v2-remaining-validators

Conversation

@dulcetberg

Copy link
Copy Markdown

Summary

Third incremental step on #256 (see #307, #308) — migrates the 5 remaining @validator usages that validate a single field with no cross-field access, to V2's @field_validator:

  • RawDataCurrentParamsBase.return_unique_value (geometry_type)
  • HDXModel.validate_tags (tags)
  • CategoryModel.validate_types (types)
  • CategoryModel.validate_export_types (formats)
  • DatasetConfig.validate_frequency (update_frequency)

allow_reuse=True is dropped where present — verified V2's field_validator rejects it outright as an invalid kwarg, since V2 removed the duplicate-validator-name restriction it existed to work around.

Branched independently from develop, same as #308.

Not included here, on purpose: the remaining 3 @validator usages (lines 235, 332, 673 on develop) all read another field's value via a values parameter — real cross-field validation. That needs a materially different conversion (field_validator(mode="before") + ValidationInfo.data instead of a values dict, plus validate_default=True on the affected Field to replicate always=True) that I verified separately and want to give its own focused PR rather than mixing complexity levels here.

Test plan

  • pytest tests/test_app.py — 5 passed, 0 failed
  • Deprecation warning count dropped by exactly 5, matching the 5 validators converted
  • Confirmed allow_reuse=True is rejected (not silently ignored) by V2's field_validator, so dropping it is the correct fix rather than a workaround

Migrates the remaining single-parameter @validator usages (no
cross-field access via `values`) to V2's @field_validator API:

- RawDataCurrentParamsBase.return_unique_value (geometry_type)
- HDXModel.validate_tags (tags)
- CategoryModel.validate_types (types)
- CategoryModel.validate_export_types (formats)
- DatasetConfig.validate_frequency (update_frequency)

allow_reuse=True is dropped where present - V2's field_validator
rejects it outright as an invalid kwarg (verified), since V2 removed
the duplicate-validator-name restriction it existed to work around.

Validator logic is unchanged in all 5 cases. Verified: test suite
still passes (5 passed), and the deprecation warning count drops by
exactly 5, matching the 5 validators converted.

Third incremental PR toward hotosm#256 (see hotosm#307, hotosm#308). The remaining 3
@validator usages in this file (lines 235, 332, 673 on develop) all
perform cross-field validation via a `values` parameter, which needs
a different, more involved conversion (field_validator mode="before"
+ ValidationInfo.data, plus validate_default=True on the affected
Field) - planned as a separate follow-up PR rather than folding in
here, since the two patterns aren't equivalent in complexity.

Signed-off-by: Brian Bergstrom <dulcetberg@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant