Documentation
¶
Overview ¶
Package edict provides a fluent, type-safe API for validating maps, structs, and direct values with zero external dependencies.
Three data modes are supported:
// Map mode: values looked up by dot-path
v := edict.FromMap(data)
v.Field("port").Required().IntRange(1, 65535)
v.Field("darkMode.kind").OneOf("selector", "mediaQuery", "both")
if err := v.Validate(); err != nil {
fmt.Println(err)
}
// Direct-value mode: caller provides values explicitly
v := edict.New()
v.Field("port").Value(cfg.Port).Required().IntRange(1, 65535)
if err := v.Validate(); err != nil {
fmt.Println(err)
}
// Struct mode: values resolved from exported struct fields
v := edict.FromStruct(&cfg)
v.Field("Port").Required().IntRange(1, 65535)
v.Field("DarkMode.Kind").OneOf("selector", "mediaQuery")
if err := v.Validate(); err != nil {
fmt.Println(err)
}
Rules are declared first, then executed in a single Validate() call. Cross-field rules work identically across all three data modes.
Index ¶
- func FormatErrors(errs []Error) string
- type Coder
- type DiveBuilder
- func (db *DiveBuilder) Apply(rules ...Rule) *DiveBuilder
- func (db *DiveBuilder) Check(fn func(value any) bool, msg string) *DiveBuilder
- func (db *DiveBuilder) CheckIf(cond bool, msg string) *DiveBuilder
- func (db *DiveBuilder) Each() *DiveBuilder
- func (db *DiveBuilder) End() *FieldBuilder
- func (db *DiveBuilder) FloatMax(max float64) *DiveBuilder
- func (db *DiveBuilder) FloatMin(min float64) *DiveBuilder
- func (db *DiveBuilder) FloatRange(min, max float64) *DiveBuilder
- func (db *DiveBuilder) IntMax(max int) *DiveBuilder
- func (db *DiveBuilder) IntMin(min int) *DiveBuilder
- func (db *DiveBuilder) IntRange(min, max int) *DiveBuilder
- func (db *DiveBuilder) LenExact(n int) *DiveBuilder
- func (db *DiveBuilder) LenRange(min, max int) *DiveBuilder
- func (db *DiveBuilder) Max(max float64) *DiveBuilder
- func (db *DiveBuilder) MaxLength(n int) *DiveBuilder
- func (db *DiveBuilder) MinLength(n int) *DiveBuilder
- func (db *DiveBuilder) NotOneOf(forbidden ...string) *DiveBuilder
- func (db *DiveBuilder) OneOf(allowed ...string) *DiveBuilder
- func (db *DiveBuilder) Required() *DiveBuilder
- type Error
- type FieldBuilder
- func (fb *FieldBuilder) Apply(rules ...Rule) *FieldBuilder
- func (fb *FieldBuilder) AsWarning() *FieldBuilder
- func (fb *FieldBuilder) Check(fn func(value any) bool, msg string) *FieldBuilder
- func (fb *FieldBuilder) CheckIf(cond bool, msg string) *FieldBuilder
- func (fb *FieldBuilder) Distinct() *FieldBuilder
- func (fb *FieldBuilder) Each() *DiveBuilder
- func (fb *FieldBuilder) EachKey() *DiveBuilder
- func (fb *FieldBuilder) EachValue() *DiveBuilder
- func (fb *FieldBuilder) EqField(other string) *FieldBuilder
- func (fb *FieldBuilder) ExcludedIf(field string, values ...string) *FieldBuilder
- func (fb *FieldBuilder) ExcludedUnless(field string, values ...string) *FieldBuilder
- func (fb *FieldBuilder) FloatMax(max float64) *FieldBuilder
- func (fb *FieldBuilder) FloatMin(min float64) *FieldBuilder
- func (fb *FieldBuilder) FloatRange(min, max float64) *FieldBuilder
- func (fb *FieldBuilder) GteField(other string) *FieldBuilder
- func (fb *FieldBuilder) IntMax(max int) *FieldBuilder
- func (fb *FieldBuilder) IntMin(min int) *FieldBuilder
- func (fb *FieldBuilder) IntRange(min, max int) *FieldBuilder
- func (fb *FieldBuilder) IsAlpha() *FieldBuilder
- func (fb *FieldBuilder) IsAlphaNumeric() *FieldBuilder
- func (fb *FieldBuilder) IsEmail() *FieldBuilder
- func (fb *FieldBuilder) IsHexColor() *FieldBuilder
- func (fb *FieldBuilder) IsIPv4() *FieldBuilder
- func (fb *FieldBuilder) IsIPv6() *FieldBuilder
- func (fb *FieldBuilder) IsNumeric() *FieldBuilder
- func (fb *FieldBuilder) IsSemver() *FieldBuilder
- func (fb *FieldBuilder) IsSlug() *FieldBuilder
- func (fb *FieldBuilder) IsURL() *FieldBuilder
- func (fb *FieldBuilder) IsUUID() *FieldBuilder
- func (fb *FieldBuilder) LenExact(n int) *FieldBuilder
- func (fb *FieldBuilder) LenRange(min, max int) *FieldBuilder
- func (fb *FieldBuilder) LessOrEqual(otherField string) *FieldBuilder
- func (fb *FieldBuilder) LteField(other string) *FieldBuilder
- func (fb *FieldBuilder) Max(max float64) *FieldBuilder
- func (fb *FieldBuilder) MaxLength(n int) *FieldBuilder
- func (fb *FieldBuilder) Message(msg string) *FieldBuilder
- func (fb *FieldBuilder) MinLength(n int) *FieldBuilder
- func (fb *FieldBuilder) NotOneOf(forbidden ...string) *FieldBuilder
- func (fb *FieldBuilder) OmitEmpty() *FieldBuilder
- func (fb *FieldBuilder) OmitNil() *FieldBuilder
- func (fb *FieldBuilder) OneOf(allowed ...string) *FieldBuilder
- func (fb *FieldBuilder) Required() *FieldBuilder
- func (fb *FieldBuilder) RequiredIf(field string, values ...string) *FieldBuilder
- func (fb *FieldBuilder) RequiredUnless(field string, values ...string) *FieldBuilder
- func (fb *FieldBuilder) RequiredWith(fields ...string) *FieldBuilder
- func (fb *FieldBuilder) RequiredWithAll(fields ...string) *FieldBuilder
- func (fb *FieldBuilder) RequiredWithout(fields ...string) *FieldBuilder
- func (fb *FieldBuilder) RequiredWithoutAll(fields ...string) *FieldBuilder
- func (fb *FieldBuilder) Value(v any) *FieldBuilder
- func (fb *FieldBuilder) When(cond bool, rules ...Rule) *FieldBuilder
- type InternalError
- type MessageFunc
- type MessageRegistry
- type Option
- type ResolverAware
- type Results
- func (r *Results) AllIssues() []Error
- func (r *Results) Error() string
- func (r *Results) Errors() []Error
- func (r *Results) FormatErrors() string
- func (r *Results) FormatWarnings() string
- func (r *Results) HasErrors() bool
- func (r *Results) HasInternalErrors() bool
- func (r *Results) HasWarnings() bool
- func (r *Results) InternalErrors() []error
- func (r *Results) Warnings() []Error
- type Rule
- func NewCheck(fn func(value any) bool, msg string) Rule
- func NewCheckIf(cond bool, msg string) Rule
- func NewDistinct() Rule
- func NewEqField(other string) Rule
- func NewExcludedIf(field string, values ...string) Rule
- func NewExcludedUnless(field string, values ...string) Rule
- func NewFloatMax(max float64) Rule
- func NewFloatMin(min float64) Rule
- func NewFloatRange(min, max float64) Rule
- func NewGteField(other string) Rule
- func NewIntMax(max int) Rule
- func NewIntMin(min int) Rule
- func NewIntRange(min, max int) Rule
- func NewLenExact(n int) Rule
- func NewLenRange(min, max int) Rule
- func NewLessOrEqual(otherPath string) Rule
- func NewLteField(other string) Rule
- func NewMax(max float64) Rule
- func NewMaxLength(n int) Rule
- func NewMinLength(n int) Rule
- func NewNotOneOf(forbidden ...string) Rule
- func NewOneOf(allowed ...string) Rule
- func NewRequired() Rule
- func NewRequiredIf(field string, values ...string) Rule
- func NewRequiredUnless(field string, values ...string) Rule
- func NewRequiredWith(fields ...string) Rule
- func NewRequiredWithAll(fields ...string) Rule
- func NewRequiredWithout(fields ...string) Rule
- func NewRequiredWithoutAll(fields ...string) Rule
- func NewWhen(cond bool, rules ...Rule) Rule
- type RuleFunc
- type RuleGroup
- type RuleGroupBuilder
- func (b *RuleGroupBuilder) Build() *RuleGroup
- func (b *RuleGroupBuilder) FloatMax(max float64) *RuleGroupBuilder
- func (b *RuleGroupBuilder) FloatMin(min float64) *RuleGroupBuilder
- func (b *RuleGroupBuilder) FloatRange(min, max float64) *RuleGroupBuilder
- func (b *RuleGroupBuilder) IntMax(max int) *RuleGroupBuilder
- func (b *RuleGroupBuilder) IntMin(min int) *RuleGroupBuilder
- func (b *RuleGroupBuilder) IntRange(min, max int) *RuleGroupBuilder
- func (b *RuleGroupBuilder) LenExact(n int) *RuleGroupBuilder
- func (b *RuleGroupBuilder) LenRange(min, max int) *RuleGroupBuilder
- func (b *RuleGroupBuilder) Max(max float64) *RuleGroupBuilder
- func (b *RuleGroupBuilder) MaxLength(n int) *RuleGroupBuilder
- func (b *RuleGroupBuilder) MinLength(n int) *RuleGroupBuilder
- func (b *RuleGroupBuilder) NotOneOf(forbidden ...string) *RuleGroupBuilder
- func (b *RuleGroupBuilder) OneOf(allowed ...string) *RuleGroupBuilder
- func (b *RuleGroupBuilder) Required() *RuleGroupBuilder
- type Severity
- type Validator
- func (v *Validator) Except(paths ...string) *Validator
- func (v *Validator) Field(path string) *FieldBuilder
- func (v *Validator) HasInternalErrors() bool
- func (v *Validator) InternalErrors() []error
- func (v *Validator) Only(paths ...string) *Validator
- func (v *Validator) Validate() error
- func (v *Validator) Warn(path string) *FieldBuilder
- func (v *Validator) Warnings() []Error
- type ValueResolver
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FormatErrors ¶
FormatErrors formats a slice of Error values as a multi-line string.
Types ¶
type Coder ¶
type Coder interface {
Code() string
}
Coder is optionally implemented by rules that provide a stable error code.
type DiveBuilder ¶
type DiveBuilder struct {
// contains filtered or unexported fields
}
DiveBuilder provides a fluent API for declaring rules on collection elements.
func (*DiveBuilder) Apply ¶
func (db *DiveBuilder) Apply(rules ...Rule) *DiveBuilder
Apply adds custom Rule implementations to validate each element.
func (*DiveBuilder) Check ¶
func (db *DiveBuilder) Check(fn func(value any) bool, msg string) *DiveBuilder
Check validates each element with a custom function.
func (*DiveBuilder) CheckIf ¶
func (db *DiveBuilder) CheckIf(cond bool, msg string) *DiveBuilder
CheckIf adds a pre-evaluated boolean condition to each element.
func (*DiveBuilder) Each ¶
func (db *DiveBuilder) Each() *DiveBuilder
Each creates a nested dive for multidimensional collections.
func (*DiveBuilder) End ¶
func (db *DiveBuilder) End() *FieldBuilder
End returns the parent FieldBuilder to resume the field chain.
func (*DiveBuilder) FloatMax ¶
func (db *DiveBuilder) FloatMax(max float64) *DiveBuilder
FloatMax validates that each element is <= max.
func (*DiveBuilder) FloatMin ¶
func (db *DiveBuilder) FloatMin(min float64) *DiveBuilder
FloatMin validates that each element is >= min.
func (*DiveBuilder) FloatRange ¶
func (db *DiveBuilder) FloatRange(min, max float64) *DiveBuilder
FloatRange validates that each element is in [min, max].
func (*DiveBuilder) IntMax ¶
func (db *DiveBuilder) IntMax(max int) *DiveBuilder
IntMax validates that each element is <= max.
func (*DiveBuilder) IntMin ¶
func (db *DiveBuilder) IntMin(min int) *DiveBuilder
IntMin validates that each element is >= min.
func (*DiveBuilder) IntRange ¶
func (db *DiveBuilder) IntRange(min, max int) *DiveBuilder
IntRange validates that each element is in [min, max].
func (*DiveBuilder) LenExact ¶
func (db *DiveBuilder) LenExact(n int) *DiveBuilder
LenExact checks that each string element has exactly n characters.
func (*DiveBuilder) LenRange ¶
func (db *DiveBuilder) LenRange(min, max int) *DiveBuilder
LenRange checks that each string element length is in [min, max].
func (*DiveBuilder) Max ¶
func (db *DiveBuilder) Max(max float64) *DiveBuilder
Max validates that each element is <= max (any numeric type).
func (*DiveBuilder) MaxLength ¶
func (db *DiveBuilder) MaxLength(n int) *DiveBuilder
MaxLength checks that each string element has at most n characters.
func (*DiveBuilder) MinLength ¶
func (db *DiveBuilder) MinLength(n int) *DiveBuilder
MinLength checks that each string element has at least n characters.
func (*DiveBuilder) NotOneOf ¶
func (db *DiveBuilder) NotOneOf(forbidden ...string) *DiveBuilder
NotOneOf validates that each element is NOT in the forbidden set.
func (*DiveBuilder) OneOf ¶
func (db *DiveBuilder) OneOf(allowed ...string) *DiveBuilder
OneOf validates that each element is one of the allowed strings.
func (*DiveBuilder) Required ¶
func (db *DiveBuilder) Required() *DiveBuilder
Required validates that each element is non-zero.
type FieldBuilder ¶
type FieldBuilder struct {
// contains filtered or unexported fields
}
FieldBuilder provides the fluent API for declaring rules on a single field.
func (*FieldBuilder) Apply ¶
func (fb *FieldBuilder) Apply(rules ...Rule) *FieldBuilder
Apply adds one or more custom Rule implementations to this field.
func (*FieldBuilder) AsWarning ¶
func (fb *FieldBuilder) AsWarning() *FieldBuilder
AsWarning sets the severity of the last added rule to Warning.
func (*FieldBuilder) Check ¶
func (fb *FieldBuilder) Check(fn func(value any) bool, msg string) *FieldBuilder
Check applies a custom function condition.
func (*FieldBuilder) CheckIf ¶
func (fb *FieldBuilder) CheckIf(cond bool, msg string) *FieldBuilder
CheckIf applies a pre-evaluated boolean condition.
func (*FieldBuilder) Distinct ¶
func (fb *FieldBuilder) Distinct() *FieldBuilder
Distinct checks that a slice or array contains no duplicate elements.
func (*FieldBuilder) Each ¶
func (fb *FieldBuilder) Each() *DiveBuilder
Each creates a DiveBuilder that validates each element of a slice or array.
func (*FieldBuilder) EachKey ¶
func (fb *FieldBuilder) EachKey() *DiveBuilder
EachKey creates a DiveBuilder that validates each key of a map.
func (*FieldBuilder) EachValue ¶
func (fb *FieldBuilder) EachValue() *DiveBuilder
EachValue creates a DiveBuilder that validates each value of a map.
func (*FieldBuilder) EqField ¶
func (fb *FieldBuilder) EqField(other string) *FieldBuilder
EqField checks that this field's value equals another field's value.
func (*FieldBuilder) ExcludedIf ¶
func (fb *FieldBuilder) ExcludedIf(field string, values ...string) *FieldBuilder
ExcludedIf fails if this field is non-zero and the other field matches any of the given values.
func (*FieldBuilder) ExcludedUnless ¶
func (fb *FieldBuilder) ExcludedUnless(field string, values ...string) *FieldBuilder
ExcludedUnless fails if this field is non-zero and the other field does NOT match any of the given values.
func (*FieldBuilder) FloatMax ¶
func (fb *FieldBuilder) FloatMax(max float64) *FieldBuilder
FloatMax checks that a float64 value is <= max.
func (*FieldBuilder) FloatMin ¶
func (fb *FieldBuilder) FloatMin(min float64) *FieldBuilder
FloatMin checks that a float64 value is >= min.
func (*FieldBuilder) FloatRange ¶
func (fb *FieldBuilder) FloatRange(min, max float64) *FieldBuilder
FloatRange checks that a float64 value is in [min, max].
func (*FieldBuilder) GteField ¶
func (fb *FieldBuilder) GteField(other string) *FieldBuilder
GteField checks that this field's value is >= another field's value.
func (*FieldBuilder) IntMax ¶
func (fb *FieldBuilder) IntMax(max int) *FieldBuilder
IntMax checks that an int value is <= max.
func (*FieldBuilder) IntMin ¶
func (fb *FieldBuilder) IntMin(min int) *FieldBuilder
IntMin checks that an int value is >= min.
func (*FieldBuilder) IntRange ¶
func (fb *FieldBuilder) IntRange(min, max int) *FieldBuilder
IntRange checks that an int value is in [min, max].
func (*FieldBuilder) IsAlpha ¶
func (fb *FieldBuilder) IsAlpha() *FieldBuilder
IsAlpha validates that the value contains only ASCII letters.
func (*FieldBuilder) IsAlphaNumeric ¶
func (fb *FieldBuilder) IsAlphaNumeric() *FieldBuilder
IsAlphaNumeric validates that the value contains only ASCII letters and digits.
func (*FieldBuilder) IsEmail ¶
func (fb *FieldBuilder) IsEmail() *FieldBuilder
IsEmail validates that the value is a valid email address.
func (*FieldBuilder) IsHexColor ¶
func (fb *FieldBuilder) IsHexColor() *FieldBuilder
IsHexColor validates hex color format (#RGB or #RRGGBB).
func (*FieldBuilder) IsIPv4 ¶
func (fb *FieldBuilder) IsIPv4() *FieldBuilder
IsIPv4 validates IPv4 address format.
func (*FieldBuilder) IsIPv6 ¶
func (fb *FieldBuilder) IsIPv6() *FieldBuilder
IsIPv6 validates IPv6 address format.
func (*FieldBuilder) IsNumeric ¶
func (fb *FieldBuilder) IsNumeric() *FieldBuilder
IsNumeric validates that the value contains only ASCII digits.
func (*FieldBuilder) IsSemver ¶
func (fb *FieldBuilder) IsSemver() *FieldBuilder
IsSemver validates semantic versioning format (X.Y.Z with optional v prefix).
func (*FieldBuilder) IsSlug ¶
func (fb *FieldBuilder) IsSlug() *FieldBuilder
IsSlug validates that the value is a valid URL slug (lowercase alphanumeric with hyphens).
func (*FieldBuilder) IsURL ¶
func (fb *FieldBuilder) IsURL() *FieldBuilder
IsURL validates URL format (requires http/https scheme and host).
func (*FieldBuilder) IsUUID ¶
func (fb *FieldBuilder) IsUUID() *FieldBuilder
IsUUID validates that the value is a valid UUID (8-4-4-4-12 hex).
func (*FieldBuilder) LenExact ¶
func (fb *FieldBuilder) LenExact(n int) *FieldBuilder
LenExact checks that a string value has exactly n characters.
func (*FieldBuilder) LenRange ¶
func (fb *FieldBuilder) LenRange(min, max int) *FieldBuilder
LenRange checks that a string value length is in [min, max].
func (*FieldBuilder) LessOrEqual ¶
func (fb *FieldBuilder) LessOrEqual(otherField string) *FieldBuilder
LessOrEqual checks that this field's int value <= another field's int value.
func (*FieldBuilder) LteField ¶
func (fb *FieldBuilder) LteField(other string) *FieldBuilder
LteField checks that this field's value is <= another field's value.
func (*FieldBuilder) Max ¶
func (fb *FieldBuilder) Max(max float64) *FieldBuilder
Max checks that a numeric value is <= max (any numeric type via float64 coercion).
func (*FieldBuilder) MaxLength ¶
func (fb *FieldBuilder) MaxLength(n int) *FieldBuilder
MaxLength checks that a string value has at most n characters.
func (*FieldBuilder) Message ¶
func (fb *FieldBuilder) Message(msg string) *FieldBuilder
Message overrides the error message of the last added rule.
func (*FieldBuilder) MinLength ¶
func (fb *FieldBuilder) MinLength(n int) *FieldBuilder
MinLength checks that a string value has at least n characters.
func (*FieldBuilder) NotOneOf ¶
func (fb *FieldBuilder) NotOneOf(forbidden ...string) *FieldBuilder
NotOneOf checks that a string value is NOT in the forbidden set.
func (*FieldBuilder) OmitEmpty ¶
func (fb *FieldBuilder) OmitEmpty() *FieldBuilder
OmitEmpty skips rule evaluation when the value is a zero value. This matches the default behavior but makes the intent explicit. Panics if called after Required(), since the two are contradictory.
func (*FieldBuilder) OmitNil ¶
func (fb *FieldBuilder) OmitNil() *FieldBuilder
OmitNil skips rule evaluation only when the value is nil. Non-nil zero values (0, "", false) are not skipped. Panics if called after Required(), since the two are contradictory.
func (*FieldBuilder) OneOf ¶
func (fb *FieldBuilder) OneOf(allowed ...string) *FieldBuilder
OneOf checks that a string value is one of the allowed values.
func (*FieldBuilder) Required ¶
func (fb *FieldBuilder) Required() *FieldBuilder
Required marks the field as required.
func (*FieldBuilder) RequiredIf ¶
func (fb *FieldBuilder) RequiredIf(field string, values ...string) *FieldBuilder
RequiredIf makes this field required when another field matches any of the given values.
func (*FieldBuilder) RequiredUnless ¶
func (fb *FieldBuilder) RequiredUnless(field string, values ...string) *FieldBuilder
RequiredUnless makes this field required unless another field matches any of the given values.
func (*FieldBuilder) RequiredWith ¶
func (fb *FieldBuilder) RequiredWith(fields ...string) *FieldBuilder
RequiredWith makes this field required when any of the listed fields are present and non-zero.
func (*FieldBuilder) RequiredWithAll ¶
func (fb *FieldBuilder) RequiredWithAll(fields ...string) *FieldBuilder
RequiredWithAll makes this field required when all of the listed fields are present and non-zero.
func (*FieldBuilder) RequiredWithout ¶
func (fb *FieldBuilder) RequiredWithout(fields ...string) *FieldBuilder
RequiredWithout makes this field required when any of the listed fields are absent or zero.
func (*FieldBuilder) RequiredWithoutAll ¶
func (fb *FieldBuilder) RequiredWithoutAll(fields ...string) *FieldBuilder
RequiredWithoutAll makes this field required when all of the listed fields are absent or zero.
func (*FieldBuilder) Value ¶
func (fb *FieldBuilder) Value(v any) *FieldBuilder
Value provides a direct value for this field.
func (*FieldBuilder) When ¶
func (fb *FieldBuilder) When(cond bool, rules ...Rule) *FieldBuilder
When wraps the given rules in a conditional guard.
type InternalError ¶
type InternalError struct {
Err error
}
InternalError signals an infrastructure failure inside a custom rule (e.g., database down, file not found). Internal errors are collected separately from validation errors and never block Validate().
func (*InternalError) Error ¶
func (e *InternalError) Error() string
func (*InternalError) Unwrap ¶
func (e *InternalError) Unwrap() error
type MessageFunc ¶
MessageFunc formats a custom message given the validation error context.
type MessageRegistry ¶
type MessageRegistry map[string]MessageFunc
MessageRegistry maps rule codes to custom message functions. When a rule produces an error whose Code matches a registry key, the registry function is called and its return value replaces the message.
type Option ¶
type Option func(*validatorOptions)
Option configures a Validator.
func StopOnFirst ¶
func StopOnFirst() Option
StopOnFirst returns an Option that causes Validate() to return after the first validation error. Warnings do not trigger the stop.
func StrictFields ¶
StrictFields returns an Option that reports any key present in the data source but not in the known set as an unknown_field error.
When called with no arguments, the known set is built from the field paths declared on the validator via Field() and Warn() calls. When called with explicit paths, those paths are used instead.
func WithMessages ¶
func WithMessages(registry MessageRegistry) Option
WithMessages returns an Option that registers custom message overrides. Registry messages are only applied when no per-field .Message() override is set. Precedence: .Message() > registry > rule default.
type ResolverAware ¶
type ResolverAware interface {
Bind(r ValueResolver)
}
ResolverAware is implemented by rules that need to look up other field values. The Validator calls Bind() before Validate() during the execution phase.
type Results ¶
type Results struct {
// contains filtered or unexported fields
}
func (*Results) FormatErrors ¶
func (*Results) FormatWarnings ¶
func (*Results) HasInternalErrors ¶
func (*Results) HasWarnings ¶
func (*Results) InternalErrors ¶
type Rule ¶
Rule validates a single value and returns nil on success or an error on failure. Rules must be safe for reuse across multiple Validate() calls.
func NewCheckIf ¶
NewCheckIf returns a Rule that fails when cond is false.
func NewDistinct ¶
func NewDistinct() Rule
NewDistinct returns a Rule that fails when a slice or array contains duplicate elements.
func NewEqField ¶
NewEqField returns a Rule that fails when this field's value does not equal another field's value.
func NewExcludedIf ¶
NewExcludedIf returns a Rule that fails when this field is non-zero and another field matches any of the given values.
func NewExcludedUnless ¶
NewExcludedUnless returns a Rule that fails when this field is non-zero and another field does NOT match any of the given values.
func NewFloatMax ¶
NewFloatMax returns a Rule that fails when the float value exceeds max.
func NewFloatMin ¶
NewFloatMin returns a Rule that fails when the float value is below min.
func NewFloatRange ¶
NewFloatRange returns a Rule that fails when the float value is outside [min, max].
func NewGteField ¶
NewGteField returns a Rule that fails when this field's value is less than another field's value.
func NewIntRange ¶
NewIntRange returns a Rule that fails when the integer value is outside [min, max].
func NewLenExact ¶
NewLenExact returns a Rule that fails when the string is not exactly n characters.
func NewLenRange ¶
NewLenRange returns a Rule that fails when the string length is outside [min, max].
func NewLessOrEqual ¶
NewLessOrEqual returns a Rule that fails when this field's value exceeds another field's value.
func NewLteField ¶
NewLteField returns a Rule that fails when this field's value is greater than another field's value.
func NewMaxLength ¶
NewMaxLength returns a Rule that fails when the string is longer than n characters.
func NewMinLength ¶
NewMinLength returns a Rule that fails when the string is shorter than n characters.
func NewNotOneOf ¶
NewNotOneOf returns a Rule that fails when the string value IS in the forbidden set.
func NewRequired ¶
func NewRequired() Rule
NewRequired returns a Rule that fails when the value is zero.
func NewRequiredIf ¶
NewRequiredIf returns a Rule that makes a field required when another field matches any of the given values.
func NewRequiredUnless ¶
NewRequiredUnless returns a Rule that makes a field required unless another field matches any of the given values.
func NewRequiredWith ¶
NewRequiredWith returns a Rule that makes a field required when any of the listed fields are present and non-zero.
func NewRequiredWithAll ¶
NewRequiredWithAll returns a Rule that makes a field required when all of the listed fields are present and non-zero.
func NewRequiredWithout ¶
NewRequiredWithout returns a Rule that makes a field required when any of the listed fields are absent or zero.
func NewRequiredWithoutAll ¶
NewRequiredWithoutAll returns a Rule that makes a field required when all of the listed fields are absent or zero.
type RuleGroup ¶
type RuleGroup struct {
// contains filtered or unexported fields
}
RuleGroup holds a reusable set of rules that can be applied to multiple fields.
type RuleGroupBuilder ¶
type RuleGroupBuilder struct {
// contains filtered or unexported fields
}
RuleGroupBuilder constructs a RuleGroup via a fluent API.
func NewRuleGroup ¶
func NewRuleGroup() *RuleGroupBuilder
NewRuleGroup starts building a reusable rule group.
func (*RuleGroupBuilder) Build ¶
func (b *RuleGroupBuilder) Build() *RuleGroup
Build finalizes the group and returns an immutable RuleGroup.
func (*RuleGroupBuilder) FloatMax ¶
func (b *RuleGroupBuilder) FloatMax(max float64) *RuleGroupBuilder
func (*RuleGroupBuilder) FloatMin ¶
func (b *RuleGroupBuilder) FloatMin(min float64) *RuleGroupBuilder
func (*RuleGroupBuilder) FloatRange ¶
func (b *RuleGroupBuilder) FloatRange(min, max float64) *RuleGroupBuilder
func (*RuleGroupBuilder) IntMax ¶
func (b *RuleGroupBuilder) IntMax(max int) *RuleGroupBuilder
func (*RuleGroupBuilder) IntMin ¶
func (b *RuleGroupBuilder) IntMin(min int) *RuleGroupBuilder
func (*RuleGroupBuilder) IntRange ¶
func (b *RuleGroupBuilder) IntRange(min, max int) *RuleGroupBuilder
func (*RuleGroupBuilder) LenExact ¶
func (b *RuleGroupBuilder) LenExact(n int) *RuleGroupBuilder
func (*RuleGroupBuilder) LenRange ¶
func (b *RuleGroupBuilder) LenRange(min, max int) *RuleGroupBuilder
func (*RuleGroupBuilder) Max ¶
func (b *RuleGroupBuilder) Max(max float64) *RuleGroupBuilder
func (*RuleGroupBuilder) MaxLength ¶
func (b *RuleGroupBuilder) MaxLength(n int) *RuleGroupBuilder
func (*RuleGroupBuilder) MinLength ¶
func (b *RuleGroupBuilder) MinLength(n int) *RuleGroupBuilder
func (*RuleGroupBuilder) NotOneOf ¶
func (b *RuleGroupBuilder) NotOneOf(forbidden ...string) *RuleGroupBuilder
func (*RuleGroupBuilder) OneOf ¶
func (b *RuleGroupBuilder) OneOf(allowed ...string) *RuleGroupBuilder
func (*RuleGroupBuilder) Required ¶
func (b *RuleGroupBuilder) Required() *RuleGroupBuilder
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator accumulates field rules and executes them in a single Validate() call.
func FromMap ¶
FromMap creates a Validator backed by a map. Supports nested maps via dot-path traversal.
func FromStruct ¶
FromStruct creates a Validator backed by a struct. Fields are resolved by exported field names using dot-path traversal.
func New ¶
New creates a Validator with no data source. Fields must provide values via .Value() on the FieldBuilder.
func (*Validator) Except ¶
Except skips validation for the given field paths. All other fields run normally. Excluded fields still contribute values for cross-field rule lookups.
func (*Validator) Field ¶
func (v *Validator) Field(path string) *FieldBuilder
Field starts a fluent rule chain for the given field path. Rules appended via Field() default to SeverityError.
func (*Validator) HasInternalErrors ¶
HasInternalErrors returns true if the last Validate() call produced internal errors.
func (*Validator) InternalErrors ¶
InternalErrors returns internal errors from the last Validate() call.
func (*Validator) Only ¶
Only restricts validation to the given field paths. All other fields are skipped. Excluded fields still contribute values for cross-field rule lookups.
func (*Validator) Validate ¶
Validate runs all accumulated rules and returns an error if any validation errors exist. Returns nil if there are no errors.
func (*Validator) Warn ¶
func (v *Validator) Warn(path string) *FieldBuilder
Warn starts a fluent rule chain identical to Field(), but rules appended via Warn() default to SeverityWarning.