fend

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2023 License: MIT Imports: 5 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Union added in v1.0.0

func Union[T any](rules ...Rules[T]) rule.Rule[T]
Example
package main

import (
	"context"
	"fmt"

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

func main() {
	email := fend.NewRules(rule.StringMin(5), rule.Email)
	phone := fend.NewRules(rule.StringMin(3), rule.Numeric)
	emailOrPhone := fend.NewRules(fend.Union(email, phone))

	{
		err := fender.All(context.TODO(),
			fend.Var("foo", fend.Union(email, phone)),
		)
		fmt.Println(err)
	}
	{
		err := fender.All(context.TODO(),
			emailOrPhone.Var("foo"),
		)
		fmt.Println(err)
	}

	{
		err := fender.All(context.TODO(),
			fend.Var("foo@bar.com", fend.Union(email, phone)),
		)
		fmt.Println(err)
	}
	{
		err := fender.All(context.TODO(),
			emailOrPhone.Var("foo@bar.com"),
		)
		fmt.Println(err)
	}

	{
		err := fender.All(context.TODO(),
			fend.Var("123456", fend.Union(email, phone)),
		)
		fmt.Println(err)
	}
	{
		err := fender.All(context.TODO(),
			emailOrPhone.Var("123456"),
		)
		fmt.Println(err)
	}

}
Output:

numeric=^[0-9]+$
numeric=^[0-9]+$
<nil>
<nil>
<nil>
<nil>

Types

type Error added in v1.0.0

type Error struct {
	Path     string
	RuleErrs []*rule.Error
}

func AsError added in v1.0.0

func AsError(err error) *Error

func NewError added in v1.0.0

func NewError(path string, ruleErrs ...*rule.Error) *Error

func NewRuleError added in v1.0.0

func NewRuleError(path string, ruleName rule.Name, meta ...string) *Error

func (*Error) Error added in v1.0.0

func (e *Error) Error() string

func (*Error) Errors added in v1.0.0

func (e *Error) Errors() []error

func (*Error) Name added in v1.0.0

func (e *Error) Name() string

type Fend

type Fend func(ctx context.Context, mode Mode) error

func DynamicField added in v1.0.0

func DynamicField(path string, rules ...rule.DynamicRule) Fend

func DynamicVar added in v1.0.0

func DynamicVar(rules ...rule.DynamicRule) Fend

func Field added in v1.0.0

func Field[T any](path string, value T, rules ...rule.Rule[T]) Fend

func Var

func Var[T any](value T, rules ...rule.Rule[T]) Fend

type Fends added in v1.0.0

type Fends []Fend

func (Fends) Append added in v1.0.0

func (f Fends) Append(v ...Fend) Fends

func (Fends) Prepend added in v1.0.0

func (f Fends) Prepend(v ...Fend) Fends

type Mode added in v1.0.0

type Mode int
const (
	ModeAll Mode = iota
	ModeFirst
	ModeAllFirst
)

type Rules added in v1.0.0

type Rules[T any] []rule.Rule[T]

func NewRules added in v1.0.0

func NewRules[T any](rules ...rule.Rule[T]) Rules[T]

func (Rules[T]) DynamicField added in v1.0.0

func (r Rules[T]) DynamicField(path string, rules ...rule.DynamicRule) Fend

func (Rules[T]) DynamicVar added in v1.0.0

func (r Rules[T]) DynamicVar(rules ...rule.DynamicRule) Fend

func (Rules[T]) Field added in v1.0.0

func (r Rules[T]) Field(path string, value T, rules ...rule.Rule[T]) Fend

func (Rules[T]) Var added in v1.0.0

func (r Rules[T]) Var(value T, rules ...rule.Rule[T]) Fend

Jump to

Keyboard shortcuts

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