Skip to content

Commit

Permalink
feat: added IsBool rule
Browse files Browse the repository at this point in the history
  • Loading branch information
guiferpa committed Feb 1, 2025
1 parent 2a2f225 commit 98a30a3
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
28 changes: 28 additions & 0 deletions rule/is_bool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package rule

import (
"fmt"
"strconv"
)

type isBool struct{}

func (r *isBool) Name() string {
return "is_bool"
}

// ErrIsBool is the representation about any error happened inside of the rule IsBool
type ErrIsBool struct {
Field string
}

func (err *ErrIsBool) Error() string {
return fmt.Sprintf("field %v must be 'true' or 'false'", err.Field)
}

func (r *isBool) Validate(f, v, p string) (bool, error) {
if _, err := strconv.ParseBool(v); err != nil {
return true, &ErrIsBool{f}
}
return true, nil
}
51 changes: 51 additions & 0 deletions rule/is_bool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package rule

import (
"reflect"
"testing"
)

func TestIsBoolName(t *testing.T) {
got := IsBool.Name()
want := "is_bool"
if got != want {
t.Errorf("IsBool.Name(), got: %v, want: %v", got, want)
}
}

func TestIsBoolWithSuccessful(t *testing.T) {
valid, err := IsBool.Validate("text", "false", "")
if got, want := valid, true; got != want {
t.Errorf(`valid, _ := IsBool.Validate("text", "false", ""), got: %v, want: %v`, got, want)
}
if got, want := reflect.TypeOf(err), reflect.TypeOf(nil); got != want {
t.Errorf(`_, err := IsBool.Validate("text", "false", ""), got: %v, want: %v`, got, want)
}

valid, err = IsBool.Validate("text", "true", "")
if got, want := valid, true; got != want {
t.Errorf(`valid, _ := IsBool.Validate("text", "true", ""), got: %v, want: %v`, got, want)
}
if got, want := reflect.TypeOf(err), reflect.TypeOf(nil); got != want {
t.Errorf(`_, err := IsBool.Validate("text", "true", ""), got: %v, want: %v`, got, want)
}
}

func TestIsBoolWithEmptyValue(t *testing.T) {
valid, err := IsBool.Validate("text", "", "")
if got, want := valid, true; got != want {
t.Errorf(`valid, _ := IsBool.Validate("text", "", ""), got: %v, want: %v`, got, want)
}
if got, want := reflect.TypeOf(err), reflect.TypeOf(&ErrIsBool{}); got != want {
t.Errorf(`_, err := IsBool.Validate("text", "", ""), got: %v, want: %v`, got, want)
}
}

func TestIsBoolError(t *testing.T) {
err := &ErrIsBool{Field: "text"}
got := err.Error()
want := "field text must be 'true' or 'false'"
if got != want {
t.Errorf(`&ErrIsBool{Field: "text"}.Error(), got: %v, want: %v`, got, want)
}
}
3 changes: 3 additions & 0 deletions rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ var (

// MinBound is a rule implemented
MinBound = &minBound{}

// IsBool is a rule implemented
IsBool = &isBool{}
)
1 change: 1 addition & 0 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func RawDefaultValidate(b interface{}, tn string, customRules []Rule) (bool, err
rule.Min,
rule.MaxBound,
rule.MinBound,
rule.IsBool,
}

return RawValidate(b, tn, append(defaultRules, customRules...))
Expand Down

0 comments on commit 98a30a3

Please sign in to comment.