Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: numeric-string rule added #107

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Each validation rule in GoValidator has it's own default message, e.g: `required
---

| Method | Description |
|------------------|---------------------------------------------------------------------------------------------------------------------|
| ---------------- | ------------------------------------------------------------------------------------------------------------------- |
| RequiredInt | `RequiredInt` checks if an integer value is provided or not. |
| RequiredFloat | `RequiredFloat` checks if a float value is provided or not. |
| RequiredString | `RequiredString` checks if a string value is empty or not. |
Expand Down Expand Up @@ -60,12 +60,13 @@ Each validation rule in GoValidator has it's own default message, e.g: `required
| DefaultFloat | `DefaultFloat` sets a default value for any pointer to a float that is passed. |
| DefaultString | `DefaultString` sets a default value for any pointer to a string that is passed. |
| IsJSON | `IsJSON` will check if given string is a valid JSON. |
| NumericString | `NumericString` will check if given value is a valid string of numbers. |
| CustomRule | `CustomRule` is a dynamic method to define any custom validation rule. |

### Functions (other common validation rules)
---
| Method | Description |
|--------|---------------------------------------------------------------------|
| ------ | ------------------------------------------------------------------- |
| In | `In` checks given value is included in the provided list of values. |
| Unique | `Unique` checks whether values in the provided slice are unique. |

Expand Down
32 changes: 32 additions & 0 deletions numeric.go
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
}
62 changes: 62 additions & 0 deletions numeric_test.go
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],
)
}
}
}
39 changes: 20 additions & 19 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,26 @@ type (
var (
// methodToErrorMessage contains each validation method and its corresponding error message.
methodToErrorMessage = map[string]string{
Required: RequiredMsg,
Exists: ExistsMsg,
Len: LenMsg,
LenList: LenListMsg,
Max: MaxMsg,
MaxString: MaxStringMsg,
MinString: MinStringMsg,
Min: MinMsg,
Between: BetweenMsg,
NotExists: NotExistsMsg,
Regex: RegexMsg,
Email: EmailMsg,
UUID: UUIDMsg,
Date: DateMsg,
URL: URLMsg,
Before: BeforeMsg,
After: AfterMsg,
IP4: IP4Msg,
JSON: JSONMsg,
Required: RequiredMsg,
Exists: ExistsMsg,
Len: LenMsg,
LenList: LenListMsg,
Max: MaxMsg,
MaxString: MaxStringMsg,
MinString: MinStringMsg,
Min: MinMsg,
Between: BetweenMsg,
NotExists: NotExistsMsg,
Regex: RegexMsg,
Email: EmailMsg,
UUID: UUIDMsg,
Date: DateMsg,
URL: URLMsg,
Before: BeforeMsg,
After: AfterMsg,
IP4: IP4Msg,
JSON: JSONMsg,
NumericString: NumericStringMsg,
}

// ErrMethodMessageNotFound is the default message when a method does not have any error message on methodToErrorMessage.
Expand Down
Loading