orwell

package module
v0.0.93 Latest Latest
Warning

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

Go to latest
Published: May 31, 2018 License: MIT Imports: 7 Imported by: 0

README

Orwell

Orwell is a validator package for the Go programming language.

Features

  • Quick and simple to use
  • Easy to extend with custom validation rules
  • Some of the most common validation rules baked in
  • Many cross-field validation rules (e.g. dependant struct fields)
  • Pure Go, no tags
  • No third party dependencies
  • Avoids complex regexes and reflection whenever possible

Usage

Simple data validation

Get a new validator instance:

import (
	"github.com/Antipitch/orwell"
)

v := orwell.NewValidator()

Validate something:

i := 42

err := v.Validate(i, v.Required())

if err != nil {
    log.Print(err.Error())
}

Add more Rules:

err := v.Validate(i, v.Required(), v.Max(42), v.DivBy(21))

Rules after failing rules wil not be applied:

// v.DivBy() will not be called
err := v.Validate(i, v.Required(), v.Max(41), v.DivBy(20))

Nil or empty values are always valid except for the Required(), NotNil(), NotEmpty() and X-Rules:

var (
    i int
    err error
)

// valid
err = v.Validate(i, v.Min(42))

// not valid
err = v.Validate(i, v.NotEmpty(), v.Min(42))

i = 41

// not valid
err = v.Validate(i, v.NotEmpty(), v.Min(42))
Struct Validation

