Documentation
¶
Overview ¶
Package validators provides built-in validation rules and plugin architecture.
The validators package contains built-in validation rules for common types (string, int, slice, bool) and a plugin architecture for extensible validation domains. It provides both built-in validation rules and a plugin architecture that allows domain-specific validators (email, UUID, ULID) to be registered and used seamlessly with the main validation system. The package is designed to be both comprehensive for common use cases and extensible for domain-specific validation requirements.
Index ¶
- func BuildIntValidator(iv *IntValidators, rules []string, intType string) (func(any) error, error)
- func BuildSliceValidator(sv *SliceValidators, rules []string) (func(any) error, error)
- func BuildStringValidator(sv *StringValidators, rules []string) (func(any) error, error)
- func GetSupportedTypes() []string
- type BoolValidator
- type BoolValidatorFactory
- type BoolValidatorImpl
- type BoolValidators
- type IntValidator
- type IntValidatorFactory
- type IntValidatorImpl
- type IntValidators
- func (iv *IntValidators) MaxInt(max int64) IntValidator
- func (iv *IntValidators) MinInt(min int64) IntValidator
- func (iv *IntValidators) Translator() translator.Translator
- func (iv *IntValidators) WithInt(validators ...IntValidator) func(any) error
- func (iv *IntValidators) WithInt64(validators ...IntValidator) func(any) error
- type Registry
- type SliceValidator
- type SliceValidators
- func (sv *SliceValidators) ForEach(elementValidator func(any) error) SliceValidator
- func (sv *SliceValidators) MaxSliceLength(n int) SliceValidator
- func (sv *SliceValidators) MinSliceLength(n int) SliceValidator
- func (sv *SliceValidators) SliceLength(n int) SliceValidator
- func (sv *SliceValidators) Translator() translator.Translator
- func (sv *SliceValidators) WithSlice(validators ...SliceValidator) func(any) error
- type StringValidator
- type StringValidatorFactory
- type StringValidatorImpl
- type StringValidators
- func (sv *StringValidators) Length(n int) StringValidator
- func (sv *StringValidators) MaxLength(n int) StringValidator
- func (sv *StringValidators) MaxRunes(n int) StringValidator
- func (sv *StringValidators) MinLength(n int) StringValidator
- func (sv *StringValidators) MinRunes(n int) StringValidator
- func (sv *StringValidators) OneOf(values ...string) StringValidator
- func (sv *StringValidators) Regex(pattern string) StringValidator
- func (sv *StringValidators) Translator() translator.Translator
- func (sv *StringValidators) WithString(validators ...StringValidator) func(any) error
- type Validator
- type ValidatorFactory
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildIntValidator ¶
func BuildIntValidator( iv *IntValidators, rules []string, intType string, ) (func(any) error, error)
BuildIntValidator builds an integer validator from tokens. rules[0] == "int" or "int64".
Parameters:
- iv: IntValidators instance for creating validators.
- rules: Slice of rule tokens.
- intType: The integer type ("int" or "int64").
Returns:
- func(any) error: The compiled validator function.
- error: An error if the rules are invalid.
func BuildSliceValidator ¶
func BuildSliceValidator( sv *SliceValidators, rules []string, ) (func(any) error, error)
BuildSliceValidator builds a composite slice validator from tag tokens. Example: "slice;min=1;max=5".
func BuildStringValidator ¶
func BuildStringValidator( sv *StringValidators, rules []string, ) (func(any) error, error)
BuildStringValidator builds a composite string validator from tokens. Expected tag: "string;min=3;max=10;regex=^a.*z$".
func GetSupportedTypes ¶
func GetSupportedTypes() []string
GetSupportedTypes returns all supported validator types from the default registry.
Types ¶
type BoolValidator ¶
BoolValidator is a function that validates a bool.
This type represents a validation function that takes a boolean value and returns an error if validation fails.
type BoolValidatorFactory ¶
type BoolValidatorFactory struct{}
BoolValidatorFactory creates boolean validators.
func (*BoolValidatorFactory) CreateValidator ¶
func (f *BoolValidatorFactory) CreateValidator(t translator.Translator) Validator
CreateValidator creates a new boolean validator instance.
type BoolValidatorImpl ¶
type BoolValidatorImpl struct {
// contains filtered or unexported fields
}
BoolValidatorImpl implements the Validator interface for boolean validation.
func (*BoolValidatorImpl) AddRule ¶
func (v *BoolValidatorImpl) AddRule(rule types.Rule)
AddRule adds a single rule to the validator.
func (*BoolValidatorImpl) Build ¶
func (v *BoolValidatorImpl) Build() func(any) error
Build creates a validation function from the accumulated rules.
func (*BoolValidatorImpl) GetRules ¶
func (v *BoolValidatorImpl) GetRules() []types.Rule
GetRules returns the current rules.
func (*BoolValidatorImpl) SetRules ¶
func (v *BoolValidatorImpl) SetRules(rules []types.Rule)
SetRules sets the rules for this validator.
type BoolValidators ¶
type BoolValidators struct {
// contains filtered or unexported fields
}
BoolValidators provides boolean validation methods.
Fields:
- translator: Optional translator for localized error messages.
func NewBoolValidators ¶
func NewBoolValidators(t translator.Translator) *BoolValidators
NewBoolValidators creates a new BoolValidators instance.
Parameters:
- t: Optional translator for localized error messages.
Returns:
- *BoolValidators: A new BoolValidators instance.
func (*BoolValidators) Translator ¶
func (bv *BoolValidators) Translator() translator.Translator
Translator returns the translator instance.
Returns:
- translator.Translator: The translator instance.
func (*BoolValidators) WithBool ¶
func (bv *BoolValidators) WithBool( validators ...BoolValidator, ) func(value any) error
WithBool is a function that validates a bool.
Parameters:
- validators: Variable number of boolean validators to apply.
Returns:
- func(any) error: A validator function that validates any value.
type IntValidator ¶
IntValidator defines a function that validates an int64.
This type represents a validation function that takes an int64 value and returns an error if validation fails.
type IntValidatorFactory ¶
type IntValidatorFactory struct{}
IntValidatorFactory creates integer validators.
func (*IntValidatorFactory) CreateValidator ¶
func (f *IntValidatorFactory) CreateValidator(t translator.Translator) Validator
CreateValidator creates a new integer validator instance.
type IntValidatorImpl ¶
type IntValidatorImpl struct {
// contains filtered or unexported fields
}
IntValidatorImpl implements the Validator interface for integer validation.
func (*IntValidatorImpl) AddRule ¶
func (v *IntValidatorImpl) AddRule(rule types.Rule)
AddRule adds a single rule to the validator.
func (*IntValidatorImpl) Build ¶
func (v *IntValidatorImpl) Build() func(any) error
Build creates a validation function from the accumulated rules.
func (*IntValidatorImpl) GetRules ¶
func (v *IntValidatorImpl) GetRules() []types.Rule
GetRules returns the current rules.
func (*IntValidatorImpl) SetExact ¶
func (v *IntValidatorImpl) SetExact(exact bool)
SetExact sets whether this validator should be exact (int64) or not (int).
func (*IntValidatorImpl) SetRules ¶
func (v *IntValidatorImpl) SetRules(rules []types.Rule)
SetRules sets the rules for this validator.
type IntValidators ¶
type IntValidators struct {
// contains filtered or unexported fields
}
IntValidators provides integer validation methods.
Fields:
- translator: Optional translator for localized error messages.
func NewIntValidators ¶
func NewIntValidators( t translator.Translator, ) *IntValidators
NewIntValidators creates a new IntValidators instance.
Parameters:
- t: Optional translator for localized error messages.
Returns:
- *IntValidators: A new IntValidators instance.
func (*IntValidators) MaxInt ¶
func (iv *IntValidators) MaxInt(max int64) IntValidator
MaxInt returns a validator that checks for maximum integer value.
Parameters:
- max: The maximum integer value allowed.
Returns:
- IntValidator: A validator function that checks maximum value.
func (*IntValidators) MinInt ¶
func (iv *IntValidators) MinInt(min int64) IntValidator
MinInt returns a validator that checks for minimum integer value.
Parameters:
- min: The minimum integer value allowed.
Returns:
- IntValidator: A validator function that checks minimum value.
func (*IntValidators) Translator ¶
func (iv *IntValidators) Translator() translator.Translator
Translator returns the translator instance.
Returns:
- translator.Translator: The translator instance.
func (*IntValidators) WithInt ¶
func (iv *IntValidators) WithInt( validators ...IntValidator, ) func(any) error
WithInt applies IntValidators converting the value to int64 first.
Parameters:
- validators: Variable number of integer validators to apply.
Returns:
- func(any) error: A validator function that validates any value.
func (*IntValidators) WithInt64 ¶
func (iv *IntValidators) WithInt64( validators ...IntValidator, ) func(any) error
WithInt64 applies IntValidators requiring exactly int64.
Parameters:
- validators: Variable number of integer validators to apply.
Returns:
- func(any) error: A validator function that validates int64 values.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry manages validator implementations and provides a way to register and retrieve validators without the core engine knowing about specific types.
func DefaultRegistry ¶
func DefaultRegistry() *Registry
DefaultRegistry returns a registry with all built-in validators registered.
func (*Registry) GetSupportedTypes ¶
GetSupportedTypes returns a list of all registered validator types.
func (*Registry) GetValidator ¶
func (r *Registry) GetValidator(name string, translator translator.Translator) (Validator, bool)
GetValidator creates a new validator instance for the given type.
func (*Registry) RegisterValidator ¶
func (r *Registry) RegisterValidator(name string, factory ValidatorFactory)
RegisterValidator registers a validator factory for a given type name.
type SliceValidator ¶
SliceValidator validates a slice presented as []any.
This type represents a validation function that takes a slice of any values and returns an error if validation fails.
type SliceValidators ¶
type SliceValidators struct {
// contains filtered or unexported fields
}
SliceValidators provides slice validation methods.
Fields:
- translator: Optional translator for localized error messages.
func NewSliceValidators ¶
func NewSliceValidators( t translator.Translator, ) *SliceValidators
NewSliceValidators creates a new SliceValidators instance.
Parameters:
- t: Optional translator for localized error messages.
Returns:
- *SliceValidators: A new SliceValidators instance.
func (*SliceValidators) ForEach ¶
func (sv *SliceValidators) ForEach( elementValidator func(any) error, ) SliceValidator
ForEach applies an element validator to every element in the slice.
func (*SliceValidators) MaxSliceLength ¶
func (sv *SliceValidators) MaxSliceLength(n int) SliceValidator
MaxSliceLength returns a validator that checks for maximum slice length.
Parameters:
- n: The maximum length the slice can have.
Returns:
- SliceValidator: A validator function that checks maximum length.
func (*SliceValidators) MinSliceLength ¶
func (sv *SliceValidators) MinSliceLength(n int) SliceValidator
MinSliceLength returns a validator that checks for minimum slice length.
Parameters:
- n: The minimum length the slice must have.
Returns:
- SliceValidator: A validator function that checks minimum length.
func (*SliceValidators) SliceLength ¶
func (sv *SliceValidators) SliceLength(n int) SliceValidator
SliceLength returns a validator that checks for exact slice length.
Parameters:
- n: The exact length the slice must have.
Returns:
- SliceValidator: A validator function that checks exact length.
func (*SliceValidators) Translator ¶
func (sv *SliceValidators) Translator() translator.Translator
Translator returns the translator instance.
Returns:
- translator.Translator: The translator instance.
func (*SliceValidators) WithSlice ¶
func (sv *SliceValidators) WithSlice( validators ...SliceValidator, ) func(any) error
WithSlice applies validators to any slice type via reflection.
Parameters:
- validators: Variable number of slice validators to apply.
Returns:
- func(any) error: A validator function that validates any slice value.
type StringValidator ¶
StringValidator defines a function that validates a string.
This type represents a validation function that takes a string value and returns an error if validation fails.
type StringValidatorFactory ¶
type StringValidatorFactory struct{}
StringValidatorFactory creates string validators.
func (*StringValidatorFactory) CreateValidator ¶
func (f *StringValidatorFactory) CreateValidator(t translator.Translator) Validator
CreateValidator creates a new string validator instance.
type StringValidatorImpl ¶
type StringValidatorImpl struct {
// contains filtered or unexported fields
}
StringValidatorImpl implements the Validator interface for string validation.
func (*StringValidatorImpl) AddRule ¶
func (v *StringValidatorImpl) AddRule(rule types.Rule)
AddRule adds a single rule to the validator.
func (*StringValidatorImpl) Build ¶
func (v *StringValidatorImpl) Build() func(any) error
Build creates a validation function from the accumulated rules.
func (*StringValidatorImpl) GetRules ¶
func (v *StringValidatorImpl) GetRules() []types.Rule
GetRules returns the current rules.
func (*StringValidatorImpl) SetRules ¶
func (v *StringValidatorImpl) SetRules(rules []types.Rule)
SetRules sets the rules for this validator.
type StringValidators ¶
type StringValidators struct {
// contains filtered or unexported fields
}
StringValidators provides string validation methods.
Fields:
- translator: Optional translator for localized error messages.
func NewStringValidators ¶
func NewStringValidators( t translator.Translator, ) *StringValidators
NewStringValidators creates a new StringValidators instance.
Parameters:
- t: Optional translator for localized error messages.
Returns:
- *StringValidators: A new StringValidators instance.
func (*StringValidators) Length ¶
func (sv *StringValidators) Length(n int) StringValidator
Length returns a validator that checks for exact length.
Parameters:
- n: The exact length the string must have.
Returns:
- StringValidator: A validator function that checks exact length.
func (*StringValidators) MaxLength ¶
func (sv *StringValidators) MaxLength(n int) StringValidator
MaxLength returns a validator that checks for maximum length.
Parameters:
- n: The maximum length the string can have.
Returns:
- StringValidator: A validator function that checks maximum length.
func (*StringValidators) MaxRunes ¶
func (sv *StringValidators) MaxRunes(n int) StringValidator
MaxRunes returns a validator that checks for maximum number of Unicode runes.
Parameters:
- n: The maximum number of runes the string can have.
Returns:
- StringValidator: A validator function that checks maximum rune count.
func (*StringValidators) MinLength ¶
func (sv *StringValidators) MinLength(n int) StringValidator
MinLength returns a validator that checks for minimum length.
Parameters:
- n: The minimum length the string must have.
Returns:
- StringValidator: A validator function that checks minimum length.
func (*StringValidators) MinRunes ¶
func (sv *StringValidators) MinRunes(n int) StringValidator
MinRunes returns a validator that checks for minimum number of Unicode runes.
Parameters:
- n: The minimum number of runes the string must have.
Returns:
- StringValidator: A validator function that checks minimum rune count.
func (*StringValidators) OneOf ¶
func (sv *StringValidators) OneOf( values ...string, ) StringValidator
OneOf returns a validator that checks if the string is one of the specified values.
Parameters:
- values: Variable number of allowed string values.
Returns:
- StringValidator: A validator function that checks if string is in the allowed values.
func (*StringValidators) Regex ¶
func (sv *StringValidators) Regex(pattern string) StringValidator
Regex returns a validator that ensures the string matches the pattern. It includes safety measures against catastrophic backtracking and enforces reasonable input length limits.
func (*StringValidators) Translator ¶
func (sv *StringValidators) Translator() translator.Translator
Translator returns the translator instance.
Returns:
- translator.Translator: The translator instance.
func (*StringValidators) WithString ¶
func (sv *StringValidators) WithString( validators ...StringValidator, ) func(any) error
WithString applies a series of string validators to a value.
Parameters:
- validators: Variable number of string validators to apply.
Returns:
- func(any) error: A validator function that validates any value.
type Validator ¶
type Validator interface {
// Build creates a validation function from the accumulated rules.
Build() func(any) error
// GetRules returns the current rules.
GetRules() []types.Rule
// SetRules sets the rules for this validator.
SetRules(rules []types.Rule)
}
Validator defines the interface that all validators must implement.
func CreateValidator ¶
func CreateValidator(typeName string, translator translator.Translator) (Validator, bool)
CreateValidator creates a validator for the given type using the default registry.
type ValidatorFactory ¶
type ValidatorFactory interface {
CreateValidator(translator translator.Translator) Validator
}
ValidatorFactory creates validator instances for a specific type.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package email provides email address validation as a plugin.
|
Package email provides email address validation as a plugin. |
|
Package ulid provides ULID validation as a plugin.
|
Package ulid provides ULID validation as a plugin. |
|
Package uuid provides UUID validation as a plugin.
|
Package uuid provides UUID validation as a plugin. |