Documentation
¶
Index ¶
- func Configure[T any](v *Validator, fn func(builder Configurator[T], m *T))
- type Configurator
- func (c Configurator[T]) Custom(...)
- func (c Configurator[T]) Field[F any](fieldPtr *F) *shared.Field[F]
- func (c Configurator[T]) Map[K comparable, V any](mapFieldPtr *map[K]V) *shared.MapFieldConfigurator[K, V]
- func (c Configurator[T]) Slice[E any](sliceFieldPtr *[]E) *shared.SliceFieldConfigurator[E]
- func (c Configurator[T]) Tags(name string)
- func (c Configurator[T]) When(fn func(ctx context.Context, obj *T) bool) Configurator[T]
- type Option
- type Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Configure ¶
func Configure[T any](v *Validator, fn func(builder Configurator[T], m *T))
Configure configures validation rules for the struct type T, replacing the rules of previous Configure calls for T. fn receives a model with all nested pointer structs allocated, rules are configured by pointers to the model fields. Configure panics on invalid configuration, call it on application start and not concurrently with Validate.
Types ¶
type Configurator ¶
type Configurator[T any] struct { // contains filtered or unexported fields }
Configurator configures validation rules for the struct type T. Field pointers passed to its methods must point to the fields of the model passed to the Configure function.
func (Configurator[T]) Custom ¶
func (c Configurator[T]) Custom(fn func(ctx context.Context, h shared.StructCustomHelper, obj *T) []shared.Error)
Custom adds a custom validation function on the whole struct. The helper passed to fn is valid only during the fn call.
func (Configurator[T]) Field ¶ added in v0.1.0
func (c Configurator[T]) Field[F any](fieldPtr *F) *shared.Field[F]
Field returns the base configurator of the field, convert it to a typed one with As: b.Field(&m.Name).As(str.Of), b.Field(&m.Discount).As(num.Ptr).
func (Configurator[T]) Map ¶ added in v0.1.0
func (c Configurator[T]) Map[K comparable, V any](mapFieldPtr *map[K]V) *shared.MapFieldConfigurator[K, V]
Map returns the configurator of the map field with the rules of the map itself, EachKey and EachValue: b.Map(&m.Labels).MaxLen(10).EachKey(func(k *shared.Field[string]) { k.As(str.Of).Hostname() }).
func (Configurator[T]) Slice ¶
func (c Configurator[T]) Slice[E any](sliceFieldPtr *[]E) *shared.SliceFieldConfigurator[E]
Slice returns the configurator of the slice field with the rules of the slice itself, convert it to a typed one with As for the rules of elements: b.Slice(&m.Tags).As(str.Slice). A pointer to a slice field is converted from its field: b.Field(&m.Tags).As(shared.SlicePtr).
func (Configurator[T]) Tags ¶ added in v0.1.0
func (c Configurator[T]) Tags(name string)
Tags configures the rules of the tag name of the fields of T and of its nested structs in the syntax of go-playground/validator, validate:"required,max=64", for a migration from it: the tags and the rules in code can be mixed in one Configure. The rules are the rules of valigo with their messages, codes and constraints: required is Required, min and max are the lengths in characters of strings, the values of numbers and the lengths of slices and maps, dive applies the next rules to the elements of a slice or the values of a map[string]V. The rules of the fields of a nil nested struct are skipped, like by go-playground/validator, unlike the rules in code. See the documentation for the list of the rules. Tags panics on an unknown rule, like Configure on invalid rules.
func (Configurator[T]) When ¶
func (c Configurator[T]) When(fn func(ctx context.Context, obj *T) bool) Configurator[T]
When returns a Configurator whose rules are applied only if fn returns true. Conditions of chained When calls are combined with AND.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option is an interface that defines a single method, apply, which is used to apply an option to a Validator.
func WithErrorsTransformer ¶ added in v0.0.2
WithErrorsTransformer returns an Option that sets the transformer for the Validator.
func WithFieldLocationNamingFn ¶
WithFieldLocationNamingFn returns an Option that sets the field location naming function for the Validator.
func WithTranslator ¶
func WithTranslator(t translator.Translator) Option
WithTranslator returns an Option that sets the translator for the Validator.
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator holds validation rules of the configured types.
func New ¶
New creates a new Validator instance with default values for storage and helper. It also applies any options passed to the function to the new instance.
func (*Validator) Constraints ¶ added in v0.1.0
func (v *Validator) Constraints() []shared.Constraint
Constraints returns the rules of the configured structs that JSON Schema can express, see shared.Constraint, for the generators of the schemas of the API. The structs are sorted by their names, the rules are in the order of the configuration.
func (*Validator) GetHelper ¶
GetHelper returns the helper object associated with the Validator instance.
func (*Validator) Validate ¶
Validate is similar to ValidateTyped, but returns errors transformed by the errors transformer. It returns nil if there are no errors.
func (*Validator) ValidateTyped ¶
ValidateTyped validates obj using the rules configured for its type. obj is a pointer to the configured struct. A struct value is validated by a shallow copy: modifications like Trim of its direct fields are not visible to the caller, but values behind pointers and slice elements are shared with the caller and can be modified. It returns nil if there are no errors, no rules for the obj type or obj is a nil pointer.
Directories
¶
| Path | Synopsis |
|---|---|
|
integrations
|
|
|
echo
module
|
|
|
gin
module
|
|
|
goi18n
module
|
|
|
huma
module
|
|
|
internal
|
|
|
helper
Package helper builds the errors of the rules with the translated messages and the locations of the fields.
|
Package helper builds the errors of the rules with the translated messages and the locations of the fields. |
|
tags
Package tags configures the rules of the tags of go-playground/validator by the rules of valigo.
|
Package tags configures the rules of the tags of go-playground/validator by the rules of valigo. |
|
testenv
Package testenv describes the environment of the tests.
|
Package testenv describes the environment of the tests. |
|
zero
Package zero allocates the pointer fields of the models of Configure, so the rules are configured by the pointers to the fields of the nested structs.
|
Package zero allocates the pointer fields of the models of Configure, so the rules are configured by the pointers to the fields of the nested structs. |
|
Package problem writes validation errors as the problem details of RFC 9457 (RFC 7807 before it): application/problem+json with the errors of the fields.
|
Package problem writes validation errors as the problem details of RFC 9457 (RFC 7807 before it): application/problem+json with the errors of the fields. |
|
rules
|
|
|
eq
Package eq configures the rules of comparable values: enums, bools, named types.
|
Package eq configures the rules of comparable values: enums, bools, named types. |
|
timex
Package timex configures the rules of time.Time values: b.Field(&m.StartsAt).As(timex.Of).After(start).
|
Package timex configures the rules of time.Time values: b.Field(&m.StartsAt).As(timex.Of).After(start). |
|
uuid
module
|
|