Skip to content

Commit

Permalink
Create not_empty rule [#7]
Browse files Browse the repository at this point in the history
This rule exists to replace name of required rule letting itself with
more sense. Take a look at #7

For now the rule not_empty is a new rule implemented, do not worry about
your older code with required rule implemented.
  • Loading branch information
guiferpa committed Feb 28, 2020
1 parent 2629271 commit 208cbf4
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 14 deletions.
12 changes: 6 additions & 6 deletions README.md
Expand Up @@ -22,27 +22,27 @@ import (
)

type Body struct {
Text string `json:"text" validate:"required=true"`
Text string `json:"text" validate:"not_empty"`
}

func main() {
b := Body{}

if valid, err := gody.Validate(b, nil); err != nil {
if !valid {
log.Println("body didn't validate", err)
return
log.Println("body didn't validate:", err)
}

switch err.(type) {
case *rule.ErrRequired:
log.Println("required error", err)
break
case *rule.ErrNotEmpty:
log.Println(err)
}
}
}
```

### Kinds of validation

### Deep validation

```go
Expand Down
15 changes: 7 additions & 8 deletions example/validate.go
Expand Up @@ -11,18 +11,18 @@ import (
// SimpleValidate is a simple func to example
func SimpleValidate() {
b := struct {
Text string `json:"text" validate:"required=true"`
Text string `json:"text" validate:"not_empty"`
}{}

valid, err := gody.Validate(b, nil)
if err != nil {
if !valid {
log.Println("body do not validated", err)
log.Println("body do not validated:", err)
}

switch err.(type) {
case *rule.ErrRequired:
log.Println("required error", err)
case *rule.ErrNotEmpty:
log.Println("not empty error:", err)

}
}
Expand Down Expand Up @@ -54,8 +54,8 @@ func (r *PalindromeRule) Validate(f, v, p string) (bool, error) {
// CustomValidate is a simple func to example about a custom rule
func CustomValidate() {
b := struct {
Text string `json:"text" validate:"required=true"`
Palindrome string `json:"palindrome" validate:"palindrome=true"`
Text string `json:"text" validate:"min_bound=5"`
Palindrome string `json:"palindrome" validate:"palindrome"`
}{
Text: "test-text",
Palindrome: "test-palindrome",
Expand Down Expand Up @@ -92,7 +92,7 @@ type ItemProduct struct {
Amount int `json:"amount" validate:"min=1"`

// validate tag's necessary for validation works if not setted it'll be ignored
Price Price `json:"price" validate:"required=true"`
Price Price `json:"price" validate:"required"`
}

// DeepValidate is a simple func to example a deep validation
Expand All @@ -102,7 +102,6 @@ func DeepValidate() {
if valid, err := gody.Validate(ip, nil); err != nil {
if !valid {
log.Println("product from cart didn't validate because of", err)
return
}

switch err.(type) {
Expand Down
28 changes: 28 additions & 0 deletions rule/not_empty.go
@@ -0,0 +1,28 @@
package rule

import (
"fmt"
"strings"
)

type notEmpty struct{}

func (_ *notEmpty) Name() string {
return "not_empty"
}

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

func (e *ErrNotEmpty) Error() string {
return fmt.Sprintf("field %v cannot be empty", e.Field)
}

func (r *notEmpty) Validate(f, v, _ string) (bool, error) {
if v == "" {
return true, &ErrNotEmpty{strings.ToLower(f)}
}
return true, nil
}
43 changes: 43 additions & 0 deletions rule/not_empty_test.go
@@ -0,0 +1,43 @@
package rule

import (
"reflect"
"testing"
)

func TestNotEmptyName(t *testing.T) {
got := NotEmpty.Name()
want := "not_empty"
if got != want {
t.Errorf("NotEmpty.Name(), got: %v, want: %v", got, want)
}
}

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

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

func TestNotEmptyError(t *testing.T) {
err := &ErrNotEmpty{Field: "text"}
got := err.Error()
want := "field text cannot be empty"
if got != want {
t.Errorf(`&ErrNotEmpty{Field: "text"}.Error(), got: %v, want: %v`, got, want)
}
}
2 changes: 2 additions & 0 deletions rule/required.go
Expand Up @@ -2,6 +2,7 @@ package rule

import (
"fmt"
"log"
"strconv"
"strings"
)
Expand All @@ -26,6 +27,7 @@ func (r *required) Validate(f, v, p string) (bool, error) {
if err != nil {
return false, err
}
log.Printf("[guiferpa/gody] :: ATTETION :: required rule is deprecated, please replace to use not_empty.")
if b && v != "" {
return true, nil
}
Expand Down
3 changes: 3 additions & 0 deletions rule/rule.go
Expand Up @@ -7,6 +7,9 @@ type Rule interface {
}

var (
// NotEmpty is a rule implemented
NotEmpty = &notEmpty{}

// Required is a rule implemented
Required = &required{}

Expand Down
1 change: 1 addition & 0 deletions validate.go
Expand Up @@ -12,6 +12,7 @@ func Validate(b interface{}, customRules []rule.Rule) (bool, error) {
}

defaultRules := []rule.Rule{
rule.NotEmpty,
rule.Required,
rule.Enum,
rule.Max,
Expand Down

0 comments on commit 208cbf4

Please sign in to comment.