playground

package
v0.2.0 Latest Latest
Warning

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

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

Documentation

Overview

Package playground adapts github.com/go-playground/validator to Celerity's validation seam.

app := celerity.New(celerity.WithValidator(playground.New()))

type CreateOrder struct {
    CustomerID string  `json:"customerId" validate:"required"`
    Total      float64 `json:"total"      validate:"gt=0"`
}

It lives in its own package rather than in core so that an application which doesn't require validation does not link it. Importing it is the only thing that does.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Validator)

Option configures the validator.

func WithMessage

func WithMessage(tag, message string) Option

WithMessage sets what a caller is told when a rule rejects their input.

Use it for rules registered elsewhere: the library's own rules, an alias, or a struct-level check registered through WithValidate.

func WithMessageFunc

func WithMessageFunc(tag string, message func(validator.FieldError) string) Option

WithMessageFunc sets a message composed from the failure, for a rule whose explanation depends on its parameter or on the value.

playground.WithMessageFunc("divisibleby", func(fe validator.FieldError) string {
    return "must be divisible by " + fe.Param()
})

func WithRule

func WithRule(tag string, fn validator.Func, message string) Option

WithRule registers a custom rule and what to tell a caller who breaks it.

playground.New(
    playground.WithRule("sku", validSKU, "must be a SKU, such as AB-1234"),
)

type CreateOrder struct {
    SKU string `json:"sku" validate:"required,sku"`
}

The rule and its message are registered together because a rule without one is reported as "failed the sku rule", which says less than it could to the one person who needs it.

func WithValidate

func WithValidate(v *validator.Validate) Option

WithValidate supplies a preconfigured *validator.Validate, for an application that registers rules, aliases or struct-level checks of its own.

The field-name function is still applied over it, so paths stay in the caller's vocabulary. Give its rules messages with WithMessage.

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator checks struct tags and reports what failed as Celerity issues.

func New

func New(opts ...Option) *Validator

New returns a validator over struct tags.

func (*Validator) Validate

func (a *Validator) Validate(input any) error

Validate satisfies celerity.Validator.

Jump to

Keyboard shortcuts

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