Use type switch in favor of fmt.Sprint

pull/440/head
Ramy Aboul Naga 6 years ago
parent 52ea448998
commit cbd5cf6393
  1. 40
      baked_in.go
  2. 10
      validator_test.go

@ -304,12 +304,48 @@ func isSSN(fl FieldLevel) bool {
// IsLongitude is the validation function for validating if the field's value is a valid longitude coordinate. // IsLongitude is the validation function for validating if the field's value is a valid longitude coordinate.
func isLongitude(fl FieldLevel) bool { func isLongitude(fl FieldLevel) bool {
return longitudeRegex.MatchString(fmt.Sprint(fl.Field().Interface())) field := fl.Field()
var v string
switch field.Kind() {
case reflect.String:
v = field.String()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v = strconv.FormatInt(field.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v = strconv.FormatUint(field.Uint(), 10)
case reflect.Float32:
v = strconv.FormatFloat(field.Float(), 'f', -1, 32)
case reflect.Float64:
v = strconv.FormatFloat(field.Float(), 'f', -1, 64)
default:
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
}
return longitudeRegex.MatchString(v)
} }
// IsLatitude is the validation function for validating if the field's value is a valid latitude coordinate. // IsLatitude is the validation function for validating if the field's value is a valid latitude coordinate.
func isLatitude(fl FieldLevel) bool { func isLatitude(fl FieldLevel) bool {
return latitudeRegex.MatchString(fmt.Sprint(fl.Field().Interface())) field := fl.Field()
var v string
switch field.Kind() {
case reflect.String:
v = field.String()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v = strconv.FormatInt(field.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v = strconv.FormatUint(field.Uint(), 10)
case reflect.Float32:
v = strconv.FormatFloat(field.Float(), 'f', -1, 32)
case reflect.Float64:
v = strconv.FormatFloat(field.Float(), 'f', -1, 64)
default:
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
}
return latitudeRegex.MatchString(v)
} }
// IsDataURI is the validation function for validating if the field's value is a valid data URI. // IsDataURI is the validation function for validating if the field's value is a valid data URI.

@ -3344,6 +3344,8 @@ func TestLongitudeValidation(t *testing.T) {
{"+73.234", true}, {"+73.234", true},
{"+382.3811", false}, {"+382.3811", false},
{"23.11111111", true}, {"23.11111111", true},
{uint(180), true},
{float32(-180.0), true},
{-180, true}, {-180, true},
{180.1, false}, {180.1, false},
} }
@ -3369,6 +3371,8 @@ func TestLongitudeValidation(t *testing.T) {
} }
} }
} }
PanicMatches(t, func() { validate.Var(true, "longitude") }, "Bad field type bool")
} }
func TestLatitudeValidation(t *testing.T) { func TestLatitudeValidation(t *testing.T) {
@ -3382,8 +3386,10 @@ func TestLatitudeValidation(t *testing.T) {
{"47.1231231", true}, {"47.1231231", true},
{"+99.9", false}, {"+99.9", false},
{"108", false}, {"108", false},
{uint(90), true},
{float32(-90.0), true},
{-90, true}, {-90, true},
{90, true}, {90.1, false},
} }
validate := New() validate := New()
@ -3407,6 +3413,8 @@ func TestLatitudeValidation(t *testing.T) {
} }
} }
} }
PanicMatches(t, func() { validate.Var(true, "latitude") }, "Bad field type bool")
} }
func TestDataURIValidation(t *testing.T) { func TestDataURIValidation(t *testing.T) {

Loading…
Cancel
Save