validator

package module
v0.0.0-...-9efa4c9 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2019 License: MIT Imports: 8 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// Unknown is unresolved OS type
	Unknown = iota
	// Win is Windows type
	Win
	// Unix is *nix OS types
	Unix
)

Used by IsFilePath func

View Source
const (
	TAG_NAME = "valid"
)

Variables

View Source
var (
	RegexValidatorMap map[string]string
	// Map of validators
	ValidatorsFn = map[string]ValidateFn{
		"required": requiredFn,
		"len":      lenFn,

		"eqf":  eqfieldFn,
		"nef":  nefieldFn,
		"gtf":  gtfieldFn,
		"gtef": gtefieldFn,
		"ltf":  ltfieldFn,
		"ltef": ltefieldFn,

		"eq": eqFn,
		"ne": neFn,

		"gt":  gtFn,
		"gte": gteFn,
		"lt":  ltFn,
		"lte": lteFn,

		"in":  inFn,
		"nin": ninFn,

		"after":  afterFn,
		"before": afterFn,
	}
)

Functions

func AddRegexValidateFn

func AddRegexValidateFn(name, regex string)

func IsEmpty

func IsEmpty(v reflect.Value) bool

func Lookup

func Lookup(s interface{}, path string) interface{}

func RegisterValidator

func RegisterValidator(id string, fn ValidateFn) error

func ToBoolean

func ToBoolean(str string) (bool, error)

ToBoolean convert the input string to a boolean.

func ToFloat

func ToFloat(str string) (float64, error)

ToFloat convert the input string to a float, or 0.0 if the input is not a float.

func ToInt

func ToInt(value interface{}) (res int64, err error)

ToInt convert the input string or any int type to an integer type 64, or 0 if the input is not an integer.

func ToJSON

func ToJSON(obj interface{}) (string, error)

ToJSON convert the input to a valid JSON string

func ToString

func ToString(obj interface{}) string

ToString convert the input to a string.

Types

type Error

type Error struct {
	Message   string
	Attribute string
	Validator string
}

func Array

func Array(s interface{}) []*Error

func Struct

func Struct(s interface{}) ([]*Error, bool)

type Errors

type Errors []Error

type ValidateFn

type ValidateFn func(i interface{}, o interface{}, v *ValidatorOption) error

type Validator

type Validator struct {
	Wg           sync.WaitGroup
	Errors       []*Error
	ValidatorsFn map[string]ValidateFn
}

func New

func New() *Validator

func (*Validator) AddError

func (t *Validator) AddError(e *Error) *Validator

func (*Validator) Declared

func (t *Validator) Declared(validator string) bool

func (*Validator) GetValidatorFn

func (t *Validator) GetValidatorFn(validator string) (ValidateFn, bool)

func (*Validator) ParseTag

func (v *Validator) ParseTag(tag, attribute string) []ValidatorOption

type ValidatorOption

type ValidatorOption struct {
	ID        string
	Attribute string
	Optional  bool
	Params    []string
	Fn        ValidateFn
}

func (*ValidatorOption) Exec

func (t *ValidatorOption) Exec(val, valueField reflect.Value) (err *Error)

Jump to

Keyboard shortcuts

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