-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from rezakhademix/feat-add-numeric-string-rule
feat: numeric-string rule added
- Loading branch information
Showing
4 changed files
with
117 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package govalidator | ||
|
||
import "strconv" | ||
|
||
const ( | ||
// NumericString represents rule name which will be used to find the default error message. | ||
NumericString = "numericString" | ||
// NumericStringMsg is the default error message format for fields with NumericString validation rule. | ||
NumericStringMsg = "%v must be a numeric string" | ||
|
||
// bitSize is a number from 0 to 64 | ||
bitSize = 64 | ||
// base is a number from 2 to 36 or 0 | ||
base = 10 | ||
) | ||
|
||
// NumericString validates that the field under validation is a numeric string. | ||
// | ||
// Example: | ||
// | ||
// v := validator.New() | ||
// v.NumericString("123456789", "postal_code", "postal_code must be a numeric string") | ||
// if v.IsFailed() { | ||
// fmt.Printf("validation errors: %#v\n", v.Errors()) | ||
// } | ||
func (v Validator) NumericString(s, field, msg string) Validator { | ||
_, err := strconv.ParseInt(s, base, bitSize) | ||
|
||
v.check(err == nil, field, v.msg(NumericString, msg, field)) | ||
|
||
return v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package govalidator | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_NumericString(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
field string | ||
value string | ||
isPassed bool | ||
msg string | ||
expectedMsg string | ||
}{ | ||
{ | ||
name: "test `1234` will pass validation as a numeric-string value", | ||
field: "postal_code", | ||
value: "1234", | ||
isPassed: true, | ||
msg: "", | ||
expectedMsg: "", | ||
}, | ||
{ | ||
name: "test `abc` won't pass numeric-string validation", | ||
field: "postal_code", | ||
value: "abc", | ||
isPassed: false, | ||
msg: "", | ||
expectedMsg: "postal_code must be a numeric string", | ||
}, | ||
{ | ||
name: "test ` ` won't pass numeric-string validation", | ||
field: "postal_code", | ||
value: " ", | ||
isPassed: false, | ||
msg: "postal_code must be a numeric string", | ||
expectedMsg: "postal_code must be a numeric string", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
v := New() | ||
|
||
v.NumericString(test.value, test.field, test.msg) | ||
|
||
assert.Equal(t, test.isPassed, v.IsPassed()) | ||
|
||
if !test.isPassed { | ||
assert.Equalf( | ||
t, | ||
test.expectedMsg, | ||
v.Errors()[test.field], | ||
"%q failed: expected: %s, got: %s", | ||
test.expectedMsg, | ||
v.Errors()[test.field], | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters