Codebase list golang-github-go-playground-validator-v10 / e10c2c9
Merge pull request #440 from RaMin0/feature/numeric_geo_coords Allow latitude/longitude validation for numeric values Dean Karn authored 5 years ago GitHub committed 5 years ago
2 changed file(s) with 53 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
308308
309309 // IsLongitude is the validation function for validating if the field's value is a valid longitude coordinate.
310310 func isLongitude(fl FieldLevel) bool {
311 return longitudeRegex.MatchString(fl.Field().String())
311 field := fl.Field()
312
313 var v string
314 switch field.Kind() {
315 case reflect.String:
316 v = field.String()
317 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
318 v = strconv.FormatInt(field.Int(), 10)
319 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
320 v = strconv.FormatUint(field.Uint(), 10)
321 case reflect.Float32:
322 v = strconv.FormatFloat(field.Float(), 'f', -1, 32)
323 case reflect.Float64:
324 v = strconv.FormatFloat(field.Float(), 'f', -1, 64)
325 default:
326 panic(fmt.Sprintf("Bad field type %T", field.Interface()))
327 }
328
329 return longitudeRegex.MatchString(v)
312330 }
313331
314332 // IsLatitude is the validation function for validating if the field's value is a valid latitude coordinate.
315333 func isLatitude(fl FieldLevel) bool {
316 return latitudeRegex.MatchString(fl.Field().String())
334 field := fl.Field()
335
336 var v string
337 switch field.Kind() {
338 case reflect.String:
339 v = field.String()
340 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
341 v = strconv.FormatInt(field.Int(), 10)
342 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
343 v = strconv.FormatUint(field.Uint(), 10)
344 case reflect.Float32:
345 v = strconv.FormatFloat(field.Float(), 'f', -1, 32)
346 case reflect.Float64:
347 v = strconv.FormatFloat(field.Float(), 'f', -1, 64)
348 default:
349 panic(fmt.Sprintf("Bad field type %T", field.Interface()))
350 }
351
352 return latitudeRegex.MatchString(v)
317353 }
318354
319355 // IsDataURI is the validation function for validating if the field's value is a valid data URI.
33343334
33353335 func TestLongitudeValidation(t *testing.T) {
33363336 tests := []struct {
3337 param string
3337 param interface{}
33383338 expected bool
33393339 }{
33403340 {"", false},
33433343 {"+73.234", true},
33443344 {"+382.3811", false},
33453345 {"23.11111111", true},
3346 {uint(180), true},
3347 {float32(-180.0), true},
3348 {-180, true},
3349 {180.1, false},
33463350 }
33473351
33483352 validate := New()
33613365 } else {
33623366 val := getError(errs, "", "")
33633367 if val.Tag() != "longitude" {
3364 t.Fatalf("Index: %d Latitude failed Error: %s", i, errs)
3368 t.Fatalf("Index: %d Longitude failed Error: %s", i, errs)
33653369 }
33663370 }
33673371 }
33683372 }
3373
3374 PanicMatches(t, func() { validate.Var(true, "longitude") }, "Bad field type bool")
33693375 }
33703376
33713377 func TestLatitudeValidation(t *testing.T) {
33723378 tests := []struct {
3373 param string
3379 param interface{}
33743380 expected bool
33753381 }{
33763382 {"", false},
33793385 {"47.1231231", true},
33803386 {"+99.9", false},
33813387 {"108", false},
3388 {uint(90), true},
3389 {float32(-90.0), true},
3390 {-90, true},
3391 {90.1, false},
33823392 }
33833393
33843394 validate := New()
34023412 }
34033413 }
34043414 }
3415
3416 PanicMatches(t, func() { validate.Var(true, "latitude") }, "Bad field type bool")
34053417 }
34063418
34073419 func TestDataURIValidation(t *testing.T) {