Documentation
¶
Overview ¶
Package validation provides functional composition for validation rules using the HasValidation interface pattern.
The core principle is that all validators implement the HasValidation interface with a single method:
Validate(ctx context.Context) error
This allows for consistent composition of validation rules using logical operators like All and Any.
Basic usage:
validator := validation.All{
validation.NotNil(user),
validation.NotEmptyString(user.Name),
validation.LengthGe(user.Email, 1),
}
if err := validator.Validate(ctx); err != nil {
// Handle validation error
}
The package provides validators for common scenarios:
- Basic validators: NotNil, Nil, True, False, Equal
- String/slice validators: NotEmptyString, NotEmptySlice
- Length validators: LengthEqual, LengthGt, LengthGe, LengthLt, LengthLe
- Logical operators: All, Any, Either, Not
- Conditional validators: NilOrValid, NotNilAndValid
All validation errors are wrapped using github.com/bborbe/errors for consistent error handling.
Index ¶
- Variables
- type All
- type Any
- type Either
- type HasValidation
- func Equal[T comparable](value T, expected T) HasValidation
- func False(value bool) HasValidation
- func LengthEqual[T any](list []T, expectedLength int) HasValidation
- func LengthGe[T any](list []T, expectedLength int) HasValidation
- func LengthGt[T any](list []T, expectedLength int) HasValidation
- func LengthLe[T any](list []T, expectedLength int) HasValidation
- func LengthLt[T any](list []T, expectedLength int) HasValidation
- func Name(fieldname string, validation HasValidation) HasValidation
- func Nil(value any) HasValidation
- func NilOrValid(validation HasValidation) HasValidation
- func Not(hasValidation HasValidation) HasValidation
- func NotEmptySlice[T any](values []T) HasValidation
- func NotEmptyString[T ~string](value T) HasValidation
- func NotNil(value any) HasValidation
- func NotNilAndValid(value HasValidation) HasValidation
- func True(value bool) HasValidation
- type HasValidationFunc
Constants ¶
This section is empty.
Variables ¶
var Error = stderrors.New("validation error")
Error is the base error used by all validation functions. All validation errors are wrapped using this error for consistent error handling.
Functions ¶
This section is empty.
Types ¶
type All ¶
type All []HasValidation
All represents a logical AND operation for multiple validators. All validators must pass for the validation to succeed.
type Any ¶
type Any []HasValidation
Any represents a logical OR operation for multiple validators. At least one validator must pass for the validation to succeed.
type Either ¶
type Either []HasValidation
Either represents an exclusive OR operation for multiple validators. Exactly one validator must pass for the validation to succeed.
type HasValidation ¶
type HasValidation interface {
// Validate performs validation logic and returns an error if validation fails.
// The context can be used for cancellation and timeout handling.
Validate(ctx context.Context) error
}
HasValidation defines the interface that all validators must implement. It provides a single method for validating data with context support.
func Equal ¶
func Equal[T comparable](value T, expected T) HasValidation
Equal validates that the value equals the expected value. It uses Go's built-in equality comparison for comparable types. It returns an error if the values don't match, otherwise returns nil.
func False ¶
func False(value bool) HasValidation
False validates that the boolean value is false. It returns an error if the value is true, otherwise returns nil.
func LengthEqual ¶ added in v1.1.0
func LengthEqual[T any](list []T, expectedLength int) HasValidation
LengthEqual validates that the slice length equals the expected length. It returns an error if the lengths don't match, otherwise returns nil.
func LengthGe ¶ added in v1.1.0
func LengthGe[T any](list []T, expectedLength int) HasValidation
LengthGe validates that the slice length is greater than or equal to the expected length. It returns an error if the slice is shorter, otherwise returns nil.
func LengthGt ¶ added in v1.1.0
func LengthGt[T any](list []T, expectedLength int) HasValidation
LengthGt validates that the slice length is greater than the expected length. It returns an error if the slice is not longer, otherwise returns nil.
func LengthLe ¶ added in v1.1.0
func LengthLe[T any](list []T, expectedLength int) HasValidation
LengthLe validates that the slice length is less than or equal to the expected length. It returns an error if the slice is longer, otherwise returns nil.
func LengthLt ¶ added in v1.1.0
func LengthLt[T any](list []T, expectedLength int) HasValidation
LengthLt validates that the slice length is less than the expected length. It returns an error if the slice is not shorter, otherwise returns nil.
func Name ¶
func Name(fieldname string, validation HasValidation) HasValidation
Name wraps a validator with a field name for better error messages. It executes the wrapped validator and includes the field name in any error. This is useful for providing context about which field failed validation.
func Nil ¶
func Nil(value any) HasValidation
Nil validates that the given value is nil. It handles both nil interfaces and nil pointers correctly. It returns an error if the value is not nil, otherwise returns nil.
func NilOrValid ¶
func NilOrValid(validation HasValidation) HasValidation
NilOrValid validates that the value is either nil or passes the provided validation. It succeeds if the validator itself is nil OR if the validator passes its validation. This is useful for optional fields that should be validated only when present.
func Not ¶
func Not(hasValidation HasValidation) HasValidation
Not inverts the result of a validator. It returns nil if the wrapped validator fails, and an error if it succeeds. Non-validation errors are passed through unchanged.
func NotEmptySlice ¶ added in v1.2.0
func NotEmptySlice[T any](values []T) HasValidation
NotEmptySlice validates that the slice is not empty. It accepts slices of any type and checks that the length is greater than zero. It returns an error if the slice is empty, otherwise returns nil.
func NotEmptyString ¶ added in v1.2.0
func NotEmptyString[T ~string](value T) HasValidation
NotEmptyString validates that the string value is not empty. It accepts any type that has string as its underlying type. It returns an error if the string is empty, otherwise returns nil.
func NotNil ¶
func NotNil(value any) HasValidation
NotNil validates that the given value is not nil. It returns an error if the value is nil, otherwise returns nil.
func NotNilAndValid ¶
func NotNilAndValid(value HasValidation) HasValidation
NotNilAndValid validates that the value is not nil AND passes the provided validation. It ensures the validator is not nil before executing the validation logic. This is useful for required fields that must be present and valid.
func True ¶
func True(value bool) HasValidation
True validates that the boolean value is true. It returns an error if the value is false, otherwise returns nil.
type HasValidationFunc ¶
HasValidationFunc is a function type that implements the HasValidation interface. It allows functions to be used as validators without creating custom types.
Source Files
¶
- doc.go
- validation_all.go
- validation_any.go
- validation_either.go
- validation_equal.go
- validation_error.go
- validation_false.go
- validation_has-validation.go
- validation_length.go
- validation_name.go
- validation_nil-or-valid.go
- validation_nil.go
- validation_not-empty-splice.go
- validation_not-empty-string.go
- validation_not-nil-and-valid.go
- validation_not-nil.go
- validation_not.go
- validation_true.go