Audit ahead of the README/doc.go work (issue #3): - number.go: Min, MinFloat32, MinFloat64 all said "less than or equal to a given maximum" -- copy-pasted from the Max family. They check the minimum. Fixed. - chars_test.go: ExampleExceptChars called Chars, not ExceptChars, so it never exercised the function it documents. - chars.go: "does not contain disallowed characters" was circular. - in.go: NotIn's parameter was named "allow"; renamed to "disallow". - length.go: note that length is bytes (len), not runes -- the "%d characters" messages imply otherwise. - url.go: spell out that ParseRequestURI wants an absolute URL or path. - uuid.go: note lowercase-hex only, version/variant not checked. - error.go: explain Err (the match-anything sentinel), how Error() formats with Data, and what With does. all.go: point at Collect. Comments only; no behaviour change. go vet + go test ./... pass. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
105 lines
2.6 KiB
Go
105 lines
2.6 KiB
Go
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")
|
|
)
|
|
|
|
// Max validates whether an integer is less than or equal to a given maximum.
|
|
// If exclusive is true, an equal value also produces an error.
|
|
func Max(n int, exclusive bool) func(int) error {
|
|
return func(value int) error {
|
|
if exclusive {
|
|
if value >= n {
|
|
return ErrMustBeLess.With(n)
|
|
}
|
|
}
|
|
if value > n {
|
|
return ErrMustBeLessOrEqual.With(n)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// MaxFloat32 validates whether a float32 is less than or equal to a given maximum.
|
|
// If exclusive is true, an equal value also produces an error.
|
|
func MaxFloat32(n float32, exclusive bool) func(float32) error {
|
|
return func(value float32) error {
|
|
if exclusive {
|
|
if value >= n {
|
|
return ErrMustBeLess.With(n)
|
|
}
|
|
}
|
|
if value > n {
|
|
return ErrMustBeLessOrEqual.With(n)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// MaxFloat64 validates whether a float64 is less than or equal to a given maximum.
|
|
// If exclusive is true, an equal value also produces an error.
|
|
func MaxFloat64(n float64, exclusive bool) func(float64) error {
|
|
return func(value float64) error {
|
|
if exclusive {
|
|
if value >= n {
|
|
return ErrMustBeLess.With(n)
|
|
}
|
|
}
|
|
if value > n {
|
|
return ErrMustBeLessOrEqual.With(n)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// Min validates whether an integer is greater than or equal to a given minimum.
|
|
// If exclusive is true, an equal value also produces an error.
|
|
func Min(n int, exclusive bool) func(int) error {
|
|
return func(value int) error {
|
|
if exclusive {
|
|
if value <= n {
|
|
return ErrMustBeGreater.With(n)
|
|
}
|
|
}
|
|
if value < n {
|
|
return ErrMustBeGreaterOrEqual.With(n)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// MinFloat32 validates whether a float32 is greater than or equal to a given minimum.
|
|
// If exclusive is true, an equal value also produces an error.
|
|
func MinFloat32(n float32, exclusive bool) func(float32) error {
|
|
return func(value float32) error {
|
|
if exclusive {
|
|
if value <= n {
|
|
return ErrMustBeGreater.With(n)
|
|
}
|
|
}
|
|
if value < n {
|
|
return ErrMustBeGreaterOrEqual.With(n)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// MinFloat64 validates whether a float64 is greater than or equal to a given minimum.
|
|
// If exclusive is true, an equal value also produces an error.
|
|
func MinFloat64(n float64, exclusive bool) func(float64) error {
|
|
return func(value float64) error {
|
|
if exclusive {
|
|
if value <= n {
|
|
return ErrMustBeGreater.With(n)
|
|
}
|
|
}
|
|
if value < n {
|
|
return ErrMustBeGreaterOrEqual.With(n)
|
|
}
|
|
return nil
|
|
}
|
|
}
|