Documentation
¶
Index ¶
- Constants
- Variables
- func GetValues(model Model) (map[ID]any, error)
- type Config
- type ConfigField
- type Constraint
- type ConstraintSpec
- type Field
- type ID
- type ListConstraint
- type MapKeyConstraintSpec
- type MapKeySpec
- type MapKeysConstraint
- type Model
- type NonEmptyConstraint
- type NonEmptyKeysConstraint
- type NonEmptyValConstraint
- type RegexConstraint
- type Spec
- type Validation
- type Validator
Constants ¶
const ( ConstraintTypeList = "list" ConstraintTypeNonEmpty = "non-empty" ConstraintTypeNonEmptyKeys = "non-empty-keys" ConstraintTypeNonEmptyVals = "non-empty-vals" ConstraintTypeRegex = "regex" ConstraintTypeMapKeys = "map-keys" )
const TagName = "validationID"
TagName is the struct tag name used for validation IDs.
Variables ¶
var ( ErrConstraintsMissing = errors.New("no constraints provided") ErrEmptyConstraintType = errors.New("constraint type is empty") ErrUnknownConstraintType = errors.New("unknown constraint type") ErrConstraintSpecMissing = errors.New("constraint spec is missing") ErrConstraintAllowListMissing = errors.New("constraint allow list is missing") ErrConstraintPatternMissing = errors.New("constraint pattern is missing") ErrConstraintKeysMissing = errors.New("constraint keys are missing") ErrConstraintKeyNameMissing = errors.New("constraint key name is missing") )
Functions ¶
Types ¶
type Config ¶
type Config struct {
// Fields represents configuration fields.
Fields []ConfigField
// Models represents models to extract validations from and check for ID existence.
Models []Model
}
Config represents the validation configuration.
type ConfigField ¶
type ConfigField struct {
ID ID `yaml:"id"`
SkipIfNotExists bool `yaml:"skipIfNotExists,omitempty"`
Constraints []Constraint `yaml:"constraints"`
}
ConfigField represents a configuration field with its validation constraints. If the ID is not defined via `TagName`, SkipIfNotExists needs to be set to true.
type Constraint ¶
type Constraint struct {
Type string `yaml:"type"`
Spec *ConstraintSpec `yaml:"spec,omitempty"`
}
Constraint represents a validation constraint for a configuration field.
type ConstraintSpec ¶
type ConstraintSpec struct {
AllowList []string `yaml:"allowList,omitempty"`
Pattern string `yaml:"pattern,omitempty"`
Keys []MapKeySpec `yaml:"keys,omitempty"`
}
ConstraintSpec holds the specification for a constraint.
type ListConstraint ¶
type ListConstraint struct {
AllowList []string `yaml:"allowList"`
}
ListConstraint validates that a value is within an allowed list.
func (ListConstraint) Validate ¶
func (l ListConstraint) Validate(value any) error
Validate checks if the provided value is in the AllowList.
type MapKeyConstraintSpec ¶ added in v1.8.0
MapKeyConstraintSpec holds the specification for validating a single map key.
type MapKeySpec ¶ added in v1.8.0
type MapKeySpec struct {
Name string `yaml:"name"`
Required bool `yaml:"required,omitempty"`
Constraints []Constraint `yaml:"constraints,omitempty"`
}
MapKeySpec holds the specification for a map key constraint.
type MapKeysConstraint ¶ added in v1.8.0
type MapKeysConstraint struct {
Keys []MapKeyConstraintSpec
}
MapKeysConstraint validates map keys according to the provided specifications.
func NewMapKeysConstraint ¶ added in v1.8.0
func NewMapKeysConstraint(keys []MapKeySpec) (*MapKeysConstraint, error)
NewMapKeysConstraint creates a new MapKeysConstraint from the provided key specifications.
func (*MapKeysConstraint) Validate ¶ added in v1.8.0
func (m *MapKeysConstraint) Validate(value any) error
Validate checks if the provided map value satisfies all key constraints.
type Model ¶
type Model interface {
Validations() []Field
}
Model defines an interface for models that provide their validation fields.
type NonEmptyConstraint ¶
type NonEmptyConstraint struct{}
NonEmptyConstraint validates that a string value is not empty.
func (NonEmptyConstraint) Validate ¶
func (n NonEmptyConstraint) Validate(value any) error
Validate checks if the provided value is a non-empty string.
type NonEmptyKeysConstraint ¶
type NonEmptyKeysConstraint struct{}
NonEmptyKeysConstraint validates that all keys in a map are non-empty strings.
func (NonEmptyKeysConstraint) Validate ¶
func (n NonEmptyKeysConstraint) Validate(value any) error
Validate checks if the provided value is a map where each key is non-empty.
type NonEmptyValConstraint ¶ added in v1.8.0
type NonEmptyValConstraint struct{}
NonEmptyValConstraint validates that all keys in a map have non-empty values.
func (NonEmptyValConstraint) Validate ¶ added in v1.8.0
func (n NonEmptyValConstraint) Validate(value any) error
Validate checks if the provided value is a map where each value corresponding to a key is non-empty.
type RegexConstraint ¶ added in v1.6.0
type RegexConstraint struct {
// contains filtered or unexported fields
}
RegexConstraint validates that the string matches the configured regex patern.
func NewRegexConstraint ¶ added in v1.6.0
func NewRegexConstraint(pattern string) (*RegexConstraint, error)
NewRegexConstraint takes a pattern and returns a RegexConstraint with the compiled regex patern.
func (*RegexConstraint) Validate ¶ added in v1.6.0
func (r *RegexConstraint) Validate(value any) error
Validate checks if the provided value satisfies the regex constraint.
type Spec ¶
type Spec struct {
// contains filtered or unexported fields
}
Spec represents the validation specification for a given ID.
type Validation ¶
type Validation struct {
// contains filtered or unexported fields
}
Validation represents a map of validation specifications by their IDs.
func New ¶
func New(cfg Config) (*Validation, error)
New creates a new Validation instance with the provided configuration fields.
func (*Validation) Validate ¶
func (v *Validation) Validate(id ID, value any) error
Validate validates a single value by its ID.
func (*Validation) ValidateAll ¶
func (v *Validation) ValidateAll(valuesByID map[ID]any) error
ValidateAll validates all provided values mapped by their IDs.