govalid

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2016 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Data validation library. For introduction, visit https://github.com/gima/govalid/blob/master/README.md

Data type verification

The supplied Validators verify data's type implicitly; they don't need extra parameters during their construction to do so.

// This means, for example, that the following code verifies that "asd" is of
// string type, even though no parameters are given to the String validator.
path, err := govalid.String().Validate("asd")

Supported data types

The supplied validators understand the following data types:

bool, array, map, ptr, slice, string and any numerical type except complex

This leaves out

complex, chan, func, interface, struct and unsafepointer

(Don't be fooled by missing interface{} support, as this library uses reflection to reach values, and reflection goes from interface value to reflection object.)

Regarding pointers

It is recommended to pass pointers to the Validators, as this avoids making copies of data and thus avoids unnecessary garbage collection. Make no mistake, non-pointers work perfectly fine as well.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayOpt

type ArrayOpt func(slice *reflect.Value) (path string, err error)

Array validation function. Parameter is reflection to array or slice.

func ArrEach

func ArrEach(validator Validator) ArrayOpt

Array validator function for checking each entry with the specified validator.

func ArrMax

func ArrMax(max int) ArrayOpt

Array validator function for checking maximum number of entries.

func ArrMin

func ArrMin(min int) ArrayOpt

Array validator function for checking minimum number of entries.

type BooleanOpt

type BooleanOpt func(bool) error

Boolean validation function.

func BoolIs

func BoolIs(expected bool) BooleanOpt

Validation function for checking boolean value.

type NumberOpt

type NumberOpt func(float64) error

Number validation function.

func NumIs

func NumIs(expected float64) NumberOpt

Number validator function for checking value.

func NumMax

func NumMax(max float64) NumberOpt

Number validator function for checking maximum value.

func NumMin

func NumMin(min float64) NumberOpt

Number validator function for checking minimum value.

type ObjectOpt

type ObjectOpt func(m *reflect.Value) (path string, err error)

Object validation function. Parameter is reflection to a map.

func ObjKV

func ObjKV(key interface{}, v Validator) ObjectOpt

Array validator function for validating a specific key's value (of key->value pair) with the specified validator. If the map under validation doesn't have such key, nil is passed to the Validator (hint: Optional Validator).

keys keys keys

func ObjKeys

func ObjKeys(v Validator) ObjectOpt

Array validator function for validating every key (of key->value pair) with the specified validator.

func ObjValues

func ObjValues(v Validator) ObjectOpt

Array validator function for validating every value (of key->value pair) with the specified validator.

type StringOpt

type StringOpt func(*string) error

String validator function.

func StrIs

func StrIs(expected string) StringOpt

String validator function for checking string equality.

func StrMax

func StrMax(maxlen int) StringOpt

String validator function for checking maximum string length.

func StrMin

func StrMin(minlen int) StringOpt

String validator function for checking minimum string length.

func StrRegExp

func StrRegExp(expr string) StringOpt

String validator function for checking regular expression match of the string. If regular expression has error, the error is returned when validation is performed.

type Validator

type Validator interface {
	// Validate the given data.
	// In the case of a failed validation, path indicates which validator failed.
	Validate(interface{}) (path string, err error)
}

Validator validates any data type that the implementing validator supports.

func And

func And(validators ...Validator) Validator

Construct a logical-and validator using the specified validators. Given no validators, this validator passes always.

func Array

func Array(opts ...ArrayOpt) Validator

Construct an array validator using the specified validation functions. Validates data type to be either an array or a slice, or a pointer to such.

func Boolean

func Boolean(opts ...BooleanOpt) Validator

Construct a boolean validator using the specified validation functions. Validates data type to be either a bool or a pointer to such.

func Function

func Function(validatorfunc ValidatorFunc) Validator

Construct a validator using a validation function.

func Number

func Number(opts ...NumberOpt) Validator

Construct a number validator using the specified validation functions. Validates data type to be either any numerical type or a pointer to such (except a complex number).

func Object

func Object(opts ...ObjectOpt) Validator

Construct an object validator using the specified validation functions. Validates data type to be either a map or a pointer to such

Currently this validator supports only map data type.

func Optional

func Optional(validator Validator) Validator

Construct an optional validator using the specified validator. Validation succeeds when given nil or a pointer to nil.

Note: json.Unmarshal() converts JSON null to Go's nil.

func Or

func Or(validators ...Validator) Validator

Construct a logical-or validator using the specified validators. Given no validators, this validator passes always.

func String

func String(opts ...StringOpt) Validator

Construct a string validator using the specified validator functions. Validates data type to be either a string or a pointer to such.

type ValidatorFunc

type ValidatorFunc func(data interface{}) (path string, err error)

Arbitrary validation function.

Source Files

  • and.go
  • array.go
  • boolean.go
  • doc.go
  • function.go
  • jsonv.go
  • number.go
  • object.go
  • optional.go
  • or.go
  • string.go

Directories

Path Synopsis
This package is internal, do not use externally.
This package is internal, do not use externally.

Jump to

Keyboard shortcuts

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