fender

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2021 License: MIT Imports: 7 Imported by: 1

README

fender

a piece of rope or a tyre that protects the side of a boat from knocks

Usage

err := fender.NewFields().
	Add("firstName", fender.RequiredString(u.FirstName)).
	Add("lastName", fender.RequiredString(u.LastName)).
	Add("age", fender.MinInt(u.Age, 0), fender.MaxInt(u.Age, 130)).
	Add("email", fender.Email(u.Email, false)).
	Add("street", fender.RequiredString(u.Street)).
	Add("city", fender.RequiredString(u.City)).
	Add("planet", fender.RequiredString(u.Planet)).
	Add("phone", fender.RequiredString(u.Phone)).
	AllError()
if err != nil {
	panic(err)
}

Rules

List of built-in fending rules:

  • email
  • enum
  • max
  • min
  • required
  • regex
  • size

Benchmarks

goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
BenchmarkFender/valid-12           	     1496362	       699 ns/op
BenchmarkFender/invalid-12               1176252 	       975 ns/op
BenchmarkPlayground/valid-12           	   42367	     25967 ns/op
BenchmarkPlayground/invalid-12         	   37567	     28034 ns/op

References & alternatives

How to Contribute

Make a pull request...

License

Distributed under MIT License, please see license file within the code for more details.

Documentation

Index

Constants

View Source
const (
	DefaultRuleDelimiter  = ";"
	DefaultFieldDelimiter = ":"
	DefaultMetaDelimiter  = "|"
	DefaultErrorDelimiter = ","
)
View Source
const (
	RuleEmail Rule = "email"
	// EmailRegexWeak as https://davidcel.is/posts/stop-validating-email-addresses-with-regex/
	EmailRegexWeak = ".+@.+\\..+"
	// EmailRegexRFC5322 as https://stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression/201378#201378
	EmailRegexRFC5322 = "" /* 497-byte string literal not displayed */
)

Variables

View Source
var (
	FieldDelimiter = DefaultFieldDelimiter
	RuleDelimiter  = DefaultRuleDelimiter
	ErrorDelimiter = DefaultErrorDelimiter
	MetaDelimiter  = DefaultMetaDelimiter
)

Functions

This section is empty.

Types

type Enumerator

type Enumerator interface {
	Valid() bool
}

type Fend added in v0.2.1

type Fend func() *RuleError

func Email

func Email(v string, lookup bool) Fend

Email validation using go standard package

func EmailRFC5322

func EmailRFC5322(v string, lookup bool) Fend

EmailRFC5322 validation using RFC5322 regex

func EmailWeak

func EmailWeak(v string) Fend

EmailWeak validation using simple regex

func Enum

func Enum(v Enumerator) Fend

func MaxFloat32

func MaxFloat32(v, expected float32) Fend

func MaxFloat32s

func MaxFloat32s(v []float32, expected int) Fend

func MaxFloat64

func MaxFloat64(v, expected float64) Fend

func MaxFloat64s

func MaxFloat64s(v []float64, expected int) Fend

func MaxInt

func MaxInt(v, expected int) Fend

func MaxInt8

func MaxInt8(v, expected int8) Fend

func MaxInt8s

func MaxInt8s(v []int8, expected int) Fend

func MaxInt32

func MaxInt32(v, expected int32) Fend

func MaxInt32s

func MaxInt32s(v []int32, expected int) Fend

func MaxInt64

func MaxInt64(v, expected int64) Fend

func MaxInt64s

func MaxInt64s(v []int64, expected int) Fend

func MaxInts

func MaxInts(v []int, expected int) Fend

func MaxString

func MaxString(v string, expected int) Fend

func MaxStrings

func MaxStrings(v []string, expected int) Fend

func MaxUInt

func MaxUInt(v, expected uint) Fend

func MaxUInt8

func MaxUInt8(v, expected uint8) Fend

func MaxUInt8s

func MaxUInt8s(v []uint8, expected int) Fend

func MaxUInt32

func MaxUInt32(v, expected uint32) Fend

func MaxUInt32s

func MaxUInt32s(v []uint32, expected int) Fend

func MaxUInt64

func MaxUInt64(v, expected uint64) Fend

func MaxUInt64s

func MaxUInt64s(v []uint64, expected int) Fend

func MaxUInts

func MaxUInts(v []uint, expected int) Fend

func MinFloat32

func MinFloat32(v, expected float32) Fend

func MinFloat32s added in v0.2.1

func MinFloat32s(v []float32, expected int) Fend

func MinFloat64

func MinFloat64(v, expected float64) Fend

func MinFloat64s added in v0.2.1

