Expanded numeric & number validations

It was originally intended for these to only be used on strings, however
it makes sense to also use them if your dealing with type interface{}

so now ints and floats return true for this validation also as they are
both numeric and numbers.

closes #356
pull/363/head v9.17.0
Dean Karn 7 years ago
parent 801c8790f3
commit ffe836d736
  1. 2
      README.md
  2. 14
      baked_in.go
  3. 1
      doc.go
  4. 6
      validator_test.go

@ -1,7 +1,7 @@
Package validator
================
<img align="right" src="https://raw.githubusercontent.com/go-playground/validator/v9/logo.png">[![Join the chat at https://gitter.im/go-playground/validator](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/go-playground/validator?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
![Project status](https://img.shields.io/badge/version-9.16.1-green.svg)
![Project status](https://img.shields.io/badge/version-9.17.0-green.svg)
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/validator/branches/v9/badge.svg)](https://semaphoreci.com/joeybloggs/validator)
[![Coverage Status](https://coveralls.io/repos/go-playground/validator/badge.svg?branch=v9&service=github)](https://coveralls.io/github/go-playground/validator?branch=v9)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/validator)](https://goreportcard.com/report/github.com/go-playground/validator)

@ -1097,12 +1097,22 @@ func isHexadecimal(fl FieldLevel) bool {
// IsNumber is the validation function for validating if the current field's value is a valid number.
func isNumber(fl FieldLevel) bool {
return numberRegex.MatchString(fl.Field().String())
switch fl.Field().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Float64:
return true
default:
return numberRegex.MatchString(fl.Field().String())
}
}
// IsNumeric is the validation function for validating if the current field's value is a valid numeric value.
func isNumeric(fl FieldLevel) bool {
return numericRegex.MatchString(fl.Field().String())
switch fl.Field().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Float64:
return true
default:
return numericRegex.MatchString(fl.Field().String())
}
}
// IsAlphanum is the validation function for validating if the current field's value is a valid alphanumeric value.

@ -538,6 +538,7 @@ Numeric
This validates that a string value contains a basic numeric value.
basic excludes exponents etc...
for integers or float it returns true.
Usage: numeric

@ -6241,8 +6241,7 @@ func TestNumber(t *testing.T) {
i := 1
errs = validate.Var(i, "number")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "number")
Equal(t, errs, nil)
}
func TestNumeric(t *testing.T) {
@ -6285,8 +6284,7 @@ func TestNumeric(t *testing.T) {
i := 1
errs = validate.Var(i, "numeric")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "numeric")
Equal(t, errs, nil)
}
func TestAlphaNumeric(t *testing.T) {

Loading…
Cancel
Save