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 ¶
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 ¶
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 ¶
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.