Skip to content

Conversation

@shindonghwi
Copy link

Summary

When using validators like required_if, required_unless, excluded_if, or excluded_unless with ValidateMap(), the validator panics with "Invalid field namespace". This happens because getStructFieldOKInternal can't navigate primitive types to find referenced fields.

Changes

  • Modified getStructFieldOKInternal in util.go to return found=false instead of panicking when encountering types it can't navigate
  • Updated existing test in TestCrossNamespaceFieldValidation to verify the new behavior
  • Added TestValidateMapWithCrossFieldValidators regression test covering all cross-field validators

How it works

Cross-field validators call GetStructFieldOKAdvanced2 to find referenced fields. When the field isn't found, they use their defaultNotFoundValue parameter to determine the result:

  • required_if: condition not met → field not required
  • required_unless: condition not met → field required
  • excluded_if: condition not met → field not excluded
  • excluded_unless: condition not met → field must be excluded

Test plan

  • All existing tests pass
  • New regression test covers ValidateMap with all cross-field validators
  • Verified original panic scenario no longer occurs

Fixes #893

@shindonghwi shindonghwi requested a review from a team as a code owner December 18, 2025 05:12
@coveralls
Copy link

coveralls commented Dec 18, 2025

Coverage Status

coverage: 73.765% (+0.02%) from 73.749%
when pulling 787fdcf on shindonghwi:fix/validatemap-cross-field-panic-893
into 79fba72 on go-playground:master.

When using validators like required_if, required_unless, excluded_if,
or excluded_unless with ValidateMap(), the validator panicked with
"Invalid field namespace" because getStructFieldOKInternal couldn't
navigate primitive types to find referenced fields.

This change makes getStructFieldOKInternal return found=false instead
of panicking when it encounters types it can't navigate. This allows
cross-field validators to handle the "not found" case gracefully using
their defaultNotFoundValue parameter.

Fixes go-playground#893
@shindonghwi shindonghwi force-pushed the fix/validatemap-cross-field-panic-893 branch from d57102d to 787fdcf Compare December 18, 2025 05:22
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_if panics with ValidateMap on simple map with a single rule

2 participants