func MinFloat64s(v []float64, expected int) Fend

func MinInt

func MinInt(v, expected int) Fend

func MinInt8

func MinInt8(v, expected int8) Fend

func MinInt8s added in v0.2.1

func MinInt8s(v []int8, expected int) Fend

func MinInt32

func MinInt32(v, expected int32) Fend

func MinInt32s added in v0.2.1

func MinInt32s(v []int32, expected int) Fend

func MinInt64

func MinInt64(v, expected int64) Fend

func MinInt64s added in v0.2.1

func MinInt64s(v []int64, expected int) Fend

func MinInts added in v0.2.1

func MinInts(v []int, expected int) Fend

func MinString

func MinString(v string, expected int) Fend

func MinStrings added in v0.2.1

func MinStrings(v []string, expected int) Fend

func MinUInt added in v0.2.1

func MinUInt(v, expected uint) Fend

func MinUInt8 added in v0.2.1

func MinUInt8(v, expected uint8) Fend

func MinUInt8s added in v0.2.1

func MinUInt8s(v []uint8, expected int) Fend

func MinUInt32 added in v0.2.1

func MinUInt32(v, expected uint32) Fend

func MinUInt32s added in v0.2.1

func MinUInt32s(v []uint32, expected int) Fend

func MinUInt64 added in v0.2.1

func MinUInt64(v, expected uint64) Fend

func MinUInt64s added in v0.2.1

func MinUInt64s(v []uint64, expected int) Fend

func MinUInts added in v0.2.1

func MinUInts(v []uint, expected int) Fend

func Regex

func Regex(v string, regexp *regexp.Regexp) Fend

Regex validation using go standard package

func RequiredFloat32

func RequiredFloat32(v float32) Fend

func RequiredFloat32s added in v0.2.1

func RequiredFloat32s(v []float32) Fend

func RequiredFloat64

func RequiredFloat64(v float64) Fend

func RequiredFloat64s added in v0.2.1

func RequiredFloat64s(v []float64) Fend

func RequiredInt

func RequiredInt(v int) Fend

func RequiredInt8

func RequiredInt8(v int8) Fend

func RequiredInt8s added in v0.2.1

func RequiredInt8s(v []int8) Fend

func RequiredInt32

func RequiredInt32(v int32) Fend

func RequiredInt32s added in v0.2.1

func RequiredInt32s(v []int32) Fend

func RequiredInt64

func RequiredInt64(v int64) Fend

func RequiredInt64s added in v0.2.1

func RequiredInt64s(v []int64) Fend

func RequiredInts added in v0.2.1

func RequiredInts(v []int) Fend

func RequiredString

func RequiredString(v string) Fend

func RequiredStrings

func RequiredStrings(v []string) Fend

func RequiredUInt added in v0.2.1

func RequiredUInt(v uint) Fend

func RequiredUInt8 added in v0.2.1

func RequiredUInt8(v uint8) Fend

func RequiredUInt8s added in v0.2.1

func RequiredUInt8s(v []uint8) Fend

func RequiredUInt32 added in v0.2.1

func RequiredUInt32(v uint32) Fend

func RequiredUInt32s added in v0.2.1

func RequiredUInt32s(v []uint32) Fend

func RequiredUInt64 added in v0.2.1

func RequiredUInt64(v uint64) Fend

func RequiredUInt64s added in v0.2.1

func RequiredUInt64s(v []uint64) Fend

func RequiredUInts added in v0.2.1

func RequiredUInts(v []uint) Fend

func SizeFloat32

func SizeFloat32(v, expected float32) Fend

func SizeFloat32s added in v0.2.1

func SizeFloat32s(v []float32, expected int) Fend

func SizeFloat64

func SizeFloat64(v, expected float64) Fend

func SizeFloat64s added in v0.2.1

func SizeFloat64s(v []float64, expected int) Fend

func SizeInt

func SizeInt(v, expected int) Fend

func SizeInt8

func SizeInt8(v, expected int8) Fend

func SizeInt8s added in v0.2.1

func SizeInt8s(v []int8, expected int) Fend

func SizeInt32

func SizeInt32(v, expected int32) Fend

func SizeInt32s added in v0.2.1

func SizeInt32s(v []int32, expected int) Fend

func SizeInt64

func SizeInt64(v, expected int64) Fend

func SizeInt64s added in v0.2.1

func SizeInt64s(v []int64, expected int) Fend

func SizeInts added in v0.2.1

func SizeInts(v []int, expected int) Fend

func SizeString

func SizeString(v string, expected int) Fend

func SizeStrings

func SizeStrings(v []string, expected int) Fend

func SizeUInt added in v0.2.1

