Skip to content

Commit

Permalink
Merge pull request #36 from Rhyanz46/manipulator
Browse files Browse the repository at this point in the history
add unit test manipulator
  • Loading branch information
Rhyanz46 authored Sep 8, 2024
2 parents 7b26216 + 409a8c7 commit 58d5f99
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 20 deletions.
2 changes: 1 addition & 1 deletion map_validator/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func validate(field string, dataTemp map[string]interface{}, validator Rules, da
}
return nil, errors.New("the field '" + field + "' should be string")
}
regex, err := doSafeRegexpMustCompile(validator.RegexString)
regex, err := regexp.Compile(validator.RegexString)
if err != nil {
return nil, err
}
Expand Down
12 changes: 12 additions & 0 deletions map_validator/roles_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ func (rw *RulesWrapper) SetManipulator(field string, fun func(data interface{})
return rw
}

func (rw *RulesWrapper) SetFieldsManipulator(fields []string, fun func(data interface{}) (result interface{}, err error)) *RulesWrapper {
for _, field := range fields {
rw.manipulator = append(rw.manipulator, manipulator{Field: field, Func: &fun})
}
return rw
}

func (rw *RulesWrapper) SetSetting(setting Setting) *RulesWrapper {
rw.Setting = setting
return rw
}

func (rw *RulesWrapper) Done() RulesWrapper {
return *rw
}
19 changes: 0 additions & 19 deletions map_validator/safe_regex.go

This file was deleted.

14 changes: 14 additions & 0 deletions map_validator/setting_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package map_validator

func BuildSetting() *Setting {
return &Setting{}
}

func (s *Setting) MakeStrict() *Setting {
s.Strict = true
return s
}

func (s *Setting) Done() Setting {
return *s
}
62 changes: 62 additions & 0 deletions test/manipulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,65 @@ func TestManipulateWithFullBuilderRoles(t *testing.T) {
t.Errorf("Expected description to be test arian keren bgt kan mantap bukan, but got %s", data["description"])
}
}

func TestManipulateOnNullableField(t *testing.T) {
data := map[string]interface{}{
"note": "coba aja mungkin bisa \t mantap",
}

trimAfterValidation := func(i interface{}) (result interface{}, e error) {
x := i.(string)
result = trimAndClean(x)
return
}

roles := map_validator.BuildRoles().
SetRule("description", map_validator.Rules{Type: reflect.String, Null: true}).
SetRule("note", map_validator.Rules{Type: reflect.String}).
SetManipulator("description", trimAfterValidation).
Done()

xx, err := map_validator.NewValidateBuilder().SetRules(roles).Load(data)
extraCheck, err := xx.RunValidate()
if err != nil {
t.Errorf("Expected not have error, but got error : %s", err)
return
}
if extraCheck.GetData()["description"] != nil {
t.Errorf("Expected description to be nil, but got %s", data["description"])
}
}

func TestManipulateOnMultiFields(t *testing.T) {
data := map[string]interface{}{
"name": "arian\n king saputra",
"description": "test arian keren bgt kan \t\n\n mantap bukan",
"note": "coba aja mungkin bisa \t mantap",
}

trimAfterValidation := func(i interface{}) (result interface{}, e error) {
x := i.(string)
result = trimAndClean(x)
return
}

roles := map_validator.BuildRoles().
SetRule("name", map_validator.Rules{Type: reflect.String}).
SetRule("description", map_validator.Rules{Type: reflect.String}).
SetRule("note", map_validator.Rules{Type: reflect.String}).
SetFieldsManipulator([]string{"description", "name"}, trimAfterValidation).
Done()

xx, err := map_validator.NewValidateBuilder().SetRules(roles).Load(data)
extraCheck, err := xx.RunValidate()
if err != nil {
t.Errorf("Expected not have error, but got error : %s", err)
return
}
if extraCheck.GetData()["name"] != "arian king saputra" {
t.Errorf("Expected name to be arian king saputra, but got %s", extraCheck.GetData()["name"])
}
if extraCheck.GetData()["description"] != "test arian keren bgt kan mantap bukan" {
t.Errorf("Expected description to be test arian keren bgt kan mantap bukan, but got %s", extraCheck.GetData()["description"])
}
}

0 comments on commit 58d5f99

Please sign in to comment.