22 lines
479 B
Go
22 lines
479 B
Go
package validate
|
|||
|
|
|
||
|
|
var (
|
||
|
|
ErrRequired = NewError("is required")
|
||
|
|
)
|
||
|
|
|
||
|
|
// Required validates that a value is not the zero value for its type: an
|
||
|
|
// empty string, 0, a nil pointer, and so on.
|
||
|
|
//
|
||
|
|
// It is defined for comparable types only. For slices and maps, use
|
||
|
|
// MinSize(1); for "non-whitespace", combine with a trimming step.
|
||
|
|
func Required[T comparable]() func(T) error {
|
||
|
|
var zero T
|
||
|
|
|
||
|
|
return func(value T) error {
|
||
|
|
if value == zero {
|
||
|
|
return ErrRequired
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
}
|