validation

package
v0.1.15 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// JSONDriver is the JSON struct field driver.
	//
	// Uses `json` field tag.
	JSONDriver = CodecDriver(iota) + 1
	// YAMLDriver is the YAML struct field driver.
	//
	// Uses `yaml` field tag.
	YAMLDriver
	// XMLDriver is the XML struct field driver.
	//
	// Uses `xml` field tag.
	XMLDriver
	// TOMLDriver is the TOML struct field driver.
	//
	// Uses `toml` field tag.
	TOMLDriver
	// EnvironmentDriver is the environment struct field driver.
	//
	// Uses `env` field tag.
	EnvironmentDriver
)

Variables

View Source
var (
	ErrInvalidCodecDriver = errors.New("validation: codec driver is invalid")
)

Functions

This section is empty.

Types

type CodecDriver added in v0.1.13

type CodecDriver uint8

CodecDriver is the codec algorithm to use for struct tag field scanning.

As validators scan struct fields, some of them might define tags for specific codecs (e.g. json). This causes a mismatch between the base and the encoded names and lead to confusion for routine callers.

A CodecDriver defines which tags of a codec mechanism to use for validator operations like error generation.

func ParseCodecDriver added in v0.1.13

func ParseCodecDriver(v string) (CodecDriver, error)

ParseCodecDriver allocates a new CodecDriver instance based on its string value.

func (CodecDriver) MarshalText added in v0.1.13

func (d CodecDriver) MarshalText() (text []byte, err error)

func (CodecDriver) String added in v0.1.13

func (d CodecDriver) String() string

func (*CodecDriver) UnmarshalText added in v0.1.13

func (d *CodecDriver) UnmarshalText(text []byte) error

type GoPlaygroundValidator

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

GoPlaygroundValidator a concrete implementation of Validator using go-playground/validator package.

func NewGoPlaygroundValidator

func NewGoPlaygroundValidator(opts ...Option) GoPlaygroundValidator

NewGoPlaygroundValidator allocates a new GoPlaygroundValidator instance.

func (GoPlaygroundValidator) Validate

func (g GoPlaygroundValidator) Validate(ctx context.Context, v any) error

Validate validates the given value. Returns error if one or more validations failed.

type Option added in v0.1.13

type Option func(*options)

Option is a function that modifies the validator behavior.

func WithCodecDriver added in v0.1.13

func WithCodecDriver(driver CodecDriver) Option

WithCodecDriver sets the codec driver to be used by the validator.

func WithRuleFunc added in v0.1.13

func WithRuleFunc(name string, fn ValidateFunc) Option

WithRuleFunc adds a custom validation rule routine to the validator.

func WithRules added in v0.1.13

func WithRules(rules ...Rule) Option

WithRules adds a set of custom validation rules to the validator.

type Rule added in v0.1.13

type Rule struct {
	// Name is the name of the validation rule.
	Name string
	// ValidateFunc is the function that performs the validation.
	ValidateFunc ValidateFunc
}

Rule represents a validation rule with a name and a validation function.

func NewDateRule added in v0.1.13

func NewDateRule() Rule

NewDateRule is a custom validation function that checks if the provided value is a valid date string in the format "YYYY-MM-DD".

type ValidateFunc added in v0.1.13

type ValidateFunc func(fieldName string, value any) bool

ValidateFunc is a function type that defines a custom validation function.

type Validator

type Validator interface {
	// Validate validates the given structure (v).
	Validate(ctx context.Context, v any) error
}

Validator is a utility component used by systems to validate structures.

Jump to

Keyboard shortcuts

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