Skip to content

Fix must_match validation with Option fields - #406

Open
ychampion wants to merge 1 commit into
Keats:masterfrom
ychampion:fix/must-match-option
Open

ychampion wants to merge 1 commit into
Keats:masterfrom
ychampion:fix/must-match-option

Conversation

@ychampion

Copy link
Copy Markdown

Fix #[validate(must_match(other = "..."))] when either the validated field or other (or both) is wrapped in Option (with or without #[validate(required)]):

  • validator_derive/src/lib.rs: Pass a &T reference (field_ref) for the unwrapped field value into must_match_tokens (avoiding &&T when if_let_option_wrapper binds Some(ref field)) and look up the Option nesting count of other across all struct fields (including #[validate(skip)] fields).
  • validator_derive/src/tokens/must_match.rs: Compare field_ref against &self.<other> when other is non-optional, self.<other>.as_ref() when other is Option<T>, and self.<other>.as_ref().and_then(Option::as_ref) when other is Option<Option<T>>, while preserving value and other error parameters.
  • validator_derive_tests: Add unit and run-pass tests covering Option<T>, Option<Option<T>>, mixed optional/non-optional fields, required combinations, #[validate(skip)], and numeric Option types.

Fixes #330

Compute an unwrapped reference for the validated field (accounting for
Option<T>, Option<Option<T>>, and by-value number bindings) and match
against the Option nesting depth of `other` in `must_match_tokens`
(0, 1, or 2 Option layers, including fields marked `#[validate(skip)]`).

Fixes Keats#330
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.

required and must_match are incompatible

1 participant