Help us shape the future of Go! Take the 2026 Go Developer Survey

valigo

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

README

codecov build Goreport GoDoc

Valigo

Valigo is a powerfull, zero allocations validation engine with localizations, conditions and custom validation functions support.

Features

  • Conditinal validation
  • Custom validation functions
  • Localizations
  • Zero allocations
  • Configured using pointers to structure fields

Roadmap

  • Zero allocations on valid structs
  • On Field Conditional validation
  • On Field Custom validation
  • On Struct Conditional validation
  • On Struct Custom validation
  • Error translations
  • Strings (MaxLen, MinLen, Required, Regexp Pattern, AnyOf, Custom) and Strings Slices validation
  • UUID and UUID Slices validation
  • Num Validation (int(8,16,32,64), uint(8,16,32,64))
  • Float validation
  • Other default types validations
  • Create validation rules based on default validations tags

Documentation

Index

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 a Validator instance for a specific type T, i.e.: var t *T. It creates a new instance of type T, allocates values for all fields recursively, creates a Configurator instance for the type T, calls the provided function fn with the Configurator instance and the instance of type T, and appends any user-defined validators to the Configurator instance.

Types

type Configurator

type Configurator[T any] interface {
	str.StringBundleConfigurator
	num.NumberBundleConfigurator
	uuid.UUIDBundleConfigurator

	// StringSlice returns str.StringSliceFieldConfigurator for slice of strings validation
	StringSlice(sliceFieldPtr any) *str.StringSliceFieldConfigurator
	// UUIDSlice returns *uuid.UUIDSliceFieldConfigurator for slice of UUID's validation
	UUIDSlice(sliceFieldPtr any) *uuid.UUIDSliceFieldConfigurator
	// Slice return shared.SliceFieldConfigurator for slice validation
	Slice(sliceFieldPtr any) *shared.SliceFieldConfigurator
	// When sets a condition for when the validator should be applied.
	When(func(ctx context.Context, obj *T) bool) Configurator[T]
	// Custom adds a custom validation function to the validator.
	Custom(fn func(ctx context.Context, h shared.StructCustomHelper, obj *T) []shared.Error)
}

Configurator is an interface that defines methods for building validators for any type T.

type NumberBuilder

type NumberBuilder[T ~int | int8 | int16 | int32 | int64 | *int | *int8 | *int16 | *int32 | *int64 |
	uint | uint8 | uint16 | uint32 | uint64 | *uint | *uint8 | *uint16 | *uint32 | *uint64 |
	float64 | float32 | *float64 | *float32] interface {
	// Max sets a maximum value for the validator.
	Max(T) NumberBuilder[T]
	// Min sets a minimum value for the validator.
	Min(T) NumberBuilder[T]
	// Custom adds a custom validation function to the validator.
	Custom(func(ctx context.Context, h *shared.Helper, value *T) []error) NumberBuilder[T]
	// When sets a condition for when the validator should be applied.
	When(func(ctx context.Context, value *T) bool) NumberBuilder[T]
}

NumberBuilder is an interface that defines methods for building validators for numeric types.

type NumbersBundleBuilder

