Documentation
¶
Index ¶
- func ResetEmitter()
- func SetEmitter(e Emitter)
- func ValidateStruct(ctx context.Context, obj any) error
- type CollectionLenRule
- type CollectionMaxLenRule
- type CollectionMinLenRule
- type ContainsRule
- type DefaultSlogEmitter
- type DiagnosticEvent
- type DiveRule
- type EmailRule
- type Emitter
- type EndsWithRule
- type EqFieldRule
- type EqRule
- type ExcludesRule
- type FloatMaxRule
- type FloatMinRule
- type GtRule
- type GteRule
- type IPRule
- type IntMaxRule
- type IntMinRule
- type LtRule
- type LteRule
- type NeFieldRule
- type NeRule
- type OneOfRule
- type RegexRule
- type RequiredRule
- type Rule
- type StartsWithRule
- type StringLenRule
- type StringMaxLengthRule
- type StringMinLengthRule
- type StringRequiredRule
- type URIRule
- type URLRule
- type UUIDRule
- type UniqueRule
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ResetEmitter ¶
func ResetEmitter()
ResetEmitter restores the default SlogEmitter. Useful in tests to avoid cross-test pollution.
func SetEmitter ¶
func SetEmitter(e Emitter)
SetEmitter replaces the active telemetry backend. Thread-safe; safe to call from multiple goroutines.
Types ¶
type CollectionLenRule ¶
CollectionLenRule validates that a collection has exactly Len elements.
func (CollectionLenRule) Validate ¶
func (r CollectionLenRule) Validate(value any, _ any) *DiagnosticEvent
type CollectionMaxLenRule ¶
CollectionMaxLenRule validates that a collection has at most Max elements.
func (CollectionMaxLenRule) Validate ¶
func (r CollectionMaxLenRule) Validate(value any, _ any) *DiagnosticEvent
type CollectionMinLenRule ¶
CollectionMinLenRule validates that a collection has at least Min elements.
func (CollectionMinLenRule) Validate ¶
func (r CollectionMinLenRule) Validate(value any, _ any) *DiagnosticEvent
type ContainsRule ¶
ContainsRule validates that a string contains Substr.
func (ContainsRule) Validate ¶
func (r ContainsRule) Validate(value any, _ any) *DiagnosticEvent
type DefaultSlogEmitter ¶
type DefaultSlogEmitter struct{}
DefaultSlogEmitter is the zero-config fallback. It is intentionally a no-op: the *DiagnosticEvent returned by ValidateStruct implements slog.LogValuer, so callers log it themselves without double-logging.
func (DefaultSlogEmitter) Emit ¶
func (DefaultSlogEmitter) Emit(_ context.Context, _ *DiagnosticEvent)
type DiagnosticEvent ¶
type DiagnosticEvent struct {
Field string
Constraint string
Value string // stringified & truncated by the Rule, max 64 chars
ValueType string // e.g. "string", "int", "unknown"
Message string
}
DiagnosticEvent is the structured payload emitted on every validation failure. Value is always a stringified, truncated copy of the failing input — never `any` — to prevent PII leaks and log-bloat in cloud billing.
func (DiagnosticEvent) Error ¶
func (e DiagnosticEvent) Error() string
Error implements the standard error interface.
func (DiagnosticEvent) LogValue ¶
func (e DiagnosticEvent) LogValue() slog.Value
LogValue implements slog.LogValuer so the event nests under "err" in JSON output.
type DiveRule ¶
DiveRule iterates over a slice and runs SubRules against every element. On first failure it returns a DiagnosticEvent with Field set to "Name[i]".
type EmailRule ¶
type EmailRule struct {
Field string
}
EmailRule validates that a string value looks like an email address. Uses a minimal structural check (no reflection, no regex package at call-time).
type Emitter ¶
type Emitter interface {
Emit(ctx context.Context, event *DiagnosticEvent)
}
Emitter routes DiagnosticEvents to any telemetry backend.
type EndsWithRule ¶
EndsWithRule validates that a string has the given suffix.
func (EndsWithRule) Validate ¶
func (r EndsWithRule) Validate(value any, _ any) *DiagnosticEvent
type EqFieldRule ¶
EqFieldRule validates that this field's value equals the named sibling field. parent must be the containing struct value passed from ValidateStruct.
func (EqFieldRule) Validate ¶
func (r EqFieldRule) Validate(value any, parent any) *DiagnosticEvent
type ExcludesRule ¶
ExcludesRule validates that a string does NOT contain Substr.
func (ExcludesRule) Validate ¶
func (r ExcludesRule) Validate(value any, _ any) *DiagnosticEvent
type FloatMaxRule ¶
FloatMaxRule validates that a float64 value is <= Max.
func (FloatMaxRule) Validate ¶
func (r FloatMaxRule) Validate(value any, _ any) *DiagnosticEvent
type FloatMinRule ¶
FloatMinRule validates that a float64 value is >= Min.
func (FloatMinRule) Validate ¶
func (r FloatMinRule) Validate(value any, _ any) *DiagnosticEvent
type IPRule ¶
type IPRule struct {
Field string
}
IPRule validates that a string is a valid IPv4 or IPv6 address. Uses net.ParseIP — stdlib, no reflection.
type IntMaxRule ¶
IntMaxRule validates that an int value is <= Max.
func (IntMaxRule) Validate ¶
func (r IntMaxRule) Validate(value any, _ any) *DiagnosticEvent
type IntMinRule ¶
IntMinRule validates that an int value is >= Min. Pattern: strict type assertion, strconv for conversion, string concatenation for messages.
func (IntMinRule) Validate ¶
func (r IntMinRule) Validate(value any, _ any) *DiagnosticEvent
type NeFieldRule ¶
NeFieldRule validates that this field's value does NOT equal the named sibling field. parent must be the containing struct value passed from ValidateStruct.
func (NeFieldRule) Validate ¶
func (r NeFieldRule) Validate(value any, parent any) *DiagnosticEvent
type RegexRule ¶
RegexRule validates that a string matches the compiled regexp. The regexp is compiled at construction time (NewRegexRule), never during Validate.
func NewRegexRule ¶
NewRegexRule compiles the pattern once and returns a ready-to-use RegexRule. Returns an error if the pattern is invalid instead of panicking.
type RequiredRule ¶
type RequiredRule struct {
Field string
}
RequiredRule validates that a string value is non-empty and not whitespace-only.
func (RequiredRule) Validate ¶
func (r RequiredRule) Validate(value any, _ any) *DiagnosticEvent
type Rule ¶
type Rule interface {
Validate(value any, parent any) *DiagnosticEvent
}
Rule is implemented by every validation constraint. Validate must NOT use reflection inside simple rules; use strict type assertions instead. On success it returns nil (zero-alloc happy path). parent is the containing struct value — nil unless the rule requires cross-field access (e.g. eqfield).
type StartsWithRule ¶
StartsWithRule validates that a string has the given prefix.
func (StartsWithRule) Validate ¶
func (r StartsWithRule) Validate(value any, _ any) *DiagnosticEvent
type StringLenRule ¶
StringLenRule validates that a string has exactly Len runes.
func (StringLenRule) Validate ¶
func (r StringLenRule) Validate(value any, _ any) *DiagnosticEvent
type StringMaxLengthRule ¶
StringMaxLengthRule validates that a string's rune length is <= Max.
func (StringMaxLengthRule) Validate ¶
func (r StringMaxLengthRule) Validate(value any, _ any) *DiagnosticEvent
type StringMinLengthRule ¶
StringMinLengthRule validates that a string's rune length is >= Min.
func (StringMinLengthRule) Validate ¶
func (r StringMinLengthRule) Validate(value any, _ any) *DiagnosticEvent
type StringRequiredRule ¶
type StringRequiredRule struct {
Field string
}
StringRequiredRule validates that a string is non-empty and not whitespace-only.
func (StringRequiredRule) Validate ¶
func (r StringRequiredRule) Validate(value any, _ any) *DiagnosticEvent
type URIRule ¶
type URIRule struct {
Field string
}
URIRule validates that a string is a valid URI (scheme required; host optional).
type URLRule ¶
type URLRule struct {
Field string
}
URLRule validates that a string is a fully-qualified URL (scheme + host required).
type UUIDRule ¶
type UUIDRule struct {
Field string
}
UUIDRule validates that a string is a canonical UUID v4 format. Zero-alloc: checks length (36) and hyphen positions only — no regex. Format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (8-4-4-4-12 hex digits)
type UniqueRule ¶
type UniqueRule struct {
Field string
}
UniqueRule validates that a slice contains no duplicate values.
func (UniqueRule) Validate ¶
func (r UniqueRule) Validate(value any, _ any) *DiagnosticEvent