validator

package
v1.3.10 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package validator provides request validation for REST APIs using go-playground/validator with support for decimal types and JSON-based field naming.

The validator converts struct validation errors to a simple ValidationError slice with JSON-friendly field names (from struct tags, not Go field names), which is safe for inclusion in API error responses. It implements the Echo validator interface.

Basic usage:

type User struct {
    Email    string          `json:"email" validate:"required,email"`
    Age      int             `json:"age" validate:"gte=0,lte=150"`
    Balance  decimal.Decimal `json:"balance" validate:"gte=0"`
}

v := validator.DefaultRestValidator()
if err := v.Validate(&user); err != nil {
    // err is ValidationErrors with JSON field names
}

Custom validation tags and rules can be registered via the underlying Validator field.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ValidationError

type ValidationError struct {
	Field   string `json:"field"`
	Tag     string `json:"tag"`
	Value   string `json:"value"`
	Message string `json:"message"`
}

type ValidationErrors

type ValidationErrors []ValidationError

func (ValidationErrors) Error

func (v ValidationErrors) Error() string

type Validator

type Validator struct {
	Validator *validator.Validate
}

func DefaultRestValidator

func DefaultRestValidator() *Validator

func (*Validator) RegisterCustomValidation

func (v *Validator) RegisterCustomValidation(tag string, fn validator.Func) error

func (*Validator) RegisterStructValidation

func (v *Validator) RegisterStructValidation(fn validator.StructLevelFunc, types ...any)

func (*Validator) Validate

func (v *Validator) Validate(i any) error

Jump to

Keyboard shortcuts

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