mirror of
https://github.com/kataras/iris.git
synced 2025-12-21 20:07:04 +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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user