Skip to content

Commit

Permalink
Merge pull request #33 from rezakhademix/add-regex-validation-rule
Browse files Browse the repository at this point in the history
added: regex validation rule
  • Loading branch information
rezakhademix authored Mar 7, 2024
2 parents dd24b87 + 670a72e commit d610444
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
19 changes: 19 additions & 0 deletions regex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package validator

import "regexp"

const (
// Regex represents the rule name which will be used to find the default error message.
Regex = "regex"
// RegexMsg is the default error message format for fields with the regex validation rule.
RegexMsg = "%s is not valid"
)

// RegexMatches checks the value of s under validation must match the given regular expression.
func (v *Validator) RegexMatches(s string, pattern string, field, msg string) *Validator {
r := regexp.MustCompile(pattern)

v.Check(r.Match([]byte(s)), field, v.msg(Regex, msg))

return v
}
77 changes: 77 additions & 0 deletions regex_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package validator

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestValidator_RegexMatches(t *testing.T) {
tests := []struct {
name string
field string
value string
pattern string
isPassed bool
message string
expectedMsg string
}{
{
name: "test an empty string value will fail regex validation",
field: "code",
value: "",
pattern: "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$",
isPassed: false,
message: "code is not valid",
expectedMsg: "code is not valid",
},
{
name: "test an empty space string value will fail regex validation",
field: "name",
value: " ",
pattern: "^[0-9]{10}$",
isPassed: false,
message: "name is not valid",
expectedMsg: "name is not valid",
},
{
name: "test a wrong string value will fail regex validation",
field: "id",
value: "09377475856",
pattern: "^[0-9]{10}$",
isPassed: false,
message: "id is not valid",
expectedMsg: "id is not valid",
},
{
name: "test a correct string value will pass validation",
field: "id",
value: "1160277052",
pattern: "^[0-9]{10}$",
isPassed: true,
message: "",
expectedMsg: "",
},
{
name: "test a correct email string value will pass validation",
field: "email",
value: "[email protected]",
pattern: "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$",
isPassed: true,
message: "",
expectedMsg: "",
},
}

for _, test := range tests {
v := New()

v.RegexMatches(test.value, test.pattern, test.field, test.message)

assert.Equal(t, test.isPassed, v.IsPassed())

if !test.isPassed {
assert.Equal(t, test.expectedMsg, v.Errors()[test.field])
}
}
}
1 change: 1 addition & 0 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
Min: MinMsg,
Between: BetweenMsg,
NotExists: NotExistsMsg,
Regex: RegexMsg,
}

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

0 comments on commit d610444

Please sign in to comment.