Skip to content

Commit

Permalink
Merge pull request #37 from Rhyanz46/handle-regex-error-compile
Browse files Browse the repository at this point in the history
handle error compile
  • Loading branch information
Rhyanz46 authored Sep 7, 2024
2 parents e626566 + 370bcf5 commit 7b26216
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 49 deletions.
5 changes: 4 additions & 1 deletion map_validator/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,10 @@ func validate(field string, dataTemp map[string]interface{}, validator Rules, da
}
return nil, errors.New("the field '" + field + "' should be string")
}
regex := regexp.MustCompile(validator.RegexString)
regex, err := doSafeRegexpMustCompile(validator.RegexString)
if err != nil {
return nil, err
}
if !regex.MatchString(data.(string)) {
if validator.CustomMsg.OnRegexString != nil {
return nil, buildMessage(*validator.CustomMsg.OnRegexString, MessageMeta{Field: &field})
Expand Down
19 changes: 19 additions & 0 deletions map_validator/safe_regex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package map_validator

import (
"errors"
"fmt"
"regexp"
)

func doSafeRegexpMustCompile(data string) (rex *regexp.Regexp, err error) {
defer func() {
if r := recover(); r != nil {
// Assign error value during panic
err = errors.New(fmt.Sprintf("Error when compiling regex: %v", r))
}
}()

rex = regexp.MustCompile(data)
return rex, nil
}
48 changes: 0 additions & 48 deletions test/modul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,54 +310,6 @@ func TestStrictTwo(t *testing.T) {
}
}

func TestValidRegex(t *testing.T) {
payload := map[string]interface{}{"hp": "+62567888", "email": "[email protected]"}
validRole := map_validator.RulesWrapper{
Rules: map[string]map_validator.Rules{
"hp": {RegexString: `^\+(?:\d{2}[- ]?\d{6}|\d{11})$`},
"email": {RegexString: `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`},
},
}
check, err := map_validator.NewValidateBuilder().SetRules(validRole).Load(payload)
if err != nil {
t.Errorf("Expected not have error, but got error : %s", err)
}
_, err = check.RunValidate()
if err != nil {
t.Errorf("Expected not have error, but got error : %s", err)
}
}

func TestInvalidRegex(t *testing.T) {
payload := map[string]interface{}{"hp": "62567888", "email": "devariansaputra.com"}
validRole := map_validator.RulesWrapper{
Rules: map[string]map_validator.Rules{
"hp": {RegexString: `^\+(?:\d{2}[- ]?\d{6}|\d{11})$`},
},
}
check, err := map_validator.NewValidateBuilder().SetRules(validRole).Load(payload)
if err != nil {
t.Errorf("Expected not have error, but got error : %s", err)
}
_, err = check.RunValidate()
if err == nil {
t.Error("Expected error, but got no error :")
}
validRole = map_validator.RulesWrapper{
Rules: map[string]map_validator.Rules{
"email": {RegexString: `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`},
},
}
check, err = map_validator.NewValidateBuilder().SetRules(validRole).Load(payload)
if err != nil {
t.Errorf("Expected not have error, but got error : %s", err)
}
_, err = check.RunValidate()
if err == nil {
t.Error("Expected error, but got no error :")
}
}

func TestValidSlice(t *testing.T) {
payload := map[string]interface{}{"hobby": []string{"reading", "football"}}
validRole := map_validator.RulesWrapper{
Expand Down
76 changes: 76 additions & 0 deletions test/regex_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package test

import (
"github.com/Rhyanz46/go-map-validator/map_validator"
"testing"
)

func TestValidRegex(t *testing.T) {
payload := map[string]interface{}{"hp": "+62567888", "email": "[email protected]"}
validRole := map_validator.RulesWrapper{
Rules: map[string]map_validator.Rules{
"hp": {RegexString: `^\+(?:\d{2}[- ]?\d{6}|\d{11})$`},
"email": {RegexString: `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`},
},
}
check, err := map_validator.NewValidateBuilder().SetRules(validRole).Load(payload)
if err != nil {
t.Errorf("Expected not have error, but got error : %s", err)
}
_, err = check.RunValidate()
if err != nil {
t.Errorf("Expected not have error, but got error : %s", err)
}
}

func TestInvalidRegex(t *testing.T) {
payload := map[string]interface{}{"hp": "62567888", "email": "devariansaputra.com"}
validRole := map_validator.RulesWrapper{
Rules: map[string]map_validator.Rules{
"hp": {RegexString: `^\+(?:\d{2}[- ]?\d{6}|\d{11})$`},
},
}
check, err := map_validator.NewValidateBuilder().SetRules(validRole).Load(payload)
if err != nil {
t.Errorf("Expected not have error, but got error : %s", err)
}
_, err = check.RunValidate()
if err == nil {
t.Error("Expected error, but got no error :")
}
validRole = map_validator.RulesWrapper{
Rules: map[string]map_validator.Rules{
"email": {RegexString: `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`},
},
}
check, err = map_validator.NewValidateBuilder().SetRules(validRole).Load(payload)
if err != nil {
t.Errorf("Expected not have error, but got error : %s", err)
}
_, err = check.RunValidate()
if err == nil {
t.Error("Expected error, but got no error :")
}
}

func TestErrorRegex(t *testing.T) {
payload := map[string]interface{}{"password": "TAlj&&28%&"}
validRole := map_validator.RulesWrapper{
Rules: map[string]map_validator.Rules{
"password": {
RegexString: "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,}$\n",
CustomMsg: map_validator.CustomMsg{
OnRegexString: map_validator.SetMessage("Your password must contain at least one uppercase letter, one lowercase letter, one number and one special character"),
},
},
},
}
check, err := map_validator.NewValidateBuilder().SetRules(validRole).Load(payload)
if err != nil {
t.Errorf("Expected not have error, but got error : %s", err)
}
_, err = check.RunValidate()
if err == nil {
t.Error("Expected error, but got no error :")
}
}

0 comments on commit 7b26216

Please sign in to comment.