-
Notifications
You must be signed in to change notification settings - Fork 1
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 #37 from Rhyanz46/handle-regex-error-compile
handle error compile
- Loading branch information
Showing
4 changed files
with
99 additions
and
49 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,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 | ||
} |
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 |
---|---|---|
|
@@ -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{ | ||
|
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,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 :") | ||
} | ||
} |