vld

package module
v0.0.0-...-6ee8d94 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2017 License: MIT Imports: 3 Imported by: 0

README

#vld

Validator engine for Golang. Validate struct fields or write custom validations as functions. Chain them together and make your code cleaner and easier to reason.2

#Installation

Make sure that Go is installed on your computer. Type the following command in your terminal:

go get github.com/mkasner/vld

#Example Example struct we want to validate with the following rules:

  • Field Kind is required and must not be longer then 10 characters
  • Feld year is required and must not be less than 1900 and greater than 3000
type Wine struct {
  Kind string
  Year int
}

The ususal way would be something like this


func (w *Wine) Validate() error {
    if w.Kind == "" {
      return errors.New("Wine.Kind required")
    }
    if len(w.Kind) > 10 {
      return errors.New("Wine.Kind must be smaller than 10 characters")
    }
    if w.Year == 0 {
      return errors.New("Wine.Year is required")
    }
    if w.Year < 1900 {
      return errors.New("Wine.Year must be greater than 1900")
    }
    if w.Year > 3000 {
      return errors.New("Wine.Year must be smaller than 3000")
    }
    return nil
}
func isValid(w *Wine) bool {
      if err := w.Validate(); err != nil {
        return true
      }
      return false
}

Or with the help of vld utility

//go:generate vld -type=Wine
package cellar

type Wine struct {
  Kind string `vld:"req,maxlen=10"`
  Year int  `vld:"req,maxint=3000,minint=1900"`
}

func isValid(w Wine) bool {
    if err := vld.New().Add(w).Validate(); err != nil {
      return true
    }
    return false
}

It generates struct validator for you, so you can just call validate method on struct and get results. #Thanks

Documentation

Overview

Package for declaring and executing validations which can be techninal validations, bussiness validations, struct validations

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	Err error
}

Passes data between validations. Used for error passing at the moment.

type DefaultValidation

type DefaultValidation struct {
}

Default struct which you can embed into your struct that implements ValidateOverride method. In that way you only must implement Validate method

func (DefaultValidation) ValidateOverride

func (f DefaultValidation) ValidateOverride(ctx Context) Context

type Validation

type Validation interface {
	Validate(Context) Context
	ValidateOverride(Context) Context
}

Every type which wants to act as validation must implement this interface

type ValidationFunc

type ValidationFunc func() error

Type that holds validation func which implements Validation interface

func MaxFloat32

func MaxFloat32(limit, value float32, name string) ValidationFunc

vld:maxfloat32

func MaxFloat64

func MaxFloat64(limit, value float32, name string) ValidationFunc

vld:maxfloat64

func MaxInt

func MaxInt(limit, value int, name string) ValidationFunc

vld:maxint

func MaxInt64

func MaxInt64(limit, value int64, name string) ValidationFunc

vld:maxint64

func MaxLength

func MaxLength(length int, value, name string) ValidationFunc

vld:maxlen

func Message

func Message(f ValidationFunc, message string) ValidationFunc

func MinFloat32

func MinFloat32(limit, value float32, name string) ValidationFunc

vld:minfloat32

func MinFloat64

func MinFloat64(limit, value float32, name string) ValidationFunc

vld:minfloat64

func MinInt

func MinInt(limit, value int, name string) ValidationFunc

vld:minint

func MinInt64

func MinInt64(limit, value int64, name string) ValidationFunc

vld:minint64

func MinLength

func MinLength(length int, value, name string) ValidationFunc

vld:minlen

func Required

func Required(value interface{}, name string) ValidationFunc

vld:req

func (ValidationFunc) Validate

func (f ValidationFunc) Validate(ctx Context) Context

func (ValidationFunc) ValidateOverride

func (f ValidationFunc) ValidateOverride(ctx Context) Context

type Validator

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

Holds and runs validations

func New

func New() *Validator

Creates new Validator on which you can chain validations

func (*Validator) Add

func (t *Validator) Add(v Validation) *Validator

Adds validation to validator

func (*Validator) AddFunc

func (t *Validator) AddFunc(f func() error) *Validator

Add regular func to validator It will act as validation on validator

func (*Validator) Validate

func (t *Validator) Validate() error

Validate and exit at first error

func (*Validator) ValidateAll

func (t *Validator) ValidateAll() VldErrors

Validates all validations and collects errors

func (*Validator) Validations

func (t *Validator) Validations(v Validation) []Validation

Returns validations on this validator

type VldErrors

type VldErrors struct {
	Err []error
}

Container for all validation errors. Used when ValidateAll function call

Directories

Path Synopsis
cmd
vld

Jump to

Keyboard shortcuts

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