type NumbersBundleBuilder interface {
	// Int returns a NumberBuilder for validating an int field.
	Int(field *int) NumberBuilder[int]
	// Int8 returns a NumberBuilder for validating an int8 field.
	Int8(field *int8) NumberBuilder[int8]
	// Int16 returns a NumberBuilder for validating an int16 field.
	Int16(field *int16) NumberBuilder[int16]
	// Int32 returns a NumberBuilder for validating an int32 field.
	Int32(field *int32) NumberBuilder[int32]
	// Int64 returns a NumberBuilder for validating an int64 field.
	Int64(field *int64) NumberBuilder[int64]
	// IntPtr returns a NumberBuilder for validating a pointer to an int field.
	IntPtr(field **int) NumberBuilder[*int]
	// Int8Ptr returns a NumberBuilder for validating a pointer to an int8 field.
	Int8Ptr(field **int8) NumberBuilder[*int8]
	// Int16Ptr returns a NumberBuilder for validating a pointer to an int16 field.
	Int16Ptr(field **int16) NumberBuilder[*int16]
	// Int32Ptr returns a NumberBuilder for validating a pointer to an int32 field.
	Int32Ptr(field **int32) NumberBuilder[*int32]
	// Int64Ptr returns a NumberBuilder for validating a pointer to an int64 field.
	Int64Ptr(field **int64) NumberBuilder[*int64]
	// Uint returns a NumberBuilder for validating an uint field.
	Uint(field *uint) NumberBuilder[uint]
	// Uint8 returns a NumberBuilder for validating an uint8 field.
	Uint8(field *uint8) NumberBuilder[uint8]
	// Uint16 returns a NumberBuilder for validating an uint16 field.
	Uint16(field *uint16) NumberBuilder[uint16]
	// Uint32 returns a NumberBuilder for validating an uint32 field.
	Uint32(field *uint32) NumberBuilder[uint32]
	// Uint64 returns a NumberBuilder for validating an uint64 field.
	Uint64(field *uint64) NumberBuilder[uint64]
	// UintPtr returns a NumberBuilder for validating a pointer to an uint field.
	UintPtr(field **uint) NumberBuilder[*uint]
	// Uint8Ptr returns a NumberBuilder for validating a pointer to an uint8 field.
	Uint8Ptr(field **uint8) NumberBuilder[*uint8]
	// Uint16Ptr returns a NumberBuilder for validating a pointer to an uint16 field.
	Uint16Ptr(field **uint16) NumberBuilder[*uint16]
	// Uint32Ptr returns a NumberBuilder for validating a pointer to an uint32 field.
	Uint32Ptr(field **uint32) NumberBuilder[*uint32]
	// Uint64Ptr returns a NumberBuilder for validating a pointer to an uint64 field.
	Uint64Ptr(field **uint64) NumberBuilder[*uint64]
}

NumbersBundleBuilder is an interface that defines methods for building validators for numeric types.

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

func WithErrorsTransformer(fn func(errs []shared.Error) []error) Option

WithErrorsTransformer returns an Option that sets the transformer for the Validator.

func WithFieldLocationNamingFn

func WithFieldLocationNamingFn(fn func(field fmap.Field) string) Option

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 SlicesBundleBuilder

type SlicesBundleBuilder interface {
	// SliceStrings returns a StringSliceBuilder for validating a string slice.
	SliceStrings(value *[]string) StringSliceBuilder[string]
	// SlicePtrStrings returns a StringSliceBuilder for validating a pointer to a string slice.
	SlicePtrStrings(value **[]string) StringSliceBuilder[string]
	// SliceStringsPtr returns a StringSliceBuilder for validating a string slice of pointers.
	SliceStringsPtr(value *[]*string) StringSliceBuilder[*string]
	// SlicePtrStringsPtr returns a StringSliceBuilder for validating a pointer to a string slice of pointers.
	SlicePtrStringsPtr(value **[]*string) StringSliceBuilder[*string]
}

SlicesBundleBuilder is an interface that defines methods for building validators for string slice types.

type StringSliceBuilder

type StringSliceBuilder[T string | *string] interface {
	// Trim removes leading and trailing whitespace from each string in the slice.
	Trim() StringSliceBuilder[T]
	// Max sets a maximum length for each string in the slice.
	Max(uint) StringSliceBuilder[T]
	// Min sets a minimum length for each string in the slice.
	Min(uint) StringSliceBuilder[T]
	// Unique ensures that all strings in the slice are unique.
	Unique() StringSliceBuilder[T]
}

StringSliceBuilder is an interface that defines methods for building validators for string slices.

type Validator

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

Validator is a struct that holds a storage and a helper object.

func New

func New(opts ...Option) *Validator

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) GetHelper

func (v *Validator) GetHelper() shared.Helper

GetHelper returns the helper object associated with the Validator instance.

func (*Validator) Validate

func (v *Validator) Validate(ctx context.Context, obj any) []error

Validate is similar to ValidateTyped, but it returns a slice of error objects instead of shared.Error objects.

func (*Validator) ValidateTyped

func (v *Validator) ValidateTyped(ctx context.Context, obj any) []shared.Error

ValidateTyped validates an object of any type using validators from the storage. It takes a context.Context and an object as input and returns a slice of shared.Error objects. If no validators are found for the object's type, it returns nil.

Directories

Path Synopsis
integrations
echo module
gin module
goi18n module
huma module
rules
uuid module

Jump to

Keyboard shortcuts

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