fender

package module
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2022 License: MIT Imports: 6 Imported by: 1

README

fender

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

Fender provides a unified way to validate data and make the errors passable to the frontend e.g. through gotsrpc.

Usage

Fender provides different ways to validate your data.

struct validation using go-playground validator
package main

import (
	"fmt"

	"github.com/foomo/fender"
)

type Test struct {
	Int     int     `validate:"min=1"`
	Int8    int8    `validate:"min=1"`
	Int32   int32   `validate:"min=1"`
	Int64   int64   `validate:"min=1"`
	UInt    uint    `validate:"min=1"`
	UInt8   uint8   `validate:"min=1"`
	UInt32  uint32  `validate:"min=1"`
	UInt64  uint64  `validate:"min=1"`
	Float32 float32 `validate:"min=1.5"`
	Float64 float64 `validate:"min=1.5"`
	Bool    bool    `validate:"required"`
	String  string  `validate:"required"`
}

func main() {
	u := Test{}
	fendErr, err := fender.Struct(u)
	if err != nil {
		panic("internal error: " + err.Error())
	}
	fmt.Println(fendErr) // bool:required;string:required;uInt:min=1;uInt8:min=1;uInt32:min=1;uInt64:min=1;float32:min=1.5;int:min=1;int8:min=1;int32:min=1;int64:min=1;float64:min=1.5
}
validate by code
package main

import (
	"fmt"

	"github.com/foomo/fender"
	"github.com/foomo/fender/fend"
	"github.com/foomo/fender/rule"
)

type Test struct {
	Int     int
	Int8    int8
	Int32   int32
	Int64   int64
	UInt    uint
	UInt8   uint8
	UInt32  uint32
	UInt64  uint64
	Float32 float32
	Float64 float64
	Bool    bool
	String  string
}

func main() {
	u := Test{}
	fendErr, err := fender.All(
		fender.Field("int", fend.Int(u.Int, rule.MinInt(1))),
		fender.Field("int8", fend.Int8(u.Int8, rule.MinInt8(1))),
		fender.Field("int32", fend.Int32(u.Int32, rule.MinInt32(1))),
		fender.Field("int64", fend.Int64(u.Int64, rule.MinInt64(1))),
		fender.Field("uint", fend.UInt(u.UInt, rule.MinUInt(1))),
		fender.Field("uint8", fend.UInt8(u.UInt8, rule.MinUInt8(1))),
		fender.Field("uint32", fend.UInt32(u.UInt32, rule.MinUInt32(1))),
		fender.Field("uint64", fend.UInt64(u.UInt64, rule.MinUInt64(1))),
		fender.Field("float32", fend.Float32(u.Float32, rule.MinFloat32(1.5))),
		fender.Field("float64", fend.Float64(u.Float64, rule.MinFloat64(1.5))),
		fender.Field("bool", fend.Bool(u.Bool, rule.Bool(true))),
		fender.Field("string", fend.String(u.String, rule.RequiredString)),
	)
	if err != nil {
		panic("internal error: " + err.Error())
	}
	fmt.Println(fendErr) // uint:min=1;uint64:min=1;float32:min=1.5;int:min=1;int8:min=1;uint8:min=1;uint32:min=1;float64:min=1.5;bool:bool=true;string:required;int32:min=1;int64:min=1
}
validate by code and stop on first error
package main

import (
	"fmt"

	"github.com/foomo/fender"
	"github.com/foomo/fender/fend"
	"github.com/foomo/fender/rule"
)

type Test struct {
	Int     int
	Int8    int8
	Int32   int32
	Int64   int64
	UInt    uint
	UInt8   uint8
	UInt32  uint32
	UInt64  uint64
	Float32 float32
	Float64 float64
	Bool    bool
	String  string
}

func main() {
	u := Test{}
	fendErr, err := fender.First(
		fender.Field("int", fend.Int(u.Int, rule.MinInt(1))),
		fender.Field("int8", fend.Int8(u.Int8, rule.MinInt8(1))),
		fender.Field("int32", fend.Int32(u.Int32, rule.MinInt32(1))),
		fender.Field("int64", fend.Int64(u.Int64, rule.MinInt64(1))),
		fender.Field("uint", fend.UInt(u.UInt, rule.MinUInt(1))),
		fender.Field("uint8", fend.UInt8(u.UInt8, rule.MinUInt8(1))),
		fender.Field("uint32", fend.UInt32(u.UInt32, rule.MinUInt32(1))),
		fender.Field("uint64", fend.UInt64(u.UInt64, rule.MinUInt64(1))),
		fender.Field("float32", fend.Float32(u.Float32, rule.MinFloat32(1.5))),
		fender.Field("float64", fend.Float64(u.Float64, rule.MinFloat64(1.5))),
		fender.Field("bool", fend.Bool(u.Bool, rule.Bool(true))),
		fender.Field("string", fend.String(u.String, rule.RequiredString)),
	)
	if err != nil {
		panic("internal error: " + err.Error())
	}
	fmt.Println(fendErr) // int:min=1
}

References & alternatives

How to Contribute

Make a pull request...

License

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

Notes

We'll take another refactoring round as soon as go supports generics

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Err = &Error{}

Functions

func NewWrappedError added in v0.4.2

func NewWrappedError(err error, cause *Error) error

func WrapError added in v0.4.3

func WrapError(err error, cause *Error) error

Types

type Error added in v0.3.0

type Error struct {
	Fields FieldErrors
}

Error type

func All added in v0.3.0

func All(fields ...FendField) (*Error, error)

func First added in v0.3.0

func First(fields ...FendField) (*Error, error)

func NewError added in v0.3.0

func NewError(fields FieldErrors) *Error

NewError constructor

func Struct added in v0.3.6

func Struct(s interface{}) (*Error, error)

func UnwrapError added in v0.4.3

func UnwrapError(err error) *Error

func (*Error) Error added in v0.3.0

func (e *Error) Error() string

Error interface

func (*Error) Errors added in v0.3.0

func (e *Error) Errors() FieldErrors

func (*Error) First added in v0.3.0

func (e *Error) First() error

func (*Error) Is added in v0.3.0

func (e *Error) Is(err error) bool

Is interface

type FendField added in v0.3.0

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

func Field added in v0.3.0

func Field(name string, fends ...fend.Fend) FendField

type FendFields added in v0.3.2

type FendFields []FendField

func (FendFields) Add added in v0.3.2

func (f FendFields) Add(fields ...FendField) FendFields

type FieldErrors added in v0.4.0

type FieldErrors map[string]*rule.Error

FieldErrors type

func (FieldErrors) String added in v0.4.0

func (m FieldErrors) String() string

String returns the string representation

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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