Documentation
¶
Overview ¶
Package validate provides composable validation helpers for Go with fluent builders, rule tags, and struct validation. It includes optional message translation support.
Package validate - opts.go Runtime options for struct validation and field path formatting.
Index ¶
- type CheckFunc
- type CheckFuncCtx
- type IntBuilder
- type SliceBuilder
- type StringBuilder
- func (b *StringBuilder) Build() func(any) error
- func (b *StringBuilder) Email() func(any) error
- func (b *StringBuilder) Length(n int) *StringBuilder
- func (b *StringBuilder) MaxLength(n int) *StringBuilder
- func (b *StringBuilder) MinLength(n int) *StringBuilder
- func (b *StringBuilder) OneOf(vals ...string) *StringBuilder
- func (b *StringBuilder) Regex(pat string) *StringBuilder
- type Validate
- func (v *Validate) Bool() func(any) error
- func (v *Validate) FromRules(rules []string) (func(any) error, error)
- func (v *Validate) Int() *IntBuilder
- func (v *Validate) Int64() *IntBuilder
- func (v *Validate) PathSeparator(sep string) *Validate
- func (v *Validate) Slice() *SliceBuilder
- func (v *Validate) String() *StringBuilder
- func (v *Validate) WithTranslator(t translator.Translator) *Validate
- type ValidateOpts
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CheckFunc ¶ added in v1.0.1
CheckFunc validates a single value and returns an error if invalid.
func WithoutContext ¶ added in v1.0.1
func WithoutContext(f CheckFuncCtx) CheckFunc
WithoutContext adapts a CheckFuncCtx to a CheckFunc by using context.Background(). Prefer passing through contexts in hot paths.
type CheckFuncCtx ¶ added in v1.0.1
CheckFuncCtx is the context-aware variant of CheckFunc.
func WithContext ¶ added in v1.0.1
func WithContext(f CheckFunc) CheckFuncCtx
WithContext adapts a CheckFunc to a CheckFuncCtx that ignores ctx.
type IntBuilder ¶ added in v1.0.1
type IntBuilder struct {
// contains filtered or unexported fields
}
IntBuilder accumulates integer rules; Build() finishes it.
func (*IntBuilder) Build ¶ added in v1.0.1
func (b *IntBuilder) Build() func(any) error
Build returns a func(any) error that validates the provided value.
func (*IntBuilder) MaxInt ¶ added in v1.0.1
func (b *IntBuilder) MaxInt(n int64) *IntBuilder
func (*IntBuilder) MinInt ¶ added in v1.0.1
func (b *IntBuilder) MinInt(n int64) *IntBuilder
type SliceBuilder ¶ added in v1.0.1
type SliceBuilder struct {
// contains filtered or unexported fields
}
SliceBuilder accumulates slice rules; Build() finishes it.
func (*SliceBuilder) Build ¶ added in v1.0.1
func (b *SliceBuilder) Build() func(any) error
func (*SliceBuilder) ForEach ¶ added in v1.0.1
func (b *SliceBuilder) ForEach( elem func(any) error, ) *SliceBuilder
ForEach applies an element validator to each item in the slice.
func (*SliceBuilder) Length ¶ added in v1.0.1
func (b *SliceBuilder) Length(n int) *SliceBuilder
func (*SliceBuilder) MaxSliceLength ¶ added in v1.0.1
func (b *SliceBuilder) MaxSliceLength(n int) *SliceBuilder
func (*SliceBuilder) MinSliceLength ¶ added in v1.0.1
func (b *SliceBuilder) MinSliceLength(n int) *SliceBuilder
type StringBuilder ¶ added in v1.0.1
type StringBuilder struct {
// contains filtered or unexported fields
}
StringBuilder accumulates string validation rules and can be finished with Build() or by calling a terminal rule (like Email()) which returns the final func(any) error.
func (*StringBuilder) Build ¶ added in v1.0.1
func (b *StringBuilder) Build() func(any) error
Build returns the composite validator func.
func (*StringBuilder) Email ¶ added in v1.0.1
func (b *StringBuilder) Email() func(any) error
Email is terminal for convenience in examples. It appends the rule and returns the finished validator func.
func (*StringBuilder) Length ¶ added in v1.0.1
func (b *StringBuilder) Length(n int) *StringBuilder
func (*StringBuilder) MaxLength ¶ added in v1.0.1
func (b *StringBuilder) MaxLength(n int) *StringBuilder
func (*StringBuilder) MinLength ¶ added in v1.0.1
func (b *StringBuilder) MinLength(n int) *StringBuilder
func (*StringBuilder) OneOf ¶ added in v1.0.1
func (b *StringBuilder) OneOf(vals ...string) *StringBuilder
func (*StringBuilder) Regex ¶ added in v1.0.1
func (b *StringBuilder) Regex(pat string) *StringBuilder
Regex is not terminal, allowing more chaining. Call Build() to finish.
type Validate ¶
type Validate struct {
// contains filtered or unexported fields
}
Validate is the main struct that holds custom validation rules and an optional translator.
func NewWithCustomRules ¶ added in v1.0.1
NewWithCustomRules creates a new Validate with pre-registered rules.
func (*Validate) Bool ¶ added in v1.0.1
Bool returns a bool validator func (no rules yet, kept for symmetry).
func (*Validate) FromRules ¶
FromRules creates a validator func from rule tokens. The first token is the type: "string", "int", "int64", "slice", "bool".
func (*Validate) Int ¶ added in v1.0.1
func (v *Validate) Int() *IntBuilder
Int returns a fluent integer validator builder that accepts any Go int type at call time.
func (*Validate) Int64 ¶ added in v1.0.1
func (v *Validate) Int64() *IntBuilder
Int64 returns a fluent builder that requires exactly int64.
func (*Validate) PathSeparator ¶ added in v1.0.1
PathSeparator customizes the separator used in nested field paths. Example: "User.Addresses[2].Zip".
func (*Validate) Slice ¶ added in v1.0.1
func (v *Validate) Slice() *SliceBuilder
Slice returns a fluent slice validator builder. It accepts any slice element type at call time.
func (*Validate) String ¶ added in v1.0.1
func (v *Validate) String() *StringBuilder
String returns a fluent string validator builder.
func (*Validate) WithTranslator ¶
func (v *Validate) WithTranslator( t translator.Translator, ) *Validate
WithTranslator sets a Translator for localized error messages and returns the receiver for chaining.
type ValidateOpts ¶ added in v1.0.1
type ValidateOpts struct {
// StopOnFirst makes validation fail fast on the first FieldError.
// Default is false (aggregate all errors).
StopOnFirst bool
// PathSep sets the separator between nested field parts.
// Example: "User.Addresses[2].Zip".
// If empty, it will be taken from Validate (or "." if nil).
PathSep string
}
ValidateOpts tunes validation behavior per call.
func ApplyOpts ¶ added in v1.0.1
func ApplyOpts(v *Validate, o ValidateOpts) ValidateOpts
ApplyOpts fills missing values using the given *Validate instance. If PathSep is empty, it uses v's separator, or "." when v is nil.
func (ValidateOpts) WithDefaults ¶ added in v1.0.1
func (o ValidateOpts) WithDefaults() ValidateOpts
WithDefaults currently does not change anything, but is kept to allow future option defaults without breaking callers.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package errors provides error types and error handling utilities for validation.
|
Package errors provides error types and error handling utilities for validation. |
|
Package structvalidator provides struct validation functionality using reflection.
|
Package structvalidator provides struct validation functionality using reflection. |
|
Package translator provides internationalization support for validation messages.
|
Package translator provides internationalization support for validation messages. |
|
Package validators provides type-specific validation functionality.
|
Package validators provides type-specific validation functionality. |