Skip to content
This repository has been archived by the owner on May 23, 2022. It is now read-only.

Add more validation methods #5

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions rule/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ 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")
var MustBeInArrayError = ValidationError.Wrap("the :attribute field does not exist in :other")

// System Error
var OptionDateIsRequiredError = errors.New("option Date is required")
Expand Down
27 changes: 27 additions & 0 deletions rule/in_array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package rule

import (
"github.com/confetti-framework/support"
)

type InArray struct {
inArray []interface{}
err error
}

// Receives slice of interface
func (i InArray) Array(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
}
34 changes: 34 additions & 0 deletions rule/regex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package rule

import (
"regexp"

"github.com/confetti-framework/support"
)

type Regex struct {
Expression *regexp.Regexp
err error
}

// Compiles Regular expression
// Returns error in Verify() if given Expression is invalid
func (r Regex) Match(exp string) Regex {
var err error

r.Expression, err = regexp.Compile(exp)
r.err = err

return r
}

func (r Regex) Verify(value support.Value) error {
// Error compiling the regex
if r.err != nil {
return MustCompileRegexError
}
if ok := r.Expression.MatchString(value.String()); !ok {
return MustMatchRegexError
}
return nil
}
62 changes: 62 additions & 0 deletions test/rule/in_array_test.go
Original file line number Diff line number Diff line change
@@ -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{}.
Array("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{}.
Array("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{}.
Array(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{}.
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())
}

func Test_empty_input(t *testing.T) {
value := support.NewValue("test_string")
err := rule.
InArray{}.
Array().
Verify(value)

require.Equal(t, "the :attribute field does not exist in :other", err.Error())
}
47 changes: 47 additions & 0 deletions test/rule/regex_test.go
Original file line number Diff line number Diff line change
@@ -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("[email protected]")
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("[email protected]")
err := rule.
Regex{}.
Match(`[\w]{3,15}@[a-z]{3,5}\.com`).
Verify(value)

require.Equal(t, "the :attribute format is invalid", err.Error())
}