-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
7 changed files
with
90 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters