validation

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package validation provides input validation with detailed error messages.

Package validation provides input validation with detailed error messages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateBody

func ValidateBody(params []ParamDef) func(http.Handler) http.Handler

ValidateBody is a helper middleware that only validates request body.

func ValidateQuery

func ValidateQuery(params []ParamDef) func(http.Handler) http.Handler

ValidateQuery is a helper middleware that only validates query parameters.

func ValidationMiddleware

func ValidationMiddleware(bodyParams, queryParams []ParamDef) func(http.Handler) http.Handler

ValidationMiddleware creates middleware that validates request query parameters and body. It validates query params first, then body params. If either fails, it returns a 400 error.

Types

type ParamDef

type ParamDef struct {
	Name      string
	Type      string
	Required  bool
	Default   any
	Min       *float64
	Max       *float64
	MinLength *int
	MaxLength *int
	Pattern   *regexp.Regexp
	Enum      []string
	Custom    func(any) error
}

ParamDef defines validation rules for a parameter.

type ValidationError

type ValidationError struct {
	Field      string `json:"field"`
	Value      any    `json:"value,omitempty"`
	Rule       string `json:"rule"`
	Message    string `json:"message"`
	Suggestion string `json:"suggestion,omitempty"`
}

ValidationError represents a single validation error with details.

func (ValidationError) Error

func (e ValidationError) Error() string

Error implements the error interface.

type ValidationErrors

type ValidationErrors struct {
	Errors []ValidationError `json:"errors"`
}

ValidationErrors aggregates multiple validation errors.

func (*ValidationErrors) Error

func (e *ValidationErrors) Error() string

Error implements the error interface.

func (*ValidationErrors) HasErrors

func (e *ValidationErrors) HasErrors() bool

HasErrors returns true if there are any validation errors.

type Validator

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

Validator validates data against parameter definitions.

func NewValidator

func NewValidator() *Validator

NewValidator creates a new Validator instance.

func (*Validator) Validate

func (v *Validator) Validate(data map[string]any, params []ParamDef) *ValidationErrors

Validate validates data against the provided parameter definitions. Returns nil if validation passes, or ValidationErrors if validation fails.

Jump to

Keyboard shortcuts

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