You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: resolve panic when using aliases with OR operator (#766)
When using RegisterAlias with OR operator (|), the validator panics with
"Undefined validation function" error. This happens because aliases are
only checked when parsing comma-separated tags, not when parsing OR-
separated values.
This fix adds alias lookup in the OR parsing logic. When an alias is
found within an OR expression, it is recursively parsed and properly
inserted into the validation chain.
Fixes#766
PanicMatches(t, func() { validate.RegisterAlias("exists!", "gt=5,lt=10") }, "Alias 'exists!' either contains restricted characters or is the same as a restricted tag needed for normal operation")
697
697
}
698
698
699
+
// TestAliasWithOrOperator tests using registered aliases within OR expressions (issue #766)
700
+
funcTestAliasWithOrOperator(t*testing.T) {
701
+
validate:=New()
702
+
703
+
// Test 1: Built-in alias "iscolor" with OR - alias at end
704
+
typeTest1struct {
705
+
Fieldstring`validate:"numeric|iscolor"`
706
+
}
707
+
708
+
t1:=Test1{Field: "#fff"}
709
+
errs:=validate.Struct(t1)
710
+
Equal(t, errs, nil) // Should pass - valid hex color
711
+
712
+
t1.Field="123"
713
+
errs=validate.Struct(t1)
714
+
Equal(t, errs, nil) // Should pass - numeric
715
+
716
+
t1.Field="invalid!"
717
+
errs=validate.Struct(t1)
718
+
NotEqual(t, errs, nil) // Should fail - neither numeric nor color
719
+
720
+
// Test 2: Built-in alias with OR - alias at start
721
+
typeTest2struct {
722
+
Fieldstring`validate:"iscolor|numeric"`
723
+
}
724
+
725
+
t2:=Test2{Field: "456"}
726
+
errs=validate.Struct(t2)
727
+
Equal(t, errs, nil) // Should pass - numeric
728
+
729
+
t2.Field="rgb(255,0,0)"
730
+
errs=validate.Struct(t2)
731
+
Equal(t, errs, nil) // Should pass - valid rgb color
732
+
733
+
// Test 3: Custom alias with OR
734
+
validate.RegisterAlias("customnum", "numeric")
735
+
736
+
typeTest3struct {
737
+
Fieldstring`validate:"alpha|customnum"`
738
+
}
739
+
740
+
t3:=Test3{Field: "789"}
741
+
errs=validate.Struct(t3)
742
+
Equal(t, errs, nil) // Should pass - numeric via alias
743
+
744
+
t3.Field="abc"
745
+
errs=validate.Struct(t3)
746
+
Equal(t, errs, nil) // Should pass - alpha
747
+
748
+
// Test 4: Multiple aliases in OR
749
+
validate.RegisterAlias("customalpha", "alpha")
750
+
751
+
typeTest4struct {
752
+
Fieldstring`validate:"customnum|customalpha"`
753
+
}
754
+
755
+
t4:=Test4{Field: "xyz"}
756
+
errs=validate.Struct(t4)
757
+
Equal(t, errs, nil) // Should pass - alpha via alias
758
+
759
+
// Test 5: Three-way OR with alias in middle
760
+
typeTest5struct {
761
+
Fieldstring`validate:"alpha|customnum|email"`
762
+
}
763
+
764
+
t5:=Test5{Field: "test@example.com"}
765
+
errs=validate.Struct(t5)
766
+
Equal(t, errs, nil) // Should pass - email
767
+
768
+
// Test 6: Alias with parameter
769
+
validate.RegisterAlias("customgt5", "gt=5")
770
+
771
+
typeTest6struct {
772
+
Fieldint`validate:"eq=0|customgt5"`
773
+
}
774
+
775
+
t6:=Test6{Field: 0}
776
+
errs=validate.Struct(t6)
777
+
Equal(t, errs, nil) // Should pass - eq=0
778
+
779
+
t6.Field=10
780
+
errs=validate.Struct(t6)
781
+
Equal(t, errs, nil) // Should pass - gt=5 via alias
782
+
783
+
t6.Field=3
784
+
errs=validate.Struct(t6)
785
+
NotEqual(t, errs, nil) // Should fail - not 0 and not > 5
786
+
787
+
// Test 7: Another built-in alias with OR (country_code)
788
+
typeTest7struct {
789
+
Fieldstring`validate:"numeric|country_code"`
790
+
}
791
+
792
+
t7:=Test7{Field: "US"}
793
+
errs=validate.Struct(t7)
794
+
Equal(t, errs, nil) // Should pass - valid country code
0 commit comments