Files
validate/number.go
T

105 lines
2.7 KiB
Go
Raw Normal View History

2023-11-19 19:33:34 +00:00
package validate
var (
ErrMustBeGreater = NewError("must be greater than %v")
ErrMustBeGreaterOrEqual = NewError("must be greater than or equal to %v")
ErrMustBeLess = NewError("must be less than %v")
ErrMustBeLessOrEqual = NewError("must be less than or equal to %v")
2023-11-19 19:33:34 +00:00
)
// Max validates whether an integer is less than or equal to a given maximum.
// If exclusive is true, an equal value will also produce an error.
2023-11-19 19:33:34 +00:00
func Max(n int, exclusive bool) func(int) error {
return func(value int) error {
if exclusive {
if value >= n {
return ErrMustBeLess.With(n)
2023-11-19 19:33:34 +00:00
}
}
if value > n {
return ErrMustBeLessOrEqual.With(n)
2023-11-19 19:33:34 +00:00
}
return nil
}
}
// MaxFloat32 validates whether a float32 is less than or equal to a given maximum.
// If exclusive is true, an equal value will also produce an error.
2023-11-19 19:33:34 +00:00
func MaxFloat32(n float32, exclusive bool) func(float32) error {
return func(value float32) error {
if exclusive {
if value >= n {
return ErrMustBeLess.With(n)
2023-11-19 19:33:34 +00:00
}
}
if value > n {
return ErrMustBeLessOrEqual.With(n)
2023-11-19 19:33:34 +00:00
}
return nil
}
}
// MaxFloat64 validates whether a float64 is less than or equal to a given maximum.
// If exclusive is true, an equal value will also produce an error.
2023-11-19 19:33:34 +00:00
func MaxFloat64(n float64, exclusive bool) func(float64) error {
return func(value float64) error {
if exclusive {
if value >= n {
return ErrMustBeLess.With(n)
2023-11-19 19:33:34 +00:00
}
}
if value > n {
return ErrMustBeLessOrEqual.With(n)
2023-11-19 19:33:34 +00:00
}
return nil
}
}
// Min validates whether an integer is less than or equal to a given maximum.
// If exclusive is true, an equal value will also produce an error.
2023-11-19 19:33:34 +00:00
func Min(n int, exclusive bool) func(int) error {
return func(value int) error {
if exclusive {
if value <= n {
return ErrMustBeGreater.With(n)
2023-11-19 19:33:34 +00:00
}
}
if value < n {
return ErrMustBeGreaterOrEqual.With(n)
2023-11-19 19:33:34 +00:00
}
return nil
}
}
// MinFloat32 validates whether a float32 is less than or equal to a given maximum.
// If exclusive is true, an equal value will also produce an error.
2023-11-19 19:33:34 +00:00
func MinFloat32(n float32, exclusive bool) func(float32) error {
return func(value float32) error {
if exclusive {
if value <= n {
return ErrMustBeGreater.With(n)
2023-11-19 19:33:34 +00:00
}
}
if value < n {
return ErrMustBeGreaterOrEqual.With(n)
2023-11-19 19:33:34 +00:00
}
return nil
}
}
// MinFloat64 validates whether a float64 is less than or equal to a given maximum.
// If exclusive is true, an equal value will also produce an error.
2023-11-19 19:33:34 +00:00
func MinFloat64(n float64, exclusive bool) func(float64) error {
return func(value float64) error {
if exclusive {
if value <= n {
return ErrMustBeGreater.With(n)
2023-11-19 19:33:34 +00:00
}
}
if value < n {
return ErrMustBeGreaterOrEqual.With(n)
2023-11-19 19:33:34 +00:00
}
return nil
}
}