Repository navigation
fix: execute runValidationWhenNil validators on untyped nil values - #1658
Open
AdamMagued wants to merge 1 commit into
Open
AdamMagued wants to merge 1 commit into
AdamMagued wants to merge 1 commit into
Conversation
When validating untyped nil values (such as data[field] == nil in ValidateMap or Var(nil, tag)), the field kind is reflect.Invalid. Previously, traverseField prematurely created a field error and returned without checking ct.runValidationWhenNil. This prevented custom validators registered with callValidationEvenIfNull: true (and built-in cross-field validators like required_if/required_unless/excluded_if) from running when a field contained an untyped nil value. This change: - Unifies reflect.Invalid handling with nullable types so ct.runValidationWhenNil is respected. - Safely handles reflect.Invalid in getValue and typeOmitNil. - Ensures current.Type() is only called when current.IsValid() to prevent panics. - Adds regression tests for ValidateMap and Var with untyped nil values. Fixes go-playground#806
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
When validating untyped
nilvalues (such asdata[field] == nilinValidateMaporVar(nil, tag)), the field value's kind isreflect.Invalid.Previously,
traverseFieldcheckedif kind == reflect.Invalidand immediately appended a field validation error and returned without checkingct.runValidationWhenNil.This caused custom validators registered with
callValidationEvenIfNull: true(and cross-field validators withnilCheckable: truesuch asrequired_if,required_unless, andexcluded_if) to be bypassed whenever a field had an untypednilvalue, erroneously reporting validation failures.Changes
reflect.Invalidwith nullable handling intraverseField:!ct.runValidationWhenNilbefore reporting an error when kind isreflect.Invalid, allowing custom validators and cross-field validators to execute as intended.typeOmitNil, treatreflect.Invalidas nil and return early.typ = current.Type()is only executed whencurrent.IsValid(), avoiding panics.getValue(val)returnsnilwhen!val.IsValid(), avoiding panics onval.CanInterface().TestIssue806invalidator_test.goverifyingValidateMap,Var, custom failure reporting, and cross-field validation (required_if) on untypednilvalues.Fixes #806