Merge pull request #440 from RaMin0/feature/numeric_geo_coords

Allow latitude/longitude validation for numeric values
pull/451/head
Dean Karn 6 years ago committed by GitHub
commit e10c2c9ddb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 40
      baked_in.go
  2. 18
      validator_test.go

@ -309,12 +309,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(fl.Field().String()) 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(fl.Field().String()) 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.

@ -3335,7 +3335,7 @@ func TestSSNValidation(t *testing.T) {
func TestLongitudeValidation(t *testing.T) { func TestLongitudeValidation(t *testing.T) {
tests := []struct { tests := []struct {
param string param interface{}
expected bool expected bool
}{ }{
{"", false}, {"", false},
@ -3344,6 +3344,10 @@ 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.1, false},
} }
validate := New() validate := New()
@ -3362,16 +3366,18 @@ func TestLongitudeValidation(t *testing.T) {
} else { } else {
val := getError(errs, "", "") val := getError(errs, "", "")
if val.Tag() != "longitude" { if val.Tag() != "longitude" {
t.Fatalf("Index: %d Latitude failed Error: %s", i, errs) t.Fatalf("Index: %d Longitude failed Error: %s", i, errs)
} }
} }
} }
} }
PanicMatches(t, func() { validate.Var(true, "longitude") }, "Bad field type bool")
} }
func TestLatitudeValidation(t *testing.T) { func TestLatitudeValidation(t *testing.T) {
tests := []struct { tests := []struct {
param string param interface{}
expected bool expected bool
}{ }{
{"", false}, {"", false},
@ -3380,6 +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.1, false},
} }
validate := New() validate := New()
@ -3403,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