update all validation tests

This commit is contained in:
Aneurin Barker Snook
2023-11-19 20:42:48 +00:00
parent db902e3c1c
commit 40482060ac
10 changed files with 24 additions and 32 deletions
+2 -2
View File
@@ -23,8 +23,8 @@ func TestAll(t *testing.T) {
{Input: "abcd", F: f}, {Input: "abcd", F: f},
{Input: "abcdef", F: f}, {Input: "abcdef", F: f},
{Input: "12345678", F: f}, {Input: "12345678", F: f},
{Input: "abc", F: f, Err: ErrTooFewChars}, {Input: "abc", F: f, Err: ErrMustBeLonger},
{Input: "abcdef012", F: f, Err: ErrTooManyChars}, {Input: "abcdef012", F: f, Err: ErrMustBeShorter},
{Input: "abcdefgh", F: f, Err: ErrDisallowedChars}, {Input: "abcdefgh", F: f, Err: ErrDisallowedChars},
{Input: "01abcd", F: f, Err: ErrValueNotAllowed}, {Input: "01abcd", F: f, Err: ErrValueNotAllowed},
} }
+1 -2
View File
@@ -1,13 +1,12 @@
package validate package validate
import ( import (
"errors"
"regexp" "regexp"
) )
// Validation error. // Validation error.
var ( var (
ErrInvalidEmail = errors.New("invalid email address") ErrInvalidEmail = NewError("invalid email address")
) )
// Based on https://stackoverflow.com/a/201378 // Based on https://stackoverflow.com/a/201378
+5 -1
View File
@@ -1,10 +1,14 @@
package validate package validate
var (
ErrNotEqual = NewError("must be equal to %v")
)
// Equal validates whether an input value is equal to a comparison value. // Equal validates whether an input value is equal to a comparison value.
func Equal[T comparable](cmp T) func(T) error { func Equal[T comparable](cmp T) func(T) error {
return func(value T) error { return func(value T) error {
if value != cmp { if value != cmp {
return ErrValueNotAllowed return ErrNotEqual.With(cmp)
} }
return nil return nil
} }
+2 -2
View File
@@ -15,7 +15,7 @@ func TestEqualInt(t *testing.T) {
testCases := []TestCase{ testCases := []TestCase{
{I: 1, C: 1}, {I: 1, C: 1},
{I: 5 ^ 3, C: 5 ^ 3}, {I: 5 ^ 3, C: 5 ^ 3},
{I: 10, C: 15, Err: ErrValueNotAllowed}, {I: 10, C: 15, Err: ErrNotEqual},
} }
for i, tc := range testCases { for i, tc := range testCases {
@@ -40,7 +40,7 @@ func TestEqualStr(t *testing.T) {
testCases := []TestCase{ testCases := []TestCase{
{I: "abc", C: "abc"}, {I: "abc", C: "abc"},
{I: "def ghi 123", C: "def ghi 123"}, {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 { for i, tc := range testCases {
+1 -3
View File
@@ -1,10 +1,8 @@
package validate package validate
import "errors"
// Validation error. // Validation error.
var ( var (
ErrValueNotAllowed = errors.New("not allowed") ErrValueNotAllowed = NewError("not allowed")
) )
// In validates whether a value is found in a slice of allowed values. // In validates whether a value is found in a slice of allowed values.
+4 -8
View File
@@ -1,20 +1,16 @@
package validate package validate
import (
"errors"
)
// Validation error. // Validation error.
var ( var (
ErrTooFewChars = errors.New("too few characters") ErrMustBeLonger = NewError("must be at least %d characters")
ErrTooManyChars = errors.New("too many 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. // MaxLength validates the length of a string as being less than or equal to a given maximum.
func MaxLength(l int) func(string) error { func MaxLength(l int) func(string) error {
return func(value string) error { return func(value string) error {
if len(value) > l { if len(value) > l {
return ErrTooManyChars return ErrMustBeShorter.With(l)
} }
return nil return nil
} }
@@ -24,7 +20,7 @@ func MaxLength(l int) func(string) error {
func MinLength(l int) func(string) error { func MinLength(l int) func(string) error {
return func(value string) error { return func(value string) error {
if len(value) < l { if len(value) < l {
return ErrTooFewChars return ErrMustBeLonger.With(l)
} }
return nil return nil
} }
+2 -2
View File
@@ -16,7 +16,7 @@ func TestMaxLength(t *testing.T) {
{Input: "abcd", L: 8}, {Input: "abcd", L: 8},
{Input: "abcdefgh", L: 8}, {Input: "abcdefgh", L: 8},
{Input: "abcd efg", L: 8}, {Input: "abcd efg", L: 8},
{Input: "abcdefghi", L: 8, Err: ErrTooManyChars}, {Input: "abcdefghi", L: 8, Err: ErrMustBeShorter},
} }
for n, tc := range testCases { for n, tc := range testCases {
@@ -39,7 +39,7 @@ func TestMinLength(t *testing.T) {
} }
testCases := []TestCase{ testCases := []TestCase{
{Input: "abcd", L: 8, Err: ErrTooFewChars}, {Input: "abcd", L: 8, Err: ErrMustBeLonger},
{Input: "abcdefgh", L: 8}, {Input: "abcdefgh", L: 8},
{Input: "abcd efg", L: 8}, {Input: "abcd efg", L: 8},
{Input: "abcdefghi", L: 8}, {Input: "abcdefghi", L: 8},
+4 -8
View File
@@ -1,20 +1,16 @@
package validate package validate
import (
"errors"
)
// Validation error. // Validation error.
var ( var (
ErrTooFewItems = errors.New("too few items") ErrMustHaveMoreItems = NewError("must have at least %d items")
ErrTooManyItems = errors.New("too many 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. // 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 { func MaxSize[T any](l int) func([]T) error {
return func(value []T) error { return func(value []T) error {
if len(value) > l { if len(value) > l {
return ErrTooManyItems return ErrMustHaveFewerItems
} }
return nil return nil
} }
@@ -24,7 +20,7 @@ func MaxSize[T any](l int) func([]T) error {
func MinSize[T any](l int) func([]T) error { func MinSize[T any](l int) func([]T) error {
return func(value []T) error { return func(value []T) error {
if len(value) < l { if len(value) < l {
return ErrTooFewItems return ErrMustHaveMoreItems
} }
return nil return nil
} }
+2 -2
View File
@@ -15,7 +15,7 @@ func TestMaxSize(t *testing.T) {
testCases := []TestCase{ testCases := []TestCase{
{Input: []int{1, 2, 3, 4}, L: 8}, {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}, 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 { for n, tc := range testCases {
@@ -38,7 +38,7 @@ func TestMinSize(t *testing.T) {
} }
testCases := []TestCase{ 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}, L: 8},
{Input: []int{1, 2, 3, 4, 5, 6, 7, 8, 9}, L: 8}, {Input: []int{1, 2, 3, 4, 5, 6, 7, 8, 9}, L: 8},
} }
+1 -2
View File
@@ -1,13 +1,12 @@
package validate package validate
import ( import (
"errors"
"regexp" "regexp"
) )
// Validation error. // Validation error.
var ( 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}$") var uuidRegexp = regexp.MustCompile("^[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}$")