Kaynağa Gözat

update all validation tests

Aneurin Barker Snook 1 yıl önce
ebeveyn
işleme
40482060ac
10 değiştirilmiş dosya ile 24 ekleme ve 32 silme
  1. 2 2
      all_test.go
  2. 1 2
      email.go
  3. 5 1
      equal.go
  4. 2 2
      equal_test.go
  5. 1 3
      in.go
  6. 4 8
      length.go
  7. 2 2
      length_test.go
  8. 4 8
      size.go
  9. 2 2
      size_test.go
  10. 1 2
      uuid.go

+ 2 - 2
all_test.go

@@ -23,8 +23,8 @@ func TestAll(t *testing.T) {
 		{Input: "abcd", F: f},
 		{Input: "abcdef", F: f},
 		{Input: "12345678", F: f},
-		{Input: "abc", F: f, Err: ErrTooFewChars},
-		{Input: "abcdef012", F: f, Err: ErrTooManyChars},
+		{Input: "abc", F: f, Err: ErrMustBeLonger},
+		{Input: "abcdef012", F: f, Err: ErrMustBeShorter},
 		{Input: "abcdefgh", F: f, Err: ErrDisallowedChars},
 		{Input: "01abcd", F: f, Err: ErrValueNotAllowed},
 	}

+ 1 - 2
email.go

@@ -1,13 +1,12 @@
 package validate
 
 import (
-	"errors"
 	"regexp"
 )
 
 // Validation error.
 var (
-	ErrInvalidEmail = errors.New("invalid email address")
+	ErrInvalidEmail = NewError("invalid email address")
 )
 
 // Based on https://stackoverflow.com/a/201378

+ 5 - 1
equal.go

@@ -1,10 +1,14 @@
 package validate
 
