validate

package
v0.1.138 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package validate holds the rules generated Validate methods call and the error type they return. It has no state.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Email

func Email(s string) bool

Email reports whether s is a single address without a display name.

func Pattern

func Pattern(re, s string) bool

Pattern reports whether s matches the anchored regular expression re. Compiled expressions are cached; the set is bounded by the patterns in the schema.

func Timezone

func Timezone(s string) bool

Timezone reports whether s names an IANA time zone ("Europe/Paris", "UTC"). The zone database is embedded (time/tzdata), so the answer does not depend on the host.

func URL

func URL(s string) bool

URL reports whether s is an absolute http(s) URL.

Types

type Errors

type Errors struct {
	Fields []FieldError `json:"fields"`
}

Errors is the error a Validate method returns when any rule fails. Its JSON form is what handlers send with status 422.

func (*Errors) Add

func (e *Errors) Add(field, rule, message string)

Add records a failure. It is safe on a nil *Errors.

func (*Errors) Error

func (e *Errors) Error() string

func (*Errors) MarshalJSON

func (e *Errors) MarshalJSON() ([]byte, error)

MarshalJSON renders {"error": "validation", "fields": [...]}.

func (*Errors) Result

func (e *Errors) Result() error

Result returns e as an error when it has failures, else nil.

type FieldError

type FieldError struct {
	// Field is the JSON name of the field.
	Field string `json:"field"`
	// Rule is the rule that failed: required, min, max, email, url,
	// pattern, enum, decimal (not a number that fits decimal(p, s)).
	Rule    string `json:"rule"`
	Message string `json:"message"`
}

FieldError is one failed rule on one field.

type Validator

type Validator interface {
	Validate() error
}

Validator is implemented by generated types; the typed router calls it on request bodies.

Jump to

Keyboard shortcuts

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