mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
thanks for the donations ❤️
This commit is contained in:
@@ -6,16 +6,16 @@ import (
|
||||
"golang.org/x/exp/constraints"
|
||||
)
|
||||
|
||||
// NumberValue is a type constraint that accepts any numeric type.
|
||||
type NumberValue interface {
|
||||
// NumberType is a type constraint that accepts any numeric type.
|
||||
type NumberType interface {
|
||||
constraints.Integer | constraints.Float
|
||||
}
|
||||
|
||||
// NumberError describes a number field validation error.
|
||||
type NumberError[T NumberValue] struct{ *FieldError[T] }
|
||||
type NumberError[T NumberType] struct{ *FieldError[T] }
|
||||
|
||||
// Number returns a new number validation error.
|
||||
func Number[T NumberValue](field string, value T) *NumberError[T] {
|
||||
func Number[T NumberType](field string, value T) *NumberError[T] {
|
||||
return &NumberError[T]{Field(field, value)}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ func (e *NumberError[T]) InRange(min, max T) *NumberError[T] {
|
||||
|
||||
// Positive accepts any numeric type and
|
||||
// returns a message if the value is not positive.
|
||||
func Positive[T NumberValue](n T) string {
|
||||
func Positive[T NumberType](n T) string {
|
||||
if n <= 0 {
|
||||
return "must be positive"
|
||||
}
|
||||
@@ -60,7 +60,7 @@ func Positive[T NumberValue](n T) string {
|
||||
}
|
||||
|
||||
// Negative accepts any numeric type and returns a message if the value is not negative.
|
||||
func Negative[T NumberValue](n T) string {
|
||||
func Negative[T NumberType](n T) string {
|
||||
if n >= 0 {
|
||||
return "must be negative"
|
||||
}
|
||||
@@ -69,7 +69,7 @@ func Negative[T NumberValue](n T) string {
|
||||
}
|
||||
|
||||
// Zero accepts any numeric type and returns a message if the value is not zero.
|
||||
func Zero[T NumberValue](n T) string {
|
||||
func Zero[T NumberType](n T) string {
|
||||
if n != 0 {
|
||||
return "must be zero"
|
||||
}
|
||||
@@ -78,7 +78,7 @@ func Zero[T NumberValue](n T) string {
|
||||
}
|
||||
|
||||
// NonZero accepts any numeric type and returns a message if the value is not zero.
|
||||
func NonZero[T NumberValue](n T) string {
|
||||
func NonZero[T NumberType](n T) string {
|
||||
if n == 0 {
|
||||
return "must not be zero"
|
||||
}
|
||||
@@ -87,7 +87,7 @@ func NonZero[T NumberValue](n T) string {
|
||||
}
|
||||
|
||||
// InRange accepts any numeric type and returns a message if the value is not in the range.
|
||||
func InRange[T NumberValue](min, max T) func(T) string {
|
||||
func InRange[T NumberType](min, max T) func(T) string {
|
||||
return func(n T) string {
|
||||
if n < min || n > max {
|
||||
return "must be in range of " + FormatRange(min, max)
|
||||
@@ -100,6 +100,6 @@ func InRange[T NumberValue](min, max T) func(T) string {
|
||||
// FormatRange returns a string representation of a range of values, such as "[1, 10]".
|
||||
// It uses a type constraint NumberValue, which means that the parameters must be numeric types
|
||||
// that support comparison and formatting operations.
|
||||
func FormatRange[T NumberValue](min, max T) string {
|
||||
func FormatRange[T NumberType](min, max T) string {
|
||||
return fmt.Sprintf("[%v, %v]", min, max)
|
||||
}
|
||||
|
||||
@@ -2,15 +2,16 @@ package validation
|
||||
|
||||
import "fmt"
|
||||
|
||||
type SliceValue[T any] interface {
|
||||
// SliceType is a type constraint that accepts any slice type.
|
||||
type SliceType[T any] interface {
|
||||
~[]T
|
||||
}
|
||||
|
||||
// SliceError describes a slice field validation error.
|
||||
type SliceError[T any, V SliceValue[T]] struct{ *FieldError[V] }
|
||||
type SliceError[T any, V SliceType[T]] struct{ *FieldError[V] }
|
||||
|
||||
// Slice returns a new slice validation error.
|
||||
func Slice[T any, V SliceValue[T]](field string, value V) *SliceError[T, V] {
|
||||
func Slice[T any, V SliceType[T]](field string, value V) *SliceError[T, V] {
|
||||
return &SliceError[T, V]{Field(field, value)}
|
||||
}
|
||||
|
||||
@@ -27,7 +28,7 @@ func (e *SliceError[T, V]) Length(min, max int) *SliceError[T, V] {
|
||||
}
|
||||
|
||||
// NotEmptySlice accepts any slice and returns a message if the value is empty.
|
||||
func NotEmptySlice[T any, V SliceValue[T]](s V) string {
|
||||
func NotEmptySlice[T any, V SliceType[T]](s V) string {
|
||||
if len(s) == 0 {
|
||||
return "must not be empty"
|
||||
}
|
||||
@@ -36,7 +37,7 @@ func NotEmptySlice[T any, V SliceValue[T]](s V) string {
|
||||
}
|
||||
|
||||
// SliceLength accepts any slice and returns a message if the length is not in the given range.
|
||||
func SliceLength[T any, V SliceValue[T]](min, max int) func(s V) string {
|
||||
func SliceLength[T any, V SliceType[T]](min, max int) func(s V) string {
|
||||
return func(s V) string {
|
||||
n := len(s)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user