valval

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

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

Go to latest
Published: Jun 7, 2019 License: MIT Imports: 6 Imported by: 4

README

Valval GoDoc

Valval is simple validation library for Go.

Defines a validator

personValidator := valval.Object(valval.M{
	"Name": valval.String(
		valval.MaxLength(20),
		valval.Regexp(regexp.MustCompile(`^[a-z ]+$`)),
	),
	"Attr": valval.Object(valval.M{
		"Age": valval.Number(
			valval.Min(0),
			valval.Max(120),
		),
		"Gender": valval.String(valval.In("male", "female")),
		"Tags": valval.Slice(
			valval.String(
				valval.MinLength(1),
				valval.MaxLength(10),
			),
		).Self(valval.MaxSliceLength(10)),
	}),
})

validate a struct

type PersonAttr struct {
	Age    int
	Gender string
	Tags   []string
}
type Person struct {
	Name string
	Attr PersonAttr
}
person := &Person{
	Name: "John!",
	Attr: PersonAttr{
		Age:    200,
		Gender: "otoko",
		Tags:   []string{"", "abcdefghijklmn"},
	},
}
if err := personValidator.Validate(person); err != nil {
	errs := valval.Errors(err)
	for _, errInfo := range errs {
		fmt.Printf("%s : %v\n", errInfo.Path, errInfo.Error)
	}
}
/* outputs
Attr.Age : must be 200 or less
Attr.Gender : invalid value. allowed are [male female]
Attr.Tags[0] : length must be 1 or greater
Attr.Tags[1] : length must be 10 or less
Name : must be match to the regexp ^[a-z ]+$
*/

and validate a map

personMap := map[string]interface{}{
	"Name": "John!",
	"Attr": map[string]interface{}{
		"Age":    200,
		"Gender": "otoko",
		"Tags":   []string{"", "abcdefghijklmn"},
	},
}
if err := personValidator.Validate(personMap); err != nil {
	errs := valval.Errors(err)
	for _, errInfo := range errs {
		fmt.Printf("%s : %v\n", errInfo.Path, errInfo.Error)
	}
}
// outputs same as struct

Install

go get github.com/wacul/valval

Documentation

Overview

Package valval is validation library

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorDescription

type ErrorDescription struct {
	Path  string
	Error error
}

func Errors

func Errors(err error) []ErrorDescription

func ErrorsBase

func ErrorsBase(err error, basePath string) []ErrorDescription

func JSONErrors

func JSONErrors(err error) []ErrorDescription

func JSONErrorsBase

func JSONErrorsBase(err error, basePath string) []ErrorDescription

type M

type M map[string]Validator

type ObjectValidator

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

func Object

func Object(m M) *ObjectValidator

func (*ObjectValidator) Self

func (*ObjectValidator) Validate

func (ov *ObjectValidator) Validate(val interface{}) error

type ObjectValidatorFunc

type ObjectValidatorFunc func(content map[string]interface{}) error

func RequiredFields

func RequiredFields(fs ...string) ObjectValidatorFunc

type SliceValidator

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

func Slice

func Slice(inner Validator) *SliceValidator

func (*SliceValidator) Self

func (*SliceValidator) Validate

func (sv *SliceValidator) Validate(slice interface{}) error

type SliceValidatorFunc

type SliceValidatorFunc func(slice []interface{}) error

func MaxSliceLength

func MaxSliceLength(max int) SliceValidatorFunc

func MinSliceLength

func MinSliceLength(min int) SliceValidatorFunc

type Validator

type Validator interface {
	Validate(val interface{}) error
}

type ValidatorFunc

type ValidatorFunc func(val interface{}) error

func And

func And(funcs ...ValidatorFunc) ValidatorFunc

func GreaterThan

func GreaterThan(min float64) ValidatorFunc

func In

func In(targetValues ...interface{}) ValidatorFunc

func LessThan

func LessThan(max float64) ValidatorFunc

func Max

func Max(max float64) ValidatorFunc

func MaxLength

func MaxLength(max int) ValidatorFunc

func Min

func Min(min float64) ValidatorFunc

func MinLength

func MinLength(min int) ValidatorFunc

func NewBoolValidator

func NewBoolValidator(inner func(bool) error) ValidatorFunc

func NewFloatValidator

func NewFloatValidator(inner func(float64) error) ValidatorFunc

func NewIntValidator

func NewIntValidator(inner func(int64) error) ValidatorFunc

func NewStringValidator

func NewStringValidator(inner func(string) error) ValidatorFunc

func Or

func Or(funcs ...ValidatorFunc) ValidatorFunc

func Regexp

func Regexp(r *regexp.Regexp) ValidatorFunc

type ValueValidator

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

func Any

func Any(vfuncs ...ValidatorFunc) *ValueValidator

func Bool

func Bool(vfuncs ...ValidatorFunc) *ValueValidator

func Number

func Number(vfuncs ...ValidatorFunc) *ValueValidator

func String

func String(vfuncs ...ValidatorFunc) *ValueValidator

func (*ValueValidator) Validate

func (v *ValueValidator) Validate(val interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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