Skip to content

Commit 80b6d11

Browse files
authored
Fix/1481 skip invalid type validations (#1498)
#1481 Only strings can pass validations for strings other types return error in 'ascii' & 'printascii' validations.
1 parent a0faae9 commit 80b6d11

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

baked_in.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,12 +534,20 @@ func hasMultiByteCharacter(fl FieldLevel) bool {
534534

535535
// isPrintableASCII is the validation function for validating if the field's value is a valid printable ASCII character.
536536
func isPrintableASCII(fl FieldLevel) bool {
537-
return printableASCIIRegex().MatchString(fl.Field().String())
537+
field := fl.Field()
538+
if field.Kind() == reflect.String {
539+
return printableASCIIRegex().MatchString(field.String())
540+
}
541+
return false
538542
}
539543

540544
// isASCII is the validation function for validating if the field's value is a valid ASCII character.
541545
func isASCII(fl FieldLevel) bool {
542-
return aSCIIRegex().MatchString(fl.Field().String())
546+
field := fl.Field()
547+
if field.Kind() == reflect.String {
548+
return aSCIIRegex().MatchString(field.String())
549+
}
550+
return false
543551
}
544552

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

validator_test.go

Lines changed: 6 additions & 2 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()
@@ -3944,7 +3946,7 @@ func TestPrintableASCIIValidation(t *testing.T) {
39443946

39453947
func TestASCIIValidation(t *testing.T) {
39463948
tests := []struct {
3947-
param string
3949+
param interface{}
39483950
expected bool
39493951
}{
39503952
{"", true},
@@ -3957,6 +3959,8 @@ func TestASCIIValidation(t *testing.T) {
39573959
{"[email protected]", true},
39583960
{"1234abcDEF", true},
39593961
{"", true},
3962+
{[]int{3000}, false},
3963+
{1, false},
39603964
}
39613965

39623966
validate := New()

0 commit comments

Comments
 (0)