validation

package module
v0.0.0-...-f432e2a Latest Latest
Warning

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

Go to latest
Published: May 18, 2018 License: GPL-3.0 Imports: 7 Imported by: 0

README

go-validation Build Status

Provides validations for struct fields based on a validation tag

See godoc for more info: http://godoc.org/github.com/BakedSoftware/go-validation

Usage Examples

type MyType struct {
    Name       string    `validation:"format=regexp:[A-Z][a-z]{3,12}"`
    Email      string    `validation:"format=email"`
    Category   string    `validation:"min_length=5 max_length=10"`
    Age        uint      `validation:"min=18"`
    Quantity   uint      `validation:"min=1 max=5"`
    Total      float32   `validation:"min=0"`
}

Documentation

Overview

Package validation provides validations for struct fields based on a validation tag

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultMap = Map{}

DefaultMap is the default validation map used to tell if a struct is valid.

Functions

func AddValidation

func AddValidation(key string, fn func(string, reflect.Kind) (Interface, error))

AddValidation registers the validation specified by key to the known validations. If more than one validation registers with the same key, the last one will become the validation for that key using DefaultValidationMap.

Types

type Interface

type Interface interface {
	SetFieldIndex(index int)
	FieldIndex() int
	SetFieldName(name string)
	FieldName() string
	Validate(value interface{}, obj reflect.Value) *ValidationError
}

Interface specifies the necessary methods a validation must implement to be compatible with this package

type Map

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

Map is an atomic validation map when two Set happen at the same time, latest that started wins.

func (*Map) AddValidation

func (vm *Map) AddValidation(key string, fn func(string, reflect.Kind) (Interface, error))

AddValidation registers the validation specified by key to the known validations. If more than one validation registers with the same key, the last one will become the validation for that key.

func (*Map) IsValid

func (vm *Map) IsValid(object interface{}) (bool, []ValidationError)

IsValid determines if an object is valid based on its validation tags.

type Validation

type Validation struct {
	Name string
	// contains filtered or unexported fields
}

Validation is an implementation of a Interface and can be used to provide basic functionality to a new validation type through an anonymous field

func (*Validation) FieldIndex

func (v *Validation) FieldIndex() int

FieldIndex retrieves the index of the field the validation was applied to

func (*Validation) FieldName

func (v *Validation) FieldName() string

FieldName retrieves the name of the field the validation was applied to

func (*Validation) SetFieldIndex

func (v *Validation) SetFieldIndex(index int)

SetFieldIndex stores the index of the field the validation was applied to

func (*Validation) SetFieldName

func (v *Validation) SetFieldName(name string)

SetFieldName stores the name of the field the validation was applied to

func (*Validation) Validate

func (v *Validation) Validate(value interface{}, obj reflect.Value) *ValidationError

Validate determines if the value is valid. Nil is returned if it is valid

type ValidationError

type ValidationError struct {
	Key     string
	Message string
}

func IsValid

func IsValid(object interface{}) (bool, []ValidationError)

IsValid determines if an object is valid based on its validation tags using DefaultValidationMap.

Example (Format)
type Person struct {
	// Email must be valid email
	Email string `validation:"format=email"`
}

var p Person

ok, errs := IsValid(p)
fmt.Println(ok, errs)
Output:

func (*ValidationError) Error

func (e *ValidationError) Error() string

type ValidationErrors

type ValidationErrors []ValidationError

func (ValidationErrors) Error

func (e ValidationErrors) Error() string

Jump to

Keyboard shortcuts

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