From 15be4cb8fac3cbced2e6bcdc278073449f22c2d4 Mon Sep 17 00:00:00 2001 From: Rhyanz46 Date: Sat, 7 Sep 2024 14:11:03 +0700 Subject: [PATCH 1/3] add unit test manipulator --- test/manipulator_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/manipulator_test.go b/test/manipulator_test.go index 8b92c1c..c32e52d 100644 --- a/test/manipulator_test.go +++ b/test/manipulator_test.go @@ -78,3 +78,31 @@ 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"]) + } +} From 13341ef19507c6629808910f9f0e4c28ac663fb9 Mon Sep 17 00:00:00 2001 From: Rhyanz46 Date: Sun, 8 Sep 2024 10:01:39 +0700 Subject: [PATCH 2/3] SetFieldsManipulator --- map_validator/roles_builder.go | 12 +++++++++++ map_validator/setting_builder.go | 14 +++++++++++++ test/manipulator_test.go | 34 ++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 map_validator/setting_builder.go diff --git a/map_validator/roles_builder.go b/map_validator/roles_builder.go index b632b12..800923d 100644 --- a/map_validator/roles_builder.go +++ b/map_validator/roles_builder.go @@ -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 } diff --git a/map_validator/setting_builder.go b/map_validator/setting_builder.go new file mode 100644 index 0000000..7abb8c5 --- /dev/null +++ b/map_validator/setting_builder.go @@ -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 +} diff --git a/test/manipulator_test.go b/test/manipulator_test.go index c32e52d..fcffa02 100644 --- a/test/manipulator_test.go +++ b/test/manipulator_test.go @@ -106,3 +106,37 @@ func TestManipulateOnNullableField(t *testing.T) { 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"]) + } +} From 409a8c78a6e8d4e2874f45a1cf927c34cb2084e4 Mon Sep 17 00:00:00 2001 From: Rhyanz46 Date: Sun, 8 Sep 2024 10:03:00 +0700 Subject: [PATCH 3/3] remove `doSafeRegexpMustCompile` --- map_validator/functions.go | 2 +- map_validator/safe_regex.go | 19 ------------------- 2 files changed, 1 insertion(+), 20 deletions(-) delete mode 100644 map_validator/safe_regex.go diff --git a/map_validator/functions.go b/map_validator/functions.go index 227f4bb..181c6bb 100644 --- a/map_validator/functions.go +++ b/map_validator/functions.go @@ -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 } diff --git a/map_validator/safe_regex.go b/map_validator/safe_regex.go deleted file mode 100644 index a5ef813..0000000 --- a/map_validator/safe_regex.go +++ /dev/null @@ -1,19 +0,0 @@ -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 -}