Use pointers both to the struct itself and to the fields to validate (fields passed as arguments to X-rules don't have to be pointers).

s := {
    StringField: "somestring",
    IntField: 1000,
    StructField: {
        ChildStringFiled: "",
        ChildIntField: 0
    } 
}

// will return orwell.IterableError interface (s.Intfield is not valid)
err := v.ValidateStruct(&s,
    v.FieldRules(&s.StringField, v.Required()),
    v.FieldRules(&s.StructField.ChildIntField, v.XLt(s.IntField, 1000))
)
Error handling

Orwell exports three error interfaces: InternalError, IterableError and FieldableError (all of them implement Go's error interface).

Orwell's Validate method will -for now- return a standard Go error, nothing special here. The ValidateStruct method will either return an InternalError or an IterableError. Proceed according to your needs with type assertion:

err := v.ValidateStruct(
    // validate some struct
)

if err != nil {
    // don't react specifically, maybe log
    log.Print(err.Error())

    // internal error
    internalError, ok := err.(orwell.InternalError)
    if ok {
        // do something with internal error
    }

    // iterable error
    iterableError, ok := err.(orwell.IterableError)
    if ok {
        l := iterableError.Len()
        for i := 0; i < l; i++ {
            if fe, ok := iterableError.ValueAt(i).(orwell.FieldableError); ok {
                f := fe.FieldName
                j := fe.JSONName
                e := fe.Error()
            }
        }
    }
}

Rules

Simple rules
  • DivBy(arg int) Must be dividable by arg
  • In(args ...interface{}) Must be equal to one of args
  • Email(arg bool) Must be RFC 5322 compliant address (uses net/mail.ParseAddress and optionally net.LookupMX)
  • LengthMax(arg int) Must be of length not greater than arg
  • LengthMin(arg int) Must be of length not less than arg
  • Max(arg int) Must not be greater than arg
  • Min(arg int) Must not be lower than arg
  • Match(arg string) Must match regular expression described by arg
  • NotNil() Must not be nil
  • Required() Must neither be nil nor empty
X-Rules
  • XAnd(arg interface{}) Required when arg is neither nil nor empty
  • XAndOr(arg interface{}, args ...interface{}) Required when arg is neither nil nor empty and all args are nil or empty
  • XGt(arg1 interface{}, arg2 int) Required when arg1 is greater than arg2
  • XGtEql(arg1 interface{}, arg2 int, arg3 interface{}, arg4 interface{}) Required when arg1 is greater than arg2 and arg3 is equal to arg4
  • XGtEqlOr(arg1 interface{}, arg2 int, arg3 interface{}, arg4 interface{}, args ...interface{}) Required when arg1 is greater than arg2, arg3 is equal to arg4 and all args are nil or empty
  • XGtOr(arg1 interface{}, arg2 int, args ...interface{}) Required when arg1 is greater than arg2 and all args are nil or empty
  • XLt(arg1 interface{}, arg2 int) Required when arg1 is lower than arg2
  • XLtOr(arg1 interface{}, arg2 int, args ...interface{}) Required when arg1 is lower than arg2 and all args are nil or empty
  • XNor(args ...interface{}) Must be nil or empty when all args are nil or empty
  • XNot(arg interface{}) Must be nil or empty when arg is neither nil nor empty
  • XOr(args ...interface{}) Required when all args are nil or empty

Orwell is a new project and not stable, yet. Don't use it in production! Thankful respects to asaskevich/govalidator and go-ozzo/ozzo-validation for inspiration. Feel free to contribute.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeepEqual added in v0.0.92

func DeepEqual(s interface{}, c interface{}) bool

func IsEmpty

func IsEmpty(value interface{}) bool

IsEmpty func

func IsNil

func IsNil(value interface{}) (interface{}, bool)

IsNil func

func LengthOf added in v0.0.8

func LengthOf(value interface{}) (int, error)

LengthOf func

func NOE

func NOE(value interface{}) bool

NOE func

func NewInternalError

func NewInternalError(err error) *internalError

func NewStructValidationError

func NewStructValidationError() *structValidationError

func NewValidationError

func NewValidationError(fieldName, jsonName, msg string) *validationError

NewValidationError func

func ToFloat64

func ToFloat64(v interface{}) (float64, error)

ToFloat64 func

func ToInt64

func ToInt64(v interface{}) (int64, error)

ToInt64 func

func ToString

func ToString(value interface{}) (string, error)

ToString func

func ToUInt64

func ToUInt64(v interface{}) (uint64, error)

ToUInt64 func

Types

type FieldableError

type FieldableError interface {
	error
	FieldName() string
	JSONName() string
}

FieldableError interface

type InternalError

type InternalError interface {
	error
	InternalError() error
}

InternalError interface

type IterableError

type IterableError interface {
	error
	Len() int
	ValueAt(int) error
}

IterableError interface

type Orwell added in v0.0.3

type Orwell struct{}

Orwell struct

func NewValidator added in v0.0.3

func NewValidator() *Orwell

NewValidator func

func (*Orwell) DivBy added in v0.0.3

func (*Orwell) DivBy(div int) *divBy

DivBy func

func (*Orwell) Email added in v0.0.3

func (*Orwell) Email(doLookUp bool) *email

EMail func

func (*Orwell) FieldRules added in v0.0.3

func (o *Orwell) FieldRules(field interface{}, rules ...Rule) *fieldRules

FieldRules func

func (*Orwell) In added in v0.0.3

func (*Orwell) In(args ...interface{}) *in

In func

func (*Orwell) LengthMax added in v0.0.91

func (*Orwell) LengthMax(max int) *lengthMax

LengthMax func

func (*Orwell) LengthMin added in v0.0.9

func (*Orwell) LengthMin(min int) *lengthMin

LengthMin func

func (*Orwell) Match added in v0.0.7

func (*Orwell) Match(arg string) *match

Match func

func (*Orwell) Max added in v0.0.3

func (*Orwell) Max(arg int) *max

Max func

func (*Orwell) Min added in v0.0.3

func (*Orwell) Min(arg int) *min

Max func

func (*Orwell) Required added in v0.0.3

func (*Orwell) Required() *required

Required func

func (*Orwell) Validate added in v0.0.3

func (o *Orwell) Validate(v interface{}, rules ...Rule) error

Validate func

func (*Orwell) ValidateStruct added in v0.0.3

func (o *Orwell) ValidateStruct(structPtr interface{}, fieldRules ...*fieldRules) error

ValidateStruct func

func (*Orwell) XAnd added in v0.0.3

func (*Orwell) XAnd(and interface{}) *xAnd

XAnd func

func (*Orwell) XAndOr added in v0.0.3

func (*Orwell) XAndOr(and interface{}, ors ...interface{}) *xAndOr

XAndOr func

func (*Orwell) XGt added in v0.0.3

func (*Orwell) XGt(xv interface{}, gt int) *xGt

XGt func

func (*Orwell) XGtEql added in v0.0.93

func (*Orwell) XGtEql(gtField interface{}, gtValue int, eqlField interface{}, eqlValue interface{}) *xGtEql

XGtEql func

func (*Orwell) XGtEqlOr added in v0.0.93

func (*Orwell) XGtEqlOr(gtField interface{}, gtValue int, eqlField interface{}, eqlValue interface{}, ors ...interface{}) *xGtEqlOr

XGtEqlOr func

func (*Orwell) XGtOr added in v0.0.4

func (*Orwell) XGtOr(xv interface{}, gt int, ors ...interface{}) *xGtOr

XGtOr func

func (*Orwell) XLt added in v0.0.3

func (*Orwell) XLt(xv interface{}, lt int) *xLt

XLt func

func (*Orwell) XLtOr added in v0.0.4

func (*Orwell) XLtOr(xv interface{}, lt int, ors ...interface{}) *xLtOr

XGtOr func

func (*Orwell) XNor added in v0.0.3

func (*Orwell) XNor(nors ...interface{}) *xNor

XNor func

func (*Orwell) XNot added in v0.0.3

func (*Orwell) XNot(not interface{}) *xNot

XNot func

func (*Orwell) XOr added in v0.0.3

func (*Orwell) XOr(ors ...interface{}) *xOr

XOr func

type Rule

type Rule interface {
	Apply(v interface{}) error
}

Rule interface

Jump to

Keyboard shortcuts

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