validation

package
v0.0.0-...-8e8e47b Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ValidatorsSep is the separator between validation names. For example: "required,oneof=THIS THAT".
	ValidatorsSep = ","

	// NameAndInstructionsSep is the separator between the validation name and the instructions.
	// For example: "oneof=THIS THAT".
	NameAndInstructionsSep = "="

	// Tag is the name of the struct field tag.
	//
	// type Example struct {
	//     Value *int `validate:"required,gt=0"`
	// }
	//
	// The tag contents contains the validators and their respective instructions.
	Tag = "validate"
)

Variables

This section is empty.

Functions

func DereferenceAndNilCheck

func DereferenceAndNilCheck(value reflect.Value) (reflect.Value, error)

DereferenceAndNilCheck is used to get the base type and ensure it's not nil.

func MustRegisterValidator

func MustRegisterValidator(name Validator, callback Callback)

MustRegisterValidator sets the callback for a validator.

func Struct

func Struct[T any](val T) error

Struct validates all struct fields using their validation tags, returning an error if any fail. In the case that the struct has tag violations, a Violations error is returned.

func Var

func Var[T any](val T, validatorInstructions string) error

Var validates a single variable with the given instructions, returning an error if it fails. In the case that the variable has tag violations, a Violations error is returned.

Types

type Callback

type Callback func(*CallbackParameters) *CallbackResult

Callback checks a value against the instructions for the validator.

type CallbackParameters

type CallbackParameters struct {
	Validator          Validator
	IsStructValidation bool
	StructValue        reflect.Value
	StructFieldName    string
	Value              reflect.Value
	Parameters         string
}

CallbackParameters are the parameters sent to the validation callback. Struct fields are only set on Struct validation.

type CallbackResult

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

CallbackResult instructs the validation on how to proceed after the validator is complete.

func NewCallbackResult

func NewCallbackResult() *CallbackResult

NewCallbackResult instantiates a CallbackResult.

func (*CallbackResult) AddValue

func (c *CallbackResult) AddValue(val reflect.Value) *CallbackResult

AddValue adds a new value in the CallbackResult.

func (*CallbackResult) WithError

func (c *CallbackResult) WithError(err error) *CallbackResult

WithError sets the error on the CallbackResult.

func (*CallbackResult) WithStop

func (c *CallbackResult) WithStop() *CallbackResult

WithStop sets the stop value in the CallbackResult.

type Validator

type Validator string

Validator is the name of a validate rule. For example: oneof, required, dive, etc...

const (
	GreaterThanValidatorName        Validator = "gt"
	GreaterThanOrEqualValidatorName Validator = "gte"
	LessThanValidatorName           Validator = "lt"
	LessThanOrEqualValidatorName    Validator = "lte"
)
const (
	LenValidatorName Validator = "len"
	MinValidatorName Validator = "min"
	MaxValidatorName Validator = "max"
)
const (
	DiveValidatorName Validator = "dive"
)
const (
	FilepathValidatorName Validator = "filepath"
)
const (
	IPAddrValidatorName Validator = "ip_addr"
)
const (
	OmitemptyValidatorName Validator = "omitempty"
)
const (
	OneOfValidatorName Validator = "oneof"
)
const (
	RequiredIfValidatorName Validator = "required_if"
)
const (
	RequiredValidatorName Validator = "required"
)

type Violation

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

Violation represents a failure for a specific validator.

func NewViolation

func NewViolation(params *CallbackParameters, err error) *Violation

NewViolation instantiates a *Violation.

func (*Violation) Error

func (v *Violation) Error() string

Error ensures Violation has the error interface.

type Violations

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

Violations represents a list of violations.

func NewViolations

func NewViolations() *Violations

NewViolations instantiates a *Violations struct.

func (*Violations) AddViolation

func (v *Violations) AddViolation(other *Violation)

AddViolation appends another violation to this list of violations.

func (*Violations) AddViolations

func (v *Violations) AddViolations(others *Violations)

AddViolations appends other violations.

func (*Violations) Error

func (v *Violations) Error() string

Error ensures Violations has the error interface.

func (*Violations) NilIfEmpty

func (v *Violations) NilIfEmpty() error

NilIfEmpty returns nil if the violation list is empty.

Jump to

Keyboard shortcuts

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