Skip to content

Commit

Permalink
Merge pull request go-playground#440 from RaMin0/feature/numeric_geo_…
Browse files Browse the repository at this point in the history
…coords

Allow latitude/longitude validation for numeric values
  • Loading branch information
fairyhunter13 committed Feb 12, 2019
2 parents dc0e158 + 0ce9b19 commit 130bd09
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
40 changes: 38 additions & 2 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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.
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.
Expand Down
18 changes: 15 additions & 3 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3335,7 +3335,7 @@ func TestSSNValidation(t *testing.T) {

func TestLongitudeValidation(t *testing.T) {
tests := []struct {
param string
param interface{}
expected bool
}{
{"", false},
Expand All @@ -3344,6 +3344,10 @@ func TestLongitudeValidation(t *testing.T) {
{"+73.234", true},
{"+382.3811", false},
{"23.11111111", true},
{uint(180), true},
{float32(-180.0), true},
{-180, true},
{180.1, false},
}

validate := New()
Expand All @@ -3362,16 +3366,18 @@ func TestLongitudeValidation(t *testing.T) {
} else {
val := getError(errs, "", "")
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) {
tests := []struct {
param string
param interface{}
expected bool
}{
{"", false},
Expand All @@ -3380,6 +3386,10 @@ func TestLatitudeValidation(t *testing.T) {
{"47.1231231", true},
{"+99.9", false},
{"108", false},
{uint(90), true},
{float32(-90.0), true},
{-90, true},
{90.1, false},
}

validate := New()
Expand All @@ -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) {
Expand Down

0 comments on commit 130bd09

Please sign in to comment.