From a2a571fc10c262f79fe958b4d39559921ebebeec Mon Sep 17 00:00:00 2001 From: Rafael Breno Date: Mon, 8 Feb 2021 22:24:58 -0300 Subject: [PATCH 01/12] Add regex error messages --- rule/errors.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rule/errors.go b/rule/errors.go index 93cc9cd..35065fc 100644 --- a/rule/errors.go +++ b/rule/errors.go @@ -32,6 +32,8 @@ var MustBeContainItemsError = ValidationError.Wrap("the :attribute must contain var MustBeASliceError = ValidationError.Wrap("the :attribute must be a slice") var MustBeAStringError = ValidationError.Wrap("the :attribute must be a string") var MustBeAMapError = ValidationError.Wrap("the :attribute must be a map") +var MustCompileRegexError = ValidationError.Wrap("the :expect isn't a valid regex") +var MustMatchRegexError = ValidationError.Wrap("the :attribute format is invalid") // System Error var OptionDateIsRequiredError = errors.New("option Date is required") From 4717eed3eb2bd394cccccce3032ab4024333fb11 Mon Sep 17 00:00:00 2001 From: Rafael Breno Date: Mon, 8 Feb 2021 22:25:09 -0300 Subject: [PATCH 02/12] Create Regex rule --- rule/regex.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 rule/regex.go diff --git a/rule/regex.go b/rule/regex.go new file mode 100644 index 0000000..53faf2f --- /dev/null +++ b/rule/regex.go @@ -0,0 +1,36 @@ +package rule + +import ( + "regexp" + + "github.com/confetti-framework/support" +) + +type Regex struct { + Expression *regexp.Regexp + Error error +} + +// Compiles Regular expression +// Returns error if given Expression is invalid +func (r Regex) Match(exp string) Regex { + var err error + + r.Expression, err = regexp.Compile(exp) + r.Error = err + + return r +} + +func (r Regex) Verify(value support.Value) error { + // Error compiling the regex + if r.Error != nil { + return MustCompileRegexError + //return r.Error + } + if ok := r.Expression.MatchString(value.String()); !ok { + //return fmt.Errorf("format invalid") + return MustMatchRegexError + } + return nil +} From 0a93d3c6c61c9726a74f5a5e9f6fc09ac3008384 Mon Sep 17 00:00:00 2001 From: Rafael Breno Date: Mon, 8 Feb 2021 22:25:26 -0300 Subject: [PATCH 03/12] Add regex validation rule tests --- test/rule/regex_test.go | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 test/rule/regex_test.go diff --git a/test/rule/regex_test.go b/test/rule/regex_test.go new file mode 100644 index 0000000..c316864 --- /dev/null +++ b/test/rule/regex_test.go @@ -0,0 +1,47 @@ +package rule + +import ( + "github.com/confetti-framework/support" + "github.com/confetti-framework/validation/rule" + "github.com/stretchr/testify/require" + "testing" +) + +func Test_regex_with_valid_expression_valid_input(t *testing.T) { + value := support.NewValue("abcd") + err := rule.Regex{}. + Match("[a-z]{1,}"). + Verify(value) + + require.Nil(t, err) +} + +func Test_regex_with_valid_advanced_expression_valid_input(t *testing.T) { + value := support.NewValue("conf_etti@github.com") + err := rule. + Regex{}. + Match(`[\w]{3,15}@[a-z]{3,5}\.com`). + Verify(value) + + require.Equal(t, "the :attribute format is invalid", err.Error()) +} + +func Test_regex_with_invalid_expression(t *testing.T) { + value := support.NewValue("abcd") + err := rule. + Regex{}. + Match(`\k`). + Verify(value) + + require.Equal(t, "the :expect isn't a valid regex", err.Error()) +} + +func Test_regex_with_valid_expression_invalid_input(t *testing.T) { + value := support.NewValue("aa@abc.com.br") + err := rule. + Regex{}. + Match(`[\w]{3,15}@[a-z]{3,5}\.com`). + Verify(value) + + require.Equal(t, "the :attribute format is invalid", err.Error()) +} From 253468c7f0f3ca4fe8d987a60befaa8c88394f3b Mon Sep 17 00:00:00 2001 From: Rafael Breno Date: Wed, 10 Feb 2021 00:54:25 -0300 Subject: [PATCH 04/12] Add 'in array' validation with tests --- rule/errors.go | 1 + rule/in_array.go | 29 ++++++++++++++++++ test/rule/in_array_test.go | 62 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 rule/in_array.go create mode 100644 test/rule/in_array_test.go diff --git a/rule/errors.go b/rule/errors.go index 35065fc..085aee6 100644 --- a/rule/errors.go +++ b/rule/errors.go @@ -34,6 +34,7 @@ var MustBeAStringError = ValidationError.Wrap("the :attribute must be a string") var MustBeAMapError = ValidationError.Wrap("the :attribute must be a map") var MustCompileRegexError = ValidationError.Wrap("the :expect isn't a valid regex") var MustMatchRegexError = ValidationError.Wrap("the :attribute format is invalid") +var MustBeInArrayError = ValidationError.Wrap("the :attribute field does not exist in :other") // System Error var OptionDateIsRequiredError = errors.New("option Date is required") diff --git a/rule/in_array.go b/rule/in_array.go new file mode 100644 index 0000000..fe544d9 --- /dev/null +++ b/rule/in_array.go @@ -0,0 +1,29 @@ +package rule + +import ( + "github.com/confetti-framework/support" +) + +type InArray struct { + inArray []interface{} + err error +} + +/* Receives slice of interface +**/ +func (i InArray) InArray(values ...interface{}) InArray { + i.inArray = values + return i +} + +/* Compare each slice item with the searched value + * All of them in interface +**/ +func (i InArray) Verify(value support.Value) error { + for _, val := range i.inArray { + if value.Source() == val { + return nil + } + } + return MustBeInArrayError +} diff --git a/test/rule/in_array_test.go b/test/rule/in_array_test.go new file mode 100644 index 0000000..cd0535f --- /dev/null +++ b/test/rule/in_array_test.go @@ -0,0 +1,62 @@ +package rule + +import ( + "github.com/confetti-framework/support" + "github.com/confetti-framework/validation/rule" + "github.com/stretchr/testify/require" + "testing" +) + +func Test_in_array_with_valid_inputs_same_type(t *testing.T) { + value := support.NewValue("test_string") + err := rule. + InArray{}. + InArray("not_valid", "invalid", "test_string"). + Verify(value) + + require.Nil(t, err) +} + +func Test_invalid_input_same_type(t *testing.T) { + value := support.NewValue("test_string") + err := rule. + InArray{}. + InArray("not_valid", "invalid", "still_invalid"). + Verify(value) + + require.Equal(t, "the :attribute field does not exist in :other", err.Error()) +} + +type testInArrayStruct struct { + HelloWorld uint +} + +func Test_valid_input_different_types(t *testing.T) { + value := support.NewValue("test_string") + err := rule. + InArray{}. + InArray(1234, 123.45, true, []string{"hello", "there"}, new(testInArrayStruct), "test_string"). + Verify(value) + + require.Nil(t, err) +} + +func Test_invalid_input_different_types(t *testing.T) { + value := support.NewValue("test_string") + err := rule. + InArray{}. + InArray(1234, 123.45, true, []string{"hello", "there"}, new(testInArrayStruct)). + Verify(value) + + require.Equal(t, "the :attribute field does not exist in :other", err.Error()) +} + +func Test_empty_input(t *testing.T) { + value := support.NewValue("test_string") + err := rule. + InArray{}. + InArray(). + Verify(value) + + require.Nil(t, err) +} From 4db913eb81b39a87e462d1b7d107d3fabd8022ad Mon Sep 17 00:00:00 2001 From: Rafael Breno Date: Wed, 10 Feb 2021 20:55:37 -0300 Subject: [PATCH 05/12] Fix in_array test --- test/rule/in_array_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/rule/in_array_test.go b/test/rule/in_array_test.go index cd0535f..1034d12 100644 --- a/test/rule/in_array_test.go +++ b/test/rule/in_array_test.go @@ -58,5 +58,5 @@ func Test_empty_input(t *testing.T) { InArray(). Verify(value) - require.Nil(t, err) + require.Equal(t, "the :attribute field does not exist in :other", err.Error()) } From 9fa634a2a684c63691d424505bbc54b46247e38c Mon Sep 17 00:00:00 2001 From: rafaelbreno <32229014+rafaelbreno@users.noreply.github.com> Date: Thu, 25 Feb 2021 13:50:47 -0300 Subject: [PATCH 06/12] Remove unused commented code in rule/regex.go Co-authored-by: Reindert <16726304+reindert-vetter@users.noreply.github.com> --- rule/regex.go | 1 - 1 file changed, 1 deletion(-) diff --git a/rule/regex.go b/rule/regex.go index 53faf2f..788464a 100644 --- a/rule/regex.go +++ b/rule/regex.go @@ -29,7 +29,6 @@ func (r Regex) Verify(value support.Value) error { //return r.Error } if ok := r.Expression.MatchString(value.String()); !ok { - //return fmt.Errorf("format invalid") return MustMatchRegexError } return nil From f41ece0083258c3a1894a4c74a94542dfe985790 Mon Sep 17 00:00:00 2001 From: rafaelbreno <32229014+rafaelbreno@users.noreply.github.com> Date: Thu, 25 Feb 2021 13:51:27 -0300 Subject: [PATCH 07/12] Remove unused commented code Co-authored-by: Reindert <16726304+reindert-vetter@users.noreply.github.com> --- rule/regex.go | 1 - 1 file changed, 1 deletion(-) diff --git a/rule/regex.go b/rule/regex.go index 788464a..52adbda 100644 --- a/rule/regex.go +++ b/rule/regex.go @@ -26,7 +26,6 @@ func (r Regex) Verify(value support.Value) error { // Error compiling the regex if r.Error != nil { return MustCompileRegexError - //return r.Error } if ok := r.Expression.MatchString(value.String()); !ok { return MustMatchRegexError From 74264be943cc7765a86872acf181004f1206a48c Mon Sep 17 00:00:00 2001 From: rafaelbreno <32229014+rafaelbreno@users.noreply.github.com> Date: Thu, 25 Feb 2021 13:52:13 -0300 Subject: [PATCH 08/12] Add more details at comment block Co-authored-by: Reindert <16726304+reindert-vetter@users.noreply.github.com> --- rule/regex.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rule/regex.go b/rule/regex.go index 52adbda..9d4ffbb 100644 --- a/rule/regex.go +++ b/rule/regex.go @@ -12,7 +12,7 @@ type Regex struct { } // Compiles Regular expression -// Returns error if given Expression is invalid +// Returns error in Verify() if given Expression is invalid func (r Regex) Match(exp string) Regex { var err error From 2c18f097c6dcbd344fc9daed4e33eb41bda69bff Mon Sep 17 00:00:00 2001 From: rafaelbreno <32229014+rafaelbreno@users.noreply.github.com> Date: Thu, 25 Feb 2021 13:52:31 -0300 Subject: [PATCH 09/12] Update comment structure Co-authored-by: Reindert <16726304+reindert-vetter@users.noreply.github.com> --- rule/in_array.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/rule/in_array.go b/rule/in_array.go index fe544d9..3825cdd 100644 --- a/rule/in_array.go +++ b/rule/in_array.go @@ -16,9 +16,8 @@ func (i InArray) InArray(values ...interface{}) InArray { return i } -/* Compare each slice item with the searched value - * All of them in interface -**/ +// Compare each slice item with the searched value +// All of them in interface func (i InArray) Verify(value support.Value) error { for _, val := range i.inArray { if value.Source() == val { From 7aab7388bf122fd2d771c9c9fa8712c2cb8dd369 Mon Sep 17 00:00:00 2001 From: rafaelbreno <32229014+rafaelbreno@users.noreply.github.com> Date: Thu, 25 Feb 2021 13:52:49 -0300 Subject: [PATCH 10/12] Update comment structure Co-authored-by: Reindert <16726304+reindert-vetter@users.noreply.github.com> --- rule/in_array.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rule/in_array.go b/rule/in_array.go index 3825cdd..50f3a0f 100644 --- a/rule/in_array.go +++ b/rule/in_array.go @@ -9,8 +9,7 @@ type InArray struct { err error } -/* Receives slice of interface -**/ +// Receives slice of interface func (i InArray) InArray(values ...interface{}) InArray { i.inArray = values return i From 124b39f9975963d837eea7209e8a48eb7bae5fb9 Mon Sep 17 00:00:00 2001 From: Rafael Breno Date: Thu, 25 Feb 2021 13:56:21 -0300 Subject: [PATCH 11/12] Update regex rule struct attribute, from 'Error' to 'err' --- rule/regex.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rule/regex.go b/rule/regex.go index 9d4ffbb..4728fe4 100644 --- a/rule/regex.go +++ b/rule/regex.go @@ -8,7 +8,7 @@ import ( type Regex struct { Expression *regexp.Regexp - Error error + err error } // Compiles Regular expression @@ -17,14 +17,14 @@ func (r Regex) Match(exp string) Regex { var err error r.Expression, err = regexp.Compile(exp) - r.Error = err + r.err = err return r } func (r Regex) Verify(value support.Value) error { // Error compiling the regex - if r.Error != nil { + if r.err != nil { return MustCompileRegexError } if ok := r.Expression.MatchString(value.String()); !ok { From 6cd4fd3dc8d398fb9d2d5ae09b8750d21f594acc Mon Sep 17 00:00:00 2001 From: Rafael Breno Date: Thu, 25 Feb 2021 21:32:20 -0300 Subject: [PATCH 12/12] Update InArray method name, from InArray to Array --- rule/in_array.go | 2 +- test/rule/in_array_test.go | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/rule/in_array.go b/rule/in_array.go index 50f3a0f..b31ffe9 100644 --- a/rule/in_array.go +++ b/rule/in_array.go @@ -10,7 +10,7 @@ type InArray struct { } // Receives slice of interface -func (i InArray) InArray(values ...interface{}) InArray { +func (i InArray) Array(values ...interface{}) InArray { i.inArray = values return i } diff --git a/test/rule/in_array_test.go b/test/rule/in_array_test.go index 1034d12..9b3f7a0 100644 --- a/test/rule/in_array_test.go +++ b/test/rule/in_array_test.go @@ -11,7 +11,7 @@ func Test_in_array_with_valid_inputs_same_type(t *testing.T) { value := support.NewValue("test_string") err := rule. InArray{}. - InArray("not_valid", "invalid", "test_string"). + Array("not_valid", "invalid", "test_string"). Verify(value) require.Nil(t, err) @@ -21,7 +21,7 @@ func Test_invalid_input_same_type(t *testing.T) { value := support.NewValue("test_string") err := rule. InArray{}. - InArray("not_valid", "invalid", "still_invalid"). + Array("not_valid", "invalid", "still_invalid"). Verify(value) require.Equal(t, "the :attribute field does not exist in :other", err.Error()) @@ -35,7 +35,7 @@ func Test_valid_input_different_types(t *testing.T) { value := support.NewValue("test_string") err := rule. InArray{}. - InArray(1234, 123.45, true, []string{"hello", "there"}, new(testInArrayStruct), "test_string"). + Array(1234, 123.45, true, []string{"hello", "there"}, new(testInArrayStruct), "test_string"). Verify(value) require.Nil(t, err) @@ -45,7 +45,7 @@ func Test_invalid_input_different_types(t *testing.T) { value := support.NewValue("test_string") err := rule. InArray{}. - InArray(1234, 123.45, true, []string{"hello", "there"}, new(testInArrayStruct)). + Array(1234, 123.45, true, []string{"hello", "there"}, new(testInArrayStruct)). Verify(value) require.Equal(t, "the :attribute field does not exist in :other", err.Error()) @@ -55,7 +55,7 @@ func Test_empty_input(t *testing.T) { value := support.NewValue("test_string") err := rule. InArray{}. - InArray(). + Array(). Verify(value) require.Equal(t, "the :attribute field does not exist in :other", err.Error())