update all validation tests
This commit is contained in:
+2
-2
@@ -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,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
|
||||
|
||||
@@ -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
@@ -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,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.
|
||||
|
||||
@@ -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
@@ -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},
|
||||
|
||||
@@ -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
@@ -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},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user