Documentation ¶
Index ¶
- Constants
- func DereferenceAndNilCheck(value reflect.Value) (reflect.Value, error)
- func MustRegisterValidator(name Validator, callback Callback)
- func Struct[T any](val T) error
- func Var[T any](val T, validatorInstructions string) error
- type Callback
- type CallbackParameters
- type CallbackResult
- type Validator
- type Violation
- type Violations
Constants ¶
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 ¶
DereferenceAndNilCheck is used to get the base type and ensure it's not nil.
func MustRegisterValidator ¶
MustRegisterValidator sets the callback for a validator.
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 (
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.
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.