func SizeUInt(v, expected uint) Fend

func SizeUInt8 added in v0.2.1

func SizeUInt8(v, expected uint8) Fend

func SizeUInt8s added in v0.2.1

func SizeUInt8s(v []uint8, expected int) Fend

func SizeUInt32 added in v0.2.1

func SizeUInt32(v, expected uint32) Fend

func SizeUInt32s added in v0.2.1

func SizeUInt32s(v []uint32, expected int) Fend

func SizeUInt64 added in v0.2.1

func SizeUInt64(v, expected uint64) Fend

func SizeUInt64s added in v0.2.1

func SizeUInt64s(v []uint64, expected int) Fend

func SizeUInts added in v0.2.1

func SizeUInts(v []uint, expected int) Fend

type FieldError added in v0.2.1

type FieldError struct {
	Field string
	Rule  Rule
	Meta  string
}

FieldError type

func NewFieldError added in v0.2.1

func NewFieldError(field string, err *RuleError) *FieldError

NewFieldError constructor

func (*FieldError) Error added in v0.2.1

func (e *FieldError) Error() string

Error interface

type Fields

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

Fields type

func NewFields

func NewFields() *Fields

NewFields constructor

func (Fields) Add

func (f Fields) Add(name string, fends ...Fend) Fields

Add adds an error fields if a validation fails

func (Fields) AllFieldsErrors added in v0.2.1

func (f Fields) AllFieldsErrors() FieldsErrorsMap

AllFieldsErrors return all fields errors

func (Fields) AllFieldsFirstError added in v0.2.1

func (f Fields) AllFieldsFirstError() FieldsErrorMap

AllFieldsFirstError returns only the first error on all fields

func (Fields) FirstFieldError added in v0.2.1

func (f Fields) FirstFieldError() error

FirstFieldError returns only the first error

type FieldsError

type FieldsError struct {
	Map FieldsErrorMap
}

FieldsError type

func NewFieldsError

func NewFieldsError(m FieldsErrorMap) *FieldsError

NewFieldsError constructor

func (*FieldsError) Error

func (e *FieldsError) Error() string

Error interface

type FieldsErrorMap added in v0.2.1

type FieldsErrorMap map[string]*RuleError

FieldsErrorMap type

func (FieldsErrorMap) Error added in v0.2.1

func (m FieldsErrorMap) Error() error

Error returns a std error if not empty

func (FieldsErrorMap) String added in v0.2.1

func (m FieldsErrorMap) String() string

type FieldsErrors

type FieldsErrors struct {
	Map FieldsErrorsMap
}

FieldsErrors type

func NewFieldsErrors

func NewFieldsErrors(m FieldsErrorsMap) *FieldsErrors

NewFieldsErrors constructor

func (*FieldsErrors) Error

func (e *FieldsErrors) Error() string

Error interface

type FieldsErrorsMap added in v0.2.1

type FieldsErrorsMap map[string][]*RuleError

FieldsErrorsMap type

func (FieldsErrorsMap) Error added in v0.2.1

func (m FieldsErrorsMap) Error() error

Error returns a std error if not empty

func (FieldsErrorsMap) String added in v0.2.1

func (m FieldsErrorsMap) String() string

String interface

type RequiredError

type RequiredError struct {
	Type Rule `json:"type"`
}

type Rule

type Rule string
const RuleEnum Rule = "enum"
const RuleMax Rule = "max"
const RuleMin Rule = "min"
const RuleRegex Rule = "regex"
const RuleRequired Rule = "required"
const RuleSize Rule = "size"

type RuleError

type RuleError struct {
	Rule Rule
	Meta string
}

func NewFendEmailError

func NewFendEmailError() *RuleError

NewFendEmailError constructor

func NewFendEnumError

func NewFendEnumError() *RuleError

NewFendEnumError constructor

func NewFendMaxError

func NewFendMaxError(verb rune, v interface{}) *RuleError

NewFendMaxError constructor

func NewFendMinError

func NewFendMinError(verb rune, v interface{}) *RuleError

NewFendMinError constructor

func NewFendRegexError

func NewFendRegexError() *RuleError

NewFendRegexError constructor

func NewFendRequiredError

func NewFendRequiredError() *RuleError

NewFendRequiredError constructor

func NewFendSizeError

func NewFendSizeError(verb rune, v interface{}) *RuleError

NewFendSizeError constructor

func NewRuleError added in v0.2.1

func NewRuleError(fend Rule, meta ...string) *RuleError

NewRuleError constructor

func (*RuleError) Error

func (e *RuleError) Error() string

RuleError interface

Jump to

Keyboard shortcuts

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