validate

package
v0.17.1 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2026 License: MIT Imports: 5 Imported by: 2

Documentation

Overview

Package validate exposes the struct-tag constraint helper used by Den.

Den runs Document automatically on every Insert and Update — there is no opt-in option, and there is no way to bypass tag-level constraints from inside Den. Call Document directly only at boundaries before the doc reaches Den (typical use: HTTP handlers that want to reject bad input before opening a database transaction). The returned *Errors mirrors what Den's write path would have produced.

The struct-tag syntax follows go-playground/validator/v10:

type Product struct {
    document.Base
    Name  string  `json:"name"  validate:"required,min=3"`
    Email string  `json:"email" validate:"required,email"`
    Price float64 `json:"price" validate:"required,min=0"`
}

Index

Constants

This section is empty.

Variables

View Source
var DefaultValidator = validator.New()

DefaultValidator is the singleton go-playground validator instance used by Struct and (internally) by Den's write path. Exposed so consumers that need to register custom validation functions can do so once for both surfaces.

Functions

func Document added in v0.12.0

func Document(doc document.Document) error

Document validates doc against its `validate` struct tags using DefaultValidator. Returns nil on success, *Errors on validation failure, or a raw error for malformed input (e.g. a nil pointer).

The parameter type is the marker interface every Den document type satisfies by embedding document.Base, so attempts to validate arbitrary non-document structs fail at compile time — use go-playground/validator/v10 directly for that case.

Den's write path calls Document automatically; this entry point is for validating outside the Den boundary (HTTP handlers, form parsers, pre-save checks).

Types

type Errors

type Errors struct {
	Fields []FieldError
}

Errors holds all field-level validation failures.

func (*Errors) Error

func (e *Errors) Error() string

type FieldError

type FieldError struct {
	Field string // struct field name
	Tag   string // validation tag that failed (e.g. "required", "min")
	Value any    // actual value
	Param string // tag parameter (e.g. "3" for min=3)
}

FieldError describes a single field that failed validation.

Jump to

Keyboard shortcuts

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