Documentation
¶
Overview ¶
Package validation provides a programmatic, type-safe validation engine using Go generics.
Overview ¶
Validation in Credo uses generic Rule interfaces and pointer-based field references for compile-time type safety. No struct tags are used for rule definition. Reflection is limited to field name extraction (cached after first use per struct type).
Quick Start ¶
import v "github.com/credo-go/credo/validation"
type CreateUserInput struct {
Name string `json:"name"`
Email string `json:"email"`
Age int `json:"age"`
}
func (c *CreateUserInput) Validate() error {
return v.ValidateStruct(c,
v.Field(&c.Name, v.Required[string](), v.Length(2, 100)),
v.Field(&c.Email, v.Required[string](), v.Email()),
v.Field(&c.Age, v.Required[int](), v.Min(18)),
)
}
When the struct implements Validatable, the Validate method is called automatically by BindBody and BindQuery. In debug mode, a warning is logged if the target does not implement Validatable.
Nested Structs ¶
A Field validates a field that implements Validatable by calling its Validate once the field's rules pass, and prefixes the failures with the field's name (address.city); a field whose rules fail reports their violations only. A slice or array of such values, structs whose pointer implements Validatable included, has each element validated, its failures prefixed with the field's name and the index (items[0].name). A struct field's or element's Validate runs on the value itself, so what a pointer-receiver Validate normalizes is kept. A nil pointer or nil interface is valid and its Validate is not called, so an optional nested object may be omitted, and a mandatory one takes NotNil:
func (o *CreateOrderInput) Validate() error {
return v.ValidateStruct(o,
v.Field(&o.Shipping), // Address: always validated
v.Field(&o.Billing), // *Address: validated when set
v.Field(&o.Contact, v.NotNil[Contact]()), // *Contact: must be set, then validated
v.Field(&o.Items), // []LineItem: each element validated
)
}
A violation names a field by its json tag, or the Go field name when the tag gives none. BindBody and BindQuery rename the violations a target's Validate returns to the tag of the source they decoded: form for a form or multipart body, query for a query.
Error Format ¶
Validation errors are returned as Errors ([]ValidationError), designed for centralized error-envelope integration. Each error includes Field, Code, Message, and optional Params for i18n template rendering.
PATCH Support ¶
For partial updates with pointer fields, use NilSafe:
v.Field(&u.Name, v.NilSafe(v.Length(2, 100)))
Adapted From ¶
API design adapted from ozzo-validation (MIT). Generic Rule architecture inspired by govy (design only, no code adapted). See NOTICES file for full attribution.
Maturity: beta
Index ¶
- func ValidateStruct(structPtr any, fields ...FieldRules) error
- type Errors
- type FieldRules
- type MessageKey
- type Rule
- func AllowedExtensions(exts ...string) Rule[*multipart.FileHeader]
- func AllowedMimeTypes(types ...string) Rule[*multipart.FileHeader]
- func Between[T cmp.Ordered](min, max T) Rule[T]
- func By[T any](fn func(T) error) Rule[T]
- func DateAfter(threshold time.Time) Rule[time.Time]
- func DateBefore(threshold time.Time) Rule[time.Time]
- func Each[T any](rules ...Rule[T]) Rule[[]T]
- func Email() Rule[string]
- func In[T comparable](values ...T) Rule[T]
- func Length(min, max int) Rule[string]
- func Max[T cmp.Ordered](threshold T) Rule[T]
- func MaxFileSize(maxBytes int64) Rule[*multipart.FileHeader]
- func Min[T cmp.Ordered](threshold T) Rule[T]
- func NilSafe[T any](rules ...Rule[T]) Rule[*T]
- func NotEmptyMap[K comparable, V any]() Rule[map[K]V]
- func NotEmptySlice[E any]() Rule[[]E]
- func NotNil[T any]() Rule[*T]
- func Regex(pattern *regexp.Regexp) Rule[string]
- func Required[T comparable]() Rule[T]
- func URL() Rule[string]
- func UUID() Rule[string]
- func When[T any](condition bool, rules ...Rule[T]) Rule[T]
- type Validatable
- type ValidationError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ValidateStruct ¶
func ValidateStruct(structPtr any, fields ...FieldRules) error
ValidateStruct validates a struct by running all field rules. Returns nil if all validations pass, or Errors containing all failures. A rule error that is neither a *ValidationError nor Errors is internal: validation stops at once and ValidateStruct returns that error unchanged.
structPtr must be a non-nil pointer to a struct. Passing a nil pointer returns nil (valid). Passing a non-pointer or pointer to non-struct panics.
Types ¶
type Errors ¶
type Errors []ValidationError
Errors is a collection of validation errors. It implements the error interface and json.Marshaler for error-envelope integration.
func (Errors) MarshalJSON ¶
MarshalJSON implements json.Marshaler. Serializes as a JSON array.
func (Errors) MarshalJSONTo ¶ added in v0.6.0
MarshalJSONTo implements the encoding/json/v2 marshaler interface, streaming the underlying slice without the intermediate []byte that MarshalJSON would otherwise force on v2 encoders.
type FieldRules ¶
type FieldRules interface {
// contains filtered or unexported methods
}
FieldRules is a type-erased container returned by Field. The unexported methods prevent external implementation — only Field can produce values satisfying this interface.
func Field ¶
func Field[T any](fieldPtr *T, rules ...Rule[T]) FieldRules
Field creates a FieldRules that validates the struct field pointed to by fieldPtr using the given rules. The type parameter T is inferred from fieldPtr.
Once the rules pass, a field whose value implements Validatable is validated by its Validate (nested struct support), and its failures are prefixed with the field's name; a field whose rules fail reports their violations only. A slice or array whose elements implement Validatable, or are structs whose pointer does, has each element validated the same way, its failures prefixed with the field's name and the element's index, as in items[0].name. A struct field's or element's Validate runs on the value itself, so what a pointer-receiver Validate normalizes is kept. A nil pointer or nil interface is valid and its Validate is not called; presence is what NotNil and Required check, so
validation.Field(&r.Billing, validation.NotNil[Address]())
requires the address and then validates it.
type MessageKey ¶ added in v0.26.0
type MessageKey string
MessageKey is a catalog key: the exact key of a message in the application's translation catalog. It is never rendered as text — a key the catalog does not define falls back to the built-in message for the code — so literal client text goes elsewhere ([credo.HTTPError.WithMessage]), and client input goes into details. The root package aliases it as credo.MessageKey.
The named type keeps a key a literal or a typed constant: a key computed as a string, such as one concatenated from a request value, does not compile without a conversion. Declare key constants with the type, so a scan of the sources finds them.
type Rule ¶
Rule is the generic validation rule interface. The type parameter T matches the field type, providing compile-time type safety.
func AllowedExtensions ¶
func AllowedExtensions(exts ...string) Rule[*multipart.FileHeader]
AllowedExtensions creates a Rule that restricts the uploaded file's extension (taken from multipart.FileHeader.Filename) to the given list. Extensions are compared case-insensitively, and a leading dot is optional in the allow-list — "jpg" and ".jpg" are equivalent. A file whose name has no extension fails unless "" is explicitly allowed. A nil file header passes — use Required to enforce presence.
func AllowedMimeTypes ¶
func AllowedMimeTypes(types ...string) Rule[*multipart.FileHeader]
AllowedMimeTypes creates a Rule that restricts the uploaded file's declared MIME type to the given list. The MIME type is read from the part's Content-Type header, which is client-supplied and therefore not authoritative — pair it with content sniffing (e.g. http.DetectContentType) when stronger guarantees are needed. Comparison is case-insensitive and ignores media-type parameters such as "; charset=utf-8". A nil file header passes — use Required to enforce presence.
func By ¶
By creates a Rule from an inline function. The type parameter T is inferred from the function signature.
The function reports a client-visible failure with a *ValidationError — NewError builds one — or Errors. Any other error is internal: validation stops and the error leaves Validate unchanged, so a lookup that could not reach its database is a server failure, not invalid input.
validation.Field(&c.Code, validation.By(func(code string) error {
if len(code) != 2 {
return validation.NewError("country_code", "must be a 2-letter code")
}
return nil
}))
func DateAfter ¶
DateAfter creates a Rule that validates the time is after the given threshold. Zero time passes validation — use Required to enforce non-zero.
func DateBefore ¶
DateBefore creates a Rule that validates the time is before the given threshold. Zero time passes validation — use Required to enforce non-zero.
func Each ¶
Each creates a Rule that validates each element of a slice against the given rules. Error fields use bracket notation: "[0]", "[1]", etc.
func Email ¶
Email creates a Rule that validates email format. Empty strings pass validation — use Required to enforce non-empty.
func In ¶
func In[T comparable](values ...T) Rule[T]
In creates a Rule that checks the value is one of the allowed values. The allowed values are not included in the error message for security.
func Length ¶
Length creates a Rule that validates string length (rune count) is between min and max (inclusive). Uses utf8.RuneCountInString for correct Unicode handling. Empty strings pass validation — use Required to enforce non-empty.
func MaxFileSize ¶
func MaxFileSize(maxBytes int64) Rule[*multipart.FileHeader]
MaxFileSize creates a Rule that fails if the uploaded file is larger than maxBytes. The size is read from multipart.FileHeader.Size, which the multipart parser fills in from the request. A nil file header passes — use Required to enforce presence.
func NilSafe ¶
NilSafe wraps rules for use with pointer fields. When the pointer is nil, validation is skipped. When non-nil, the value is dereferenced and inner rules execute. Used for PATCH/partial update support.
type UpdateUserInput struct {
Name *string `json:"name"`
}
validation.Field(&u.Name, validation.NilSafe(validation.Length(2, 100)))
func NotEmptyMap ¶
func NotEmptyMap[K comparable, V any]() Rule[map[K]V]
NotEmptyMap creates a Rule that fails when the map has no entries (nil or zero length). It is the map counterpart of Required, whose comparable constraint excludes maps; the check is len-based — no reflection.
validation.Field(&c.Limits, validation.NotEmptyMap[string, int]())
func NotEmptySlice ¶
NotEmptySlice creates a Rule that fails when the slice has no elements (nil or zero length). It is the slice counterpart of Required, whose comparable constraint excludes slices; the check is len-based — no reflection.
validation.Field(&o.Items, validation.NotEmptySlice[Item]())
func Regex ¶
Regex creates a Rule that validates the value matches the given pattern. The pattern must be pre-compiled via regexp.MustCompile or regexp.Compile. Empty strings pass validation — use Required to enforce non-empty. Panics if pattern is nil — a nil pattern is a programming error, never a runtime condition.
func Required ¶
func Required[T comparable]() Rule[T]
Required creates a Rule that fails if the value is the zero value for type T. Works with any comparable type: strings, ints, bools, etc.
func URL ¶
URL creates a Rule that validates URL format. The URL must have both a scheme and a host. Empty strings pass validation — use Required to enforce non-empty.
func UUID ¶
UUID creates a Rule that validates UUID format. Accepts both hyphenated (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) and plain (xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx) formats. Empty strings pass validation — use Required to enforce non-empty.
func When ¶
When creates a Rule that conditionally applies rules based on a boolean condition. When the condition is false, validation is skipped (returns nil). When true, all inner rules are executed and errors are collected.
validation.Field(&o.CardNumber,
validation.When(o.PaymentMethod == "card",
validation.Required[string](), validation.Length(13, 19),
),
)
type Validatable ¶
type Validatable interface {
Validate() error
}
Validatable is implemented by types that can validate themselves. When a target struct implements Validatable, the Bind methods (BindBody, BindQuery) automatically call Validate() after decoding ("parse, don't validate").
Nested fields that implement Validatable, and the elements of a slice or array of them, are validated by Field once the field's rules pass; a nil nested pointer is valid.
type ValidationError ¶
type ValidationError struct {
// Field is the field path, e.g. "name", "address.city", "items[0]".
Field string `json:"field"`
// Code is the rule identifier / i18n key, e.g. "required", "email", "min".
Code string `json:"code"`
// Message is the default English error message.
Message string `json:"message"`
// MessageKey is an optional catalog key for the message. It is used by
// Credo's i18n error pipeline and is never serialized to clients.
MessageKey MessageKey `json:"-"`
// Params holds template variables for localized messages,
// e.g. {"min": 2, "max": 100}.
//
// Params is serialized into the HTTP error response (deliberately —
// clients use it to render localized messages). Custom rules must not
// place internal or sensitive values here.
Params map[string]any `json:"params,omitempty"`
}
ValidationError represents a single field validation failure.
func NewError ¶ added in v0.24.0
func NewError(code, message string) *ValidationError
NewError returns a client-visible validation failure with the given rule code and message, for a custom rule or an inline By function. The field path is filled in by Field, as for the built-in rules.
return validation.NewError("country_code", "must be a 2-letter code")
A rule error that is neither a *ValidationError nor Errors is internal: validation stops and the error is returned unchanged, so the error pipeline classifies it like any handler error and its text never reaches the client.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
Error implements the error interface.