Skip to content

Commit ba73c96

Browse files
committed
Fix: update validation functions to return false for invalid field types instead of panicking
1 parent 4ca2bfb commit ba73c96

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

baked_in.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ func isPrintableASCII(fl FieldLevel) bool {
538538
if field.Kind() == reflect.String {
539539
return printableASCIIRegex().MatchString(field.String())
540540
}
541-
panic(fmt.Sprintf("Bad field type %s", field.Type()))
541+
return false
542542
}
543543

544544
// isASCII is the validation function for validating if the field's value is a valid ASCII character.
@@ -547,7 +547,7 @@ func isASCII(fl FieldLevel) bool {
547547
if field.Kind() == reflect.String {
548548
return aSCIIRegex().MatchString(field.String())
549549
}
550-
panic(fmt.Sprintf("Bad field type %s", field.Type()))
550+
return false
551551
}
552552

553553
// isUUID5 is the validation function for validating if the field's value is a valid v5 UUID.

validator_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3904,7 +3904,7 @@ func TestMultibyteValidation(t *testing.T) {
39043904

39053905
func TestPrintableASCIIValidation(t *testing.T) {
39063906
tests := []struct {
3907-
param string
3907+
param interface{}
39083908
expected bool
39093909
}{
39103910
{"", true},
@@ -3918,6 +3918,8 @@ func TestPrintableASCIIValidation(t *testing.T) {
39183918
{"1234abcDEF", true},
39193919
{"newline\n", false},
39203920
{"\x19test\x7F", false},
3921+
{[]int{3000}, false},
3922+
{1, false},
39213923
}
39223924

39233925
validate := New()
@@ -3940,13 +3942,11 @@ func TestPrintableASCIIValidation(t *testing.T) {
39403942
}
39413943
}
39423944
}
3943-
PanicMatches(t, func() { _ = validate.Var([]int{3000}, "printascii") }, "Bad field type []int")
3944-
PanicMatches(t, func() { _ = validate.Var(1, "printascii") }, "Bad field type int")
39453945
}
39463946

39473947
func TestASCIIValidation(t *testing.T) {
39483948
tests := []struct {
3949-
param string
3949+
param interface{}
39503950
expected bool
39513951
}{
39523952
{"", true},
@@ -3959,6 +3959,8 @@ func TestASCIIValidation(t *testing.T) {
39593959
{"[email protected]", true},
39603960
{"1234abcDEF", true},
39613961
{"", true},
3962+
{[]int{3000}, false},
3963+
{1, false},
39623964
}
39633965

39643966
validate := New()
@@ -3981,8 +3983,6 @@ func TestASCIIValidation(t *testing.T) {
39813983
}
39823984
}
39833985
}
3984-
PanicMatches(t, func() { _ = validate.Var([]int{3000}, "ascii") }, "Bad field type []int")
3985-
PanicMatches(t, func() { _ = validate.Var(1, "ascii") }, "Bad field type int")
39863986
}
39873987

39883988
func TestUUID5Validation(t *testing.T) {

0 commit comments

Comments
 (0)