Skip to content

Commit

Permalink
Fix issue #48 (#50)
Browse files Browse the repository at this point in the history
Fixed digits:int goes scientific at 13 digits
  • Loading branch information
thedevsaddam authored Nov 22, 2018
1 parent 69b7cbf commit 4bee3f9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
3 changes: 3 additions & 0 deletions regex_patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 12 additions & 2 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
31 changes: 25 additions & 6 deletions rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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!")
}
}
Expand Down
1 change: 1 addition & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
}

Expand Down

0 comments on commit 4bee3f9

Please sign in to comment.