validate

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 24, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package validate implements the tag-driven struct validator used by pkg/server (HTTP request binding) and pkg/mcp (typed-tool argument binding). It lives in internal/ so both packages can import it without creating a cycle between pkg/server and pkg/mcp.

The exported surface — Validate, ValidationError, FieldError — is re-exposed by pkg/server via type aliases for callers that consume the validator through the public server API.

Supported `validate` tag verbs:

required           field must be a non-zero value
min=N              numeric/length >= N
max=N              numeric/length <= N
len=N              length exactly N (string/slice/map)
email              loose RFC 5322 sanity check on the local-part/domain
oneof=A B C        value must equal one of the (space-separated) options
url                must parse via net/url and have a scheme + host

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FieldName

func FieldName(sf reflect.StructField, parent string) string

FieldName resolves the wire-level name of a struct field: the json tag if present and non-empty, else the Go field name. The optional parent prefix produces dotted paths for nested structs.

func Struct

func Struct(dst any) error

Struct runs `validate:"..."` rules over dst (must be a pointer to a struct or a struct). Returns a *ValidationError when any rule fails, or nil otherwise. Nested structs are recursed into; pointers are dereferenced.

Types

type FieldError

type FieldError struct {
	Field   string // JSON-tag name if present, else struct field name.
	Tag     string // The verb that failed (required, min, oneof, …).
	Param   string // The argument to the verb (the N in min=N, etc.).
	Value   any    // The field's actual value.
	Message string // Human-readable rendering.
}

FieldError describes one failed validation rule.

func (*FieldError) Error

func (f *FieldError) Error() string

Error implements error.

type ValidationError

type ValidationError struct {
	Fields []*FieldError
}

ValidationError is the aggregate error returned by Validate when one or more fields fail their rules.

func (*ValidationError) Error

func (e *ValidationError) Error() string

Error implements error. Returns a single line summarising all failures so it's safe to log without further formatting.

func (*ValidationError) HasField

func (e *ValidationError) HasField(field string) bool

HasField reports whether the validation error has an entry for `field` (matched against FieldError.Field).

Jump to

Keyboard shortcuts

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