Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/is boolean validation #20

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions rule/is_boolean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package rule

import (
"fmt"
"strings"
"strconv"
)

type isBoolean struct{}

func (r *isBoolean) Name() string { return "is_boolean" }

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

func (e *ErrIsBoolean) Error() string { return fmt.Sprintf("field %v must be boolean", e.Field) }

func (r *isBoolean) Validate(f, v, _ string) (bool, error) {
_, err := strconv.ParseBool(v)
if err != nil { return true, &ErrIsBoolean{strings.ToLower(f)} }
return true, nil
}
49 changes: 49 additions & 0 deletions rule/is_boolean_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package rule

import (
"reflect"
"testing"
)

func TestIsBooleanName(t *testing.T) {
r := IsBoolean
got := r.Name()
want := "is_boolean"
if got != want {
t.Errorf("IsBoolean.Name(), got: %v, want: %v", got, want)
}
}

func TestIsBooleanWithSuccessful(t *testing.T) {
r := IsBoolean
cases := [2]string{"true", "false"}
for _, test := range cases {
valid, err := r.Validate("text", test, "")
if got, want := valid, true; got != want {
t.Errorf(`valid, _ := IsBoolean.Validate("text", "%v", ""), got: %v, want: %v`, got, test, want)
}
if got, want := reflect.TypeOf(err), reflect.TypeOf(nil); got != want {
t.Errorf(`_, err := IsBoolean.Validate("text", "", ""), got: %v, want: %v`, got, want)
}
}
}

func TestIsBooleanWithInvalidValues(t *testing.T) {
r := IsBoolean
ok, err := r.Validate("text", "james", "")
if err == nil {
t.Error("Unexpected no error")
}
if got, want := ok, true; got != want {
t.Errorf(`_, err := IsBoolean.Validate("text", "", ""), got: %v, want: %v`, got, want)
}
}

func TestIsBooleanError(t *testing.T) {
err := &ErrIsBoolean{Field: "text"}
got := err.Error()
want := "field text must be boolean"
if got != want {
t.Errorf(`&ErrNotEmpty{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{}

// IsBoolean is a rule implemented
IsBoolean = &isBoolean{}
)