diff --git a/regex_patterns.go b/regex_patterns.go index 23e850f..f16f76d 100644 --- a/regex_patterns.go +++ b/regex_patterns.go @@ -23,6 +23,8 @@ const ( Date string = "^(((19|20)([2468][048]|[13579][26]|0[48])|2000)[/-]02[/-]29|((19|20)[0-9]{2}[/-](0[469]|11)[/-](0[1-9]|[12][0-9]|30)|(19|20)[0-9]{2}[/-](0[13578]|1[02])[/-](0[1-9]|[12][0-9]|3[01])|(19|20)[0-9]{2}[/-]02[/-](0[1-9]|1[0-9]|2[0-8])))$" // DateDDMMYY represents regular expression for valid date of format dd/mm/yyyy , dd-mm-yyyy etc.Ref: http://regexr.com/346hf DateDDMMYY string = "^(0?[1-9]|[12][0-9]|3[01])[\\/\\-](0?[1-9]|1[012])[\\/\\-]\\d{4}$" + // Digits represents regular epxression for validating digits + Digits string = "^[+-]?([0-9]*\\.?[0-9]+|[0-9]+\\.?[0-9]*)([eE][+-]?[0-9]+)?$" // Email represents regular expression for email Email string = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$" // Float represents regular expression for finding float number @@ -61,6 +63,7 @@ var ( regexCSSColor = regexp.MustCompile(CSSColor) regexDate = regexp.MustCompile(Date) regexDateDDMMYY = regexp.MustCompile(DateDDMMYY) + regexDigits = regexp.MustCompile(Digits) regexEmail = regexp.MustCompile(Email) regexFloat = regexp.MustCompile(Float) regexNumeric = regexp.MustCompile(Numeric) diff --git a/rules.go b/rules.go index 678bdc2..5474821 100644 --- a/rules.go +++ b/rules.go @@ -439,8 +439,18 @@ func init() { if message != "" { err = errors.New(message) } - str := toString(value) - if len(str) != l || !isNumeric(str) { + var str string + switch v := value.(type) { + case string: + str = v + case float64: + str = toString(int64(v)) + case float32: + str = toString(int64(v)) + default: + str = toString(v) + } + if len(str) != l || !regexDigits.MatchString(str) { return err } diff --git a/rules_test.go b/rules_test.go index b18749b..76d34bc 100644 --- a/rules_test.go +++ b/rules_test.go @@ -558,19 +558,37 @@ func Test_CSSColor(t *testing.T) { func Test_Digits(t *testing.T) { type user struct { - Zip string `json:"zip"` - Level string `json:"level"` + Zip string `json:"zip"` + Level string `json:"level"` + EpochInt int `json:"epoch_int"` + EpochInt64 int64 `json:"epoch_int_64"` + EpochFloat32 float32 `json:"epoch_float_32"` + EpochFloat64 float64 `json:"epoch_float_64"` + EpochString string `json:"epoch_string"` } - postUser := user{Zip: "8322", Level: "10"} + postUser := user{ + Zip: "8322", + Level: "10", + EpochInt: 1541689, + EpochInt64: 15416890380008, + EpochFloat32: 15416890380008, + EpochFloat64: 15416890380008, + EpochString: "15416890380008", + } var userObj user body, _ := json.Marshal(postUser) req, _ := http.NewRequest("POST", "http://www.example.com", bytes.NewReader(body)) rules := MapData{ - "zip": []string{"digits:5"}, - "level": []string{"digits:1"}, + "zip": []string{"digits:5"}, + "level": []string{"digits:1"}, + "epoch_int": []string{"digits:13"}, + "epoch_int_64": []string{"digits:13"}, + "epoch_float_32": []string{"digits:13"}, + "epoch_float_64": []string{"digits:13"}, + "epoch_string": []string{"digits:13"}, } opts := Options{ @@ -581,7 +599,8 @@ func Test_Digits(t *testing.T) { vd := New(opts) validationErr := vd.ValidateJSON() - if len(validationErr) != 2 { + if len(validationErr) != 7 { + t.Log(validationErr) t.Error("Digits validation failed!") } } diff --git a/validator_test.go b/validator_test.go index 6d1397f..5b6014e 100644 --- a/validator_test.go +++ b/validator_test.go @@ -40,6 +40,7 @@ func TestValidator_Validate(t *testing.T) { v := New(opts) validationError := v.Validate() if len(validationError) > 0 { + t.Log(validationError) t.Error("Validate failed to validate correct inputs!") }