validation

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package validation provides input validation utilities for API handlers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FieldError

type FieldError struct {
	Field   string `json:"field"`
	Message string `json:"message"`
}

FieldError represents a validation error for a specific field.

type ValidationError

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

ValidationError represents a collection of validation errors.

func (*ValidationError) Error

func (e *ValidationError) Error() string

Error implements the error interface.

func (*ValidationError) HasErrors

func (e *ValidationError) HasErrors() bool

HasErrors returns true if there are validation errors.

type Validator

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

Validator collects validation errors.

func New

func New() *Validator

New creates a new Validator.

func (*Validator) AddError

func (v *Validator) AddError(field, message string)

AddError adds a validation error for a field.

func (*Validator) Email

func (v *Validator) Email(field, value string)

Email validates that a string is a valid email address.

func (*Validator) Error

func (v *Validator) Error() error

Error returns the validation error if there are errors, nil otherwise.

func (*Validator) HasErrors

func (v *Validator) HasErrors() bool

HasErrors returns true if there are validation errors.

func (*Validator) MaxLength

func (v *Validator) MaxLength(field, value string, max int)

MaxLength validates that a string doesn't exceed max length (in runes).

func (*Validator) MinLength

func (v *Validator) MinLength(field, value string, min int)

MinLength validates that a string has at least min length (in runes).

func (*Validator) NoControlChars

func (v *Validator) NoControlChars(field, value string)

NoControlChars validates that a string doesn't contain control characters (except common whitespace).

func (*Validator) NoDangerousChars

func (v *Validator) NoDangerousChars(field, value string)

NoDangerousChars validates that a string doesn't contain potentially dangerous characters. This is a basic sanitization check for fields that shouldn't contain special chars.

func (*Validator) NonNegative

func (v *Validator) NonNegative(field string, value int)

NonNegative validates that an int is non-negative (>= 0).

func (*Validator) NonNegativeFloat

func (v *Validator) NonNegativeFloat(field string, value float64)

NonNegativeFloat validates that a float64 is non-negative (>= 0).

func (*Validator) OneOf

func (v *Validator) OneOf(field, value string, allowed []string)

OneOf validates that a string is one of the allowed values.

func (*Validator) Positive

func (v *Validator) Positive(field string, value int)

Positive validates that an int is positive (> 0).

func (*Validator) PositiveFloat

func (v *Validator) PositiveFloat(field string, value float64)

PositiveFloat validates that a float64 is positive (> 0).

func (*Validator) Range

func (v *Validator) Range(field string, value, min, max int)

Range validates that an int is within range (inclusive).

func (*Validator) RangeFloat

func (v *Validator) RangeFloat(field string, value, min, max float64)

RangeFloat validates that a float64 is within range (inclusive).

func (*Validator) Required

func (v *Validator) Required(field, value string)

Required validates that a string field is not empty.

func (*Validator) RequiredInt

func (v *Validator) RequiredInt(field string, value int)

RequiredInt validates that an int field is not zero.

func (*Validator) RequiredInt64

func (v *Validator) RequiredInt64(field string, value int64)

RequiredInt64 validates that an int64 field is not zero.

func (*Validator) URL

func (v *Validator) URL(field, value string)

URL validates that a string looks like a URL (basic check).

Jump to

Keyboard shortcuts

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