add min/max length validation, improve tests
This commit is contained in:
+19
-18
@@ -2,26 +2,27 @@ package validate
|
|||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestInvalidEmail(t *testing.T) {
|
func TestEmail(t *testing.T) {
|
||||||
valid := []string{
|
type TestCase struct {
|
||||||
"testexample.com",
|
Input string
|
||||||
|
Err bool
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, email := range valid {
|
testCases := []TestCase{
|
||||||
if err := Email(email); err == nil {
|
{Input: "test@example.com"},
|
||||||
t.Errorf("%s is not a valid email", email)
|
{Input: "testexample.com", Err: true},
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
for _, testCase := range testCases {
|
||||||
|
err := Email(testCase.Input)
|
||||||
func TestValidEmail(t *testing.T) {
|
if testCase.Err {
|
||||||
valid := []string{
|
if err == nil {
|
||||||
"test@example.com",
|
t.Errorf("Expected %q to be an invalid email; got nil", testCase.Input)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
for _, email := range valid {
|
if err != nil {
|
||||||
if err := Email(email); err != nil {
|
t.Errorf("Expected %q to be a valid email; got %s", testCase.Input, err)
|
||||||
t.Errorf("%s is a valid email", email)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package validate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MaxLength validates the length of a string as being less than or equal to a given maximum.
|
||||||
|
func MaxLength(l int) func(string) error {
|
||||||
|
return func(value string) error {
|
||||||
|
if len(value) > l {
|
||||||
|
if l != 1 {
|
||||||
|
return fmt.Errorf("Must not be longer than %d characters", l)
|
||||||
|
}
|
||||||
|
return errors.New("Must not be longer than 1 character")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MinLength validates the length of a string as being greater than or equal to a given minimum.
|
||||||
|
func MinLength(l int) func(string) error {
|
||||||
|
return func(value string) error {
|
||||||
|
if len(value) < l {
|
||||||
|
if l != 1 {
|
||||||
|
return fmt.Errorf("Must not be shorter than %d characters", l)
|
||||||
|
}
|
||||||
|
return errors.New("Must not be shorter than 1 character")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
package validate
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestMaxLength(t *testing.T) {
|
||||||
|
type TestCase struct {
|
||||||
|
L int
|
||||||
|
Input string
|
||||||
|
Err bool
|
||||||
|
}
|
||||||
|
|
||||||
|
testCases := []TestCase{
|
||||||
|
{L: 8, Input: "abcd"},
|
||||||
|
{L: 8, Input: "abcdefgh"},
|
||||||
|
{L: 8, Input: "abcd efg"},
|
||||||
|
{L: 8, Input: "abcdefghi", Err: true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, testCase := range testCases {
|
||||||
|
t.Logf("Max length %d for %q", testCase.L, testCase.Input)
|
||||||
|
|
||||||
|
f := MaxLength(testCase.L)
|
||||||
|
err := f(testCase.Input)
|
||||||
|
if testCase.Err {
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Expected an error; got nil")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Expected nil; got %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMinLength(t *testing.T) {
|
||||||
|
type TestCase struct {
|
||||||
|
L int
|
||||||
|
Input string
|
||||||
|
Err bool
|
||||||
|
}
|
||||||
|
|
||||||
|
testCases := []TestCase{
|
||||||
|
{L: 8, Input: "abcd", Err: true},
|
||||||
|
{L: 8, Input: "abcdefgh"},
|
||||||
|
{L: 8, Input: "abcd efg"},
|
||||||
|
{L: 8, Input: "abcdefghi"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, testCase := range testCases {
|
||||||
|
t.Logf("Min length %d for %q", testCase.L, testCase.Input)
|
||||||
|
|
||||||
|
f := MinLength(testCase.L)
|
||||||
|
err := f(testCase.Input)
|
||||||
|
if testCase.Err {
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Expected an error; got nil")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Expected nil; got %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+24
-23
@@ -2,31 +2,32 @@ package validate
|
|||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestInvalidUUID(t *testing.T) {
|
func TestUUID(t *testing.T) {
|
||||||
invalid := []string{
|
type TestCase struct {
|
||||||
"Not a UUID",
|
Input string
|
||||||
"00000000-00-0000-0000-00000000000000",
|
Err bool
|
||||||
"00000000000000000000000000000000",
|
|
||||||
"01234567-89ab-cdef-ghij-klmnopqrstuv",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, uuid := range invalid {
|
testCases := []TestCase{
|
||||||
if err := UUID(uuid); err == nil {
|
{Input: "00000000-0000-0000-0000-000000000000"},
|
||||||
t.Errorf("%s is not a valid UUID", uuid)
|
{Input: "01234567-89ab-cdef-0123-456789abcdef"},
|
||||||
}
|
{Input: "abcdef01-2345-6789-abcd-ef0123456789"},
|
||||||
}
|
{Input: "Not a UUID", Err: true},
|
||||||
}
|
{Input: "00000000-00-0000-0000-00000000000000", Err: true},
|
||||||
|
{Input: "00000000000000000000000000000000", Err: true},
|
||||||
func TestValidUUID(t *testing.T) {
|
{Input: "01234567-89ab-cdef-ghij-klmnopqrstuv", Err: true},
|
||||||
valid := []string{
|
}
|
||||||
"00000000-0000-0000-0000-000000000000",
|
|
||||||
"01234567-89ab-cdef-0123-456789abcdef",
|
for _, testCase := range testCases {
|
||||||
"abcdef01-2345-6789-abcd-ef0123456789",
|
err := UUID(testCase.Input)
|
||||||
}
|
if testCase.Err {
|
||||||
|
if err == nil {
|
||||||
for _, uuid := range valid {
|
t.Errorf("Expected %q to be an invalid UUID; got nil", testCase.Input)
|
||||||
if err := UUID(uuid); err != nil {
|
}
|
||||||
t.Errorf("%s is a valid UUID", uuid)
|
} else {
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Expected %q to be a valid UUID; got %s", testCase.Input, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user