+var (
+	ErrNotEqual = NewError("must be equal to %v")
+)
+
 // Equal validates whether an input value is equal to a comparison value.
 func Equal[T comparable](cmp T) func(T) error {
 	return func(value T) error {
 		if value != cmp {
-			return ErrValueNotAllowed
+			return ErrNotEqual.With(cmp)
 		}
 		return nil
 	}

+ 2 - 2
equal_test.go

@@ -15,7 +15,7 @@ func TestEqualInt(t *testing.T) {
 	testCases := []TestCase{
 		{I: 1, C: 1},
 		{I: 5 ^ 3, C: 5 ^ 3},
-		{I: 10, C: 15, Err: ErrValueNotAllowed},
+		{I: 10, C: 15, Err: ErrNotEqual},
 	}
 
 	for i, tc := range testCases {
@@ -40,7 +40,7 @@ func TestEqualStr(t *testing.T) {
 	testCases := []TestCase{
 		{I: "abc", C: "abc"},
 		{I: "def ghi 123", C: "def ghi 123"},
-		{I: "jkl", C: "mno", Err: ErrValueNotAllowed},
+		{I: "jkl", C: "mno", Err: ErrNotEqual},
 	}
 
 	for i, tc := range testCases {

+ 1 - 3
in.go

@@ -1,10 +1,8 @@
 package validate
 
-import "errors"
-
 // Validation error.
 var (
-	ErrValueNotAllowed = errors.New("not allowed")
+	ErrValueNotAllowed = NewError("not allowed")
 )
 
 // In validates whether a value is found in a slice of allowed values.

+ 4 - 8
length.go

@@ -1,20 +1,16 @@
 package validate
 
-import (
-	"errors"
-)
-
 // Validation error.
 var (
-	ErrTooFewChars  = errors.New("too few characters")
-	ErrTooManyChars = errors.New("too many characters")
+	ErrMustBeLonger  = NewError("must be at least %d characters")
+	ErrMustBeShorter = NewError("must be no more than %d characters")
 )
 
 // 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 {
-			return ErrTooManyChars
+			return ErrMustBeShorter.With(l)
 		}
 		return nil
 	}
@@ -24,7 +20,7 @@ func MaxLength(l int) func(string) error {
 func MinLength(l int) func(string) error {
 	return func(value string) error {
 		if len(value) < l {
-			return ErrTooFewChars
+			return ErrMustBeLonger.With(l)
 		}
 		return nil
 	}

+ 2 - 2
length_test.go

@@ -16,7 +16,7 @@ func TestMaxLength(t *testing.T) {
 		{Input: "abcd", L: 8},
 		{Input: "abcdefgh", L: 8},
 		{Input: "abcd efg", L: 8},
-		{Input: "abcdefghi", L: 8, Err: ErrTooManyChars},
+		{Input: "abcdefghi", L: 8, Err: ErrMustBeShorter},
 	}
 
 	for n, tc := range testCases {
@@ -39,7 +39,7 @@ func TestMinLength(t *testing.T) {
 	}
 
 	testCases := []TestCase{
-		{Input: "abcd", L: 8, Err: ErrTooFewChars},
+		{Input: "abcd", L: 8, Err: ErrMustBeLonger},
 		{Input: "abcdefgh", L: 8},
 		{Input: "abcd efg", L: 8},
 		{Input: "abcdefghi", L: 8},

+ 4 - 8
size.go

@@ -1,20 +1,16 @@
 package validate
 
-import (
-	"errors"
-)
-
 // Validation error.
 var (
-	ErrTooFewItems  = errors.New("too few items")
-	ErrTooManyItems = errors.New("too many items")
+	ErrMustHaveMoreItems  = NewError("must have at least %d items")
+	ErrMustHaveFewerItems = NewError("must have no more than %d items")
 )
 
 // MaxSize validates the length of a slice as being less than or equal to a given maximum.
 func MaxSize[T any](l int) func([]T) error {
 	return func(value []T) error {
 		if len(value) > l {
-			return ErrTooManyItems
+			return ErrMustHaveFewerItems
 		}
 		return nil
 	}
@@ -24,7 +20,7 @@ func MaxSize[T any](l int) func([]T) error {
 func MinSize[T any](l int) func([]T) error {
 	return func(value []T) error {
 		if len(value) < l {
-			return ErrTooFewItems
+			return ErrMustHaveMoreItems
 		}
 		return nil
 	}

+ 2 - 2
size_test.go

@@ -15,7 +15,7 @@ func TestMaxSize(t *testing.T) {
 	testCases := []TestCase{
 		{Input: []int{1, 2, 3, 4}, L: 8},
 		{Input: []int{1, 2, 3, 4, 5, 6, 7, 8}, L: 8},
-		{Input: []int{1, 2, 3, 4, 5, 6, 7, 8, 9}, L: 8, Err: ErrTooManyItems},
+		{Input: []int{1, 2, 3, 4, 5, 6, 7, 8, 9}, L: 8, Err: ErrMustHaveFewerItems},
 	}
 
 	for n, tc := range testCases {
@@ -38,7 +38,7 @@ func TestMinSize(t *testing.T) {
 	}
 
 	testCases := []TestCase{
-		{Input: []int{1, 2, 3, 4}, L: 8, Err: ErrTooFewItems},
+		{Input: []int{1, 2, 3, 4}, L: 8, Err: ErrMustHaveMoreItems},
 		{Input: []int{1, 2, 3, 4, 5, 6, 7, 8}, L: 8},
 		{Input: []int{1, 2, 3, 4, 5, 6, 7, 8, 9}, L: 8},
 	}

+ 1 - 2
uuid.go

@@ -1,13 +1,12 @@
 package validate
 
 import (
-	"errors"
 	"regexp"
 )
 
 // Validation error.
 var (
-	ErrInvalidUUID = errors.New("invalid UUID")
+	ErrInvalidUUID = NewError("invalid UUID")
 )
 
 var uuidRegexp = regexp.MustCompile("^[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}$")