Skip to content

Commit 4bee3f9

Browse files
authored
Fix issue #48 (#50)
Fixed digits:int goes scientific at 13 digits
1 parent 69b7cbf commit 4bee3f9

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

regex_patterns.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const (
2323
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])))$"
2424
// DateDDMMYY represents regular expression for valid date of format dd/mm/yyyy , dd-mm-yyyy etc.Ref: http://regexr.com/346hf
2525
DateDDMMYY string = "^(0?[1-9]|[12][0-9]|3[01])[\\/\\-](0?[1-9]|1[012])[\\/\\-]\\d{4}$"
26+
// Digits represents regular epxression for validating digits
27+
Digits string = "^[+-]?([0-9]*\\.?[0-9]+|[0-9]+\\.?[0-9]*)([eE][+-]?[0-9]+)?$"
2628
// Email represents regular expression for email
2729
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])?)+$"
2830
// Float represents regular expression for finding float number
@@ -61,6 +63,7 @@ var (
6163
regexCSSColor = regexp.MustCompile(CSSColor)
6264
regexDate = regexp.MustCompile(Date)
6365
regexDateDDMMYY = regexp.MustCompile(DateDDMMYY)
66+
regexDigits = regexp.MustCompile(Digits)
6467
regexEmail = regexp.MustCompile(Email)
6568
regexFloat = regexp.MustCompile(Float)
6669
regexNumeric = regexp.MustCompile(Numeric)

rules.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,18 @@ func init() {
439439
if message != "" {
440440
err = errors.New(message)
441441
}
442-
str := toString(value)
443-
if len(str) != l || !isNumeric(str) {
442+
var str string
443+
switch v := value.(type) {
444+
case string:
445+
str = v
446+
case float64:
447+
str = toString(int64(v))
448+
case float32:
449+
str = toString(int64(v))
450+
default:
451+
str = toString(v)
452+
}
453+
if len(str) != l || !regexDigits.MatchString(str) {
444454
return err
445455
}
446456

rules_test.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -558,19 +558,37 @@ func Test_CSSColor(t *testing.T) {
558558

559559
func Test_Digits(t *testing.T) {
560560
type user struct {
561-
Zip string `json:"zip"`
562-
Level string `json:"level"`
561+
Zip string `json:"zip"`
562+
Level string `json:"level"`
563+
EpochInt int `json:"epoch_int"`
564+
EpochInt64 int64 `json:"epoch_int_64"`
565+
EpochFloat32 float32 `json:"epoch_float_32"`
566+
EpochFloat64 float64 `json:"epoch_float_64"`
567+
EpochString string `json:"epoch_string"`
563568
}
564569

565-
postUser := user{Zip: "8322", Level: "10"}
570+
postUser := user{
571+
Zip: "8322",
572+
Level: "10",
573+
EpochInt: 1541689,
574+
EpochInt64: 15416890380008,
575+
EpochFloat32: 15416890380008,
576+
EpochFloat64: 15416890380008,
577+
EpochString: "15416890380008",
578+
}
566579
var userObj user
567580

568581
body, _ := json.Marshal(postUser)
569582
req, _ := http.NewRequest("POST", "http://www.example.com", bytes.NewReader(body))
570583

571584
rules := MapData{
572-
"zip": []string{"digits:5"},
573-
"level": []string{"digits:1"},
585+
"zip": []string{"digits:5"},
586+
"level": []string{"digits:1"},
587+
"epoch_int": []string{"digits:13"},
588+
"epoch_int_64": []string{"digits:13"},
589+
"epoch_float_32": []string{"digits:13"},
590+
"epoch_float_64": []string{"digits:13"},
591+
"epoch_string": []string{"digits:13"},
574592
}
575593

576594
opts := Options{
@@ -581,7 +599,8 @@ func Test_Digits(t *testing.T) {
581599

582600
vd := New(opts)
583601
validationErr := vd.ValidateJSON()
584-
if len(validationErr) != 2 {
602+
if len(validationErr) != 7 {
603+
t.Log(validationErr)
585604
t.Error("Digits validation failed!")
586605
}
587606
}

validator_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func TestValidator_Validate(t *testing.T) {
4040
v := New(opts)
4141
validationError := v.Validate()
4242
if len(validationError) > 0 {
43+
t.Log(validationError)
4344
t.Error("Validate failed to validate correct inputs!")
4445
}
4546

0 commit comments

Comments
 (0)