govalid: github.com/twharmon/govalid Index | Examples | Files

package govalid

import "github.com/twharmon/govalid"

Index

Examples

Package Files

constraint.go float32_constraint.go float64_constraint.go govalid.go int64_constraint.go int_constraint.go model.go nil_constraint.go string_constraint.go validator.go

Variables

var ErrNotRegistered = errors.New("structs must be registered before validating")

ErrNotRegistered is encountered when an attempt is made to validate a type that has not yet been registered.

var ErrNotStruct = errors.New("only structs can be validated")

ErrNotStruct is encountered when an attempt is made to validate a type that is not a struct is made.

type Validator Uses

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

Validator .

Code:

v := govalid.New()
type User struct {
    Name string `govalid:"req|max:8"`
}
v.Register(User{})
vio, _ := v.Violation(&User{"foobarbaz"})
fmt.Println(vio)

Output:

Name can not be longer than 8 characters

func New Uses

func New() *Validator

New .

func (*Validator) AddCustom Uses

func (v *Validator) AddCustom(s interface{}, f ...func(interface{}) string) error

AddCustom adds custom validation functions to struct s.

func (*Validator) Register Uses

func (v *Validator) Register(structs ...interface{}) error

Register is required for all structs that you wish to validate. It is intended to be ran at load time and caches information about the structs to reduce run time allocations.

func (*Validator) Violation Uses

func (v *Validator) Violation(s interface{}) (string, error)

Violation checks the struct s against all constraints and custom validation functions, if any. It returns an violation if the struct fails validation. If the type being validated is not a struct, ErrNotStruct will be returned. If the type being validated has not yet been registered, ErrNotRegistered is returned.

func (*Validator) Violations Uses

func (v *Validator) Violations(s interface{}) ([]string, error)

Violations checks the struct s against all constraints and custom validation functions, if any. It returns a slice of violations if the struct fails validation. If the type being validated is not a struct, ErrNotStruct will be returned. If the type being validated has not yet been registered, ErrNotRegistered is returned.

Package govalid imports 7 packages (graph). Updated 2020-10-22. Refresh now. Tools for package owners.