validation

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 24, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package validation provides rules-based output validation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CustomRule

type CustomRule struct {
	// contains filtered or unexported fields
}

CustomRule allows custom validation logic.

func Custom

func Custom(name, field string, check func(value any) (valid bool, message string)) *CustomRule

Custom creates a custom validation rule.

func (*CustomRule) Name

func (r *CustomRule) Name() string

func (*CustomRule) Validate

func (r *CustomRule) Validate(value any) *ValidationError

type EnumRule

type EnumRule struct {
	// contains filtered or unexported fields
}

EnumRule validates that a value is in a set of allowed values.

func Enum

func Enum(field string, allowed ...string) *EnumRule

Enum creates an enum validation rule.

func (*EnumRule) Name

func (r *EnumRule) Name() string

func (*EnumRule) Validate

func (r *EnumRule) Validate(value any) *ValidationError

type LengthRule

type LengthRule struct {
	// contains filtered or unexported fields
}

LengthRule validates string or slice length.

func Length

func Length(field string, min, max int) *LengthRule

Length creates a length validation rule.

func (*LengthRule) Name

func (r *LengthRule) Name() string

func (*LengthRule) Validate

func (r *LengthRule) Validate(value any) *ValidationError

type RangeRule

type RangeRule struct {
	// contains filtered or unexported fields
}

RangeRule validates that a numeric value is within a range.

func Range

func Range(field string, min, max float64) *RangeRule

Range creates a range validation rule.

func (*RangeRule) Name

func (r *RangeRule) Name() string

func (*RangeRule) Validate

func (r *RangeRule) Validate(value any) *ValidationError

type RegexRule

type RegexRule struct {
	// contains filtered or unexported fields
}

RegexRule validates a string against a regex pattern.

func Regex

func Regex(field, pattern, message string) *RegexRule

Regex creates a regex validation rule.

func (*RegexRule) Name

func (r *RegexRule) Name() string

func (*RegexRule) Validate

func (r *RegexRule) Validate(value any) *ValidationError

type RequiredRule

type RequiredRule struct {
	// contains filtered or unexported fields
}

RequiredRule checks that a value is not nil or empty.

func Required

func Required(field string) *RequiredRule

Required creates a required field rule.

func (*RequiredRule) Name

func (r *RequiredRule) Name() string

func (*RequiredRule) Validate

func (r *RequiredRule) Validate(value any) *ValidationError

type Result

type Result struct {
	Valid    bool
	Errors   []*ValidationError
	Warnings []*ValidationError
}

Result contains validation results.

type Rule

type Rule interface {
	// Name returns the rule identifier.
	Name() string

	// Validate checks the value and returns an error if invalid.
	Validate(value any) *ValidationError
}

Rule defines a validation rule interface.

type RuleSet

type RuleSet struct {
	// contains filtered or unexported fields
}

RuleSet composes multiple rules.

func NewRuleSet

func NewRuleSet(name string, rules ...Rule) *RuleSet

NewRuleSet creates a new rule set.

func (*RuleSet) Add

func (rs *RuleSet) Add(rules ...Rule) *RuleSet

Add adds rules to the set.

func (*RuleSet) Name

func (rs *RuleSet) Name() string

Name returns the rule set name.

func (*RuleSet) Validate

func (rs *RuleSet) Validate(value any) []*ValidationError

Validate runs all rules and returns all errors.

func (*RuleSet) ValidateFirst

func (rs *RuleSet) ValidateFirst(value any) *ValidationError

ValidateFirst runs rules until first error.

type ValidationError

type ValidationError struct {
	Rule    string
	Field   string
	Message string
	Value   any
}

ValidationError represents a validation failure.

func (*ValidationError) Error

func (e *ValidationError) Error() string

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator validates values against rule sets.

func NewValidator

func NewValidator(rules *RuleSet) *Validator

NewValidator creates a new validator.

func (*Validator) Validate

func (v *Validator) Validate(value any) *Result

Validate checks a value against all rules.

func (*Validator) WithWarnings

func (v *Validator) WithWarnings(rules *RuleSet) *Validator

WithWarnings adds warning rules (non-fatal).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL