mirror of
https://github.com/Fishwaldo/validator.git
synced 2025-03-15 11:41:32 +00:00
Fixed country_code validation to properly handle strings (#873)
This commit is contained in:
parent
ce34f361cc
commit
19f8e61dba
2 changed files with 45 additions and 3 deletions
|
@ -2351,6 +2351,12 @@ func isIso3166AlphaNumeric(fl FieldLevel) bool {
|
|||
|
||||
var code int
|
||||
switch field.Kind() {
|
||||
case reflect.String:
|
||||
i, err := strconv.Atoi(field.String())
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
code = i % 1000
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
code = int(field.Int() % 1000)
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
|
|
|
@ -11053,12 +11053,15 @@ func TestIsIso3166Alpha3Validation(t *testing.T) {
|
|||
|
||||
func TestIsIso3166AlphaNumericValidation(t *testing.T) {
|
||||
tests := []struct {
|
||||
value int
|
||||
value interface{}
|
||||
expected bool
|
||||
}{
|
||||
{248, true},
|
||||
{"248", true},
|
||||
{0, false},
|
||||
{1, false},
|
||||
{"1", false},
|
||||
{"invalid_int", false},
|
||||
}
|
||||
|
||||
validate := New()
|
||||
|
@ -11079,8 +11082,41 @@ func TestIsIso3166AlphaNumericValidation(t *testing.T) {
|
|||
}
|
||||
|
||||
PanicMatches(t, func() {
|
||||
_ = validate.Var("1", "iso3166_1_alpha_numeric")
|
||||
}, "Bad field type string")
|
||||
_ = validate.Var([]string{"1"}, "iso3166_1_alpha_numeric")
|
||||
}, "Bad field type []string")
|
||||
}
|
||||
|
||||
func TestCountryCodeValidation(t *testing.T) {
|
||||
tests := []struct {
|
||||
value interface{}
|
||||
expected bool
|
||||
}{
|
||||
{248, true},
|
||||
{0, false},
|
||||
{1, false},
|
||||
{"POL", true},
|
||||
{"NO", true},
|
||||
{"248", true},
|
||||
{"1", false},
|
||||
{"0", false},
|
||||
}
|
||||
|
||||
validate := New()
|
||||
|
||||
for i, test := range tests {
|
||||
|
||||
errs := validate.Var(test.value, "country_code")
|
||||
|
||||
if test.expected {
|
||||
if !IsEqual(errs, nil) {
|
||||
t.Fatalf("Index: %d country_code failed Error: %s", i, errs)
|
||||
}
|
||||
} else {
|
||||
if IsEqual(errs, nil) {
|
||||
t.Fatalf("Index: %d country_code failed Error: %s", i, errs)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsIso4217Validation(t *testing.T) {
|
||||
|
|
Loading…
Add table
Reference in a new issue