rule

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: 12 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrBreak     = errors.New("break")
	ErrUnhandled = errors.New("unhandled type")
)
View Source
var Alnum = Match(NameAlnum.String(), config.RegexAlnum)
View Source
var Alpha = Match(NameAlpha.String(), config.RegexAlpha)
View Source
var Numeric = Match(NameNumeric.String(), config.RegexNumeric)

Functions

func Email

func Email(ctx context.Context, v string) error

Email validation using go standard package

func EmailLookup added in v0.3.2

func EmailLookup(ctx context.Context, v string) error

EmailLookup validation using simple regex

func EmailWeak

func EmailWeak(ctx context.Context, v string) error

EmailWeak validation using simple regex

func Hostname added in v1.0.0

func Hostname(ctx context.Context, v string) error

func MD5 added in v1.0.0

func MD5(ctx context.Context, v string) error

func Meta added in v1.0.0

func Meta(verb rune, value any) string

func Optional added in v1.0.0

func Optional[T any](ctx context.Context, v T) error

func Required added in v1.0.0

func Required[T any](ctx context.Context, v T) error

func URI added in v1.0.0

func URI(ctx context.Context, v string) error

func URL added in v1.0.0

func URL(ctx context.Context, v string) error

func UUID added in v1.0.0

func UUID(ctx context.Context, v string) error

func Valid

func Valid[T Validator](ctx context.Context, v T) error

Types

type AnyRule added in v1.0.0

type AnyRule = Rule[any]

type AnyRules added in v1.0.0

type AnyRules = Rules[any]

type BoolRule

type BoolRule = Rule[bool]

func Bool

func Bool(expectd bool) BoolRule

type BoolRules added in v1.0.0

type BoolRules = Rules[bool]

type DynamicRule added in v1.0.0

type DynamicRule func(ctx context.Context) error

type Error

type Error struct {
	Rule Name
	Meta []string
}

func AsError added in v1.0.0

func AsError(err error) *Error

func NewError

func NewError(rule Name, meta ...string) *Error

NewError constructor

func (*Error) Error

func (e *Error) Error() string

Error interface

type Float added in v1.0.0

type Float = constraints.Float

type Float32Rule

type Float32Rule = Rule[float32]

type Float32Rules added in v1.0.0

type Float32Rules = Rules[float32]

type Float64Rule

type Float64Rule = Rule[float64]

type Float64Rules added in v1.0.0

type Float64Rules = Rules[float64]

type Int32Rule

type Int32Rule = Rule[int32]

type Int32Rules added in v1.0.0

type Int32Rules = Rules[int32]

type Int64Rule

type Int64Rule = Rule[int64]

type Int64Rules added in v1.0.0

type Int64Rules = Rules[int64]

type Int8Rule

type Int8Rule = Rule[int8]

type Int8Rules added in v1.0.0

type Int8Rules = Rules[int8]

type IntRule

type IntRule = Rule[int]

type IntRules added in v1.0.0

type IntRules = Rules[int]

type Integer added in v1.0.0

type Integer = constraints.Integer

type InterfaceRule

type InterfaceRule = Rule[interface{}]

type InterfaceRules added in v1.0.0

type InterfaceRules = Rules[interface{}]

type Name

type Name string
const NameAlnum Name = "alnum"
const NameAlpha Name = "alpha"
const NameBool Name = "bool"
const NameContains Name = "contains"
const NameEmail Name = "email"
const NameEqual Name = "equal"
const NameHostname Name = "hostname"
const NameMD5 Name = "md5"
const NameMatch Name = "match"
const NameMax Name = "max"
const NameMin Name = "min"
const NameNumeric Name = "numeric"
const NamePrefix Name = "prefix"
const NameRange Name = "range"
const NameRequired Name = "required"
const NameSize Name = "size"
const NameSuffix Name = "suffix"
const NameURI Name = "uri"
const NameURL Name = "url"
const NameUUID Name = "uuid"
const NameValid Name = "valid"

func (Name) String

func (n Name) String() string

type Number added in v1.0.0

type Number interface {
	Integer | Float
}

type Ordered added in v1.0.0

type Ordered = constraints.Ordered

type Rule

type Rule[T any] func(ctx context.Context, v T) error

func Dynamic added in v1.0.0

func Dynamic[T any](fn func(ctx context.Context) error) Rule[T]
Example
package main

import (
	"context"
	"fmt"

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

func main() {
	err := fender.All(context.TODO(),
		fend.Field("demo", "foo", rule.Dynamic[string](
			func(ctx context.Context) error {
				return rule.NewError("bar")
			},
		)),
	)
	fmt.Println(err)
}
Output:

demo:bar

func Equal added in v1.0.0

func Equal[T comparable](expected T) Rule[T]

func IsRequired added in v1.0.0

func IsRequired[T any](expected bool) Rule[T]

func NotEqual added in v1.0.0

func NotEqual[T comparable](expected T) Rule[T]

func NumberMax added in v1.0.0

func NumberMax[T Number](expected T) Rule[T]

func NumberMin added in v1.0.0

func NumberMin[T Number](expected T) Rule[T]

func NumberNotRange added in v1.0.0

func NumberNotRange[T Number](min, max T) Rule[T]

func NumberNotSize added in v1.0.0

func NumberNotSize[T Number](expected T) Rule[T]

func NumberRange added in v1.0.0

func NumberRange[T Number](min, max T) Rule[T]

func NumberSize added in v1.0.0

func NumberSize[T Number](expected T) Rule[T]

func Union added in v1.0.0

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

import (
	"context"
	"fmt"

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

func main() {
	{
		err := fender.All(context.TODO(),
			fend.Var("foo", rule.Union(
				rule.StringMin(10),
				rule.Equal("bar"),
			)),
		)
		fmt.Println(err)
	}

	{
		err := fender.All(context.TODO(),
			fend.Var("foo", rule.Union(
				rule.StringMin(10),
				rule.Equal("foo"),
			)),
		)
		fmt.Println(err)
	}
}
Output:

equal=bar
<nil>

type Rules added in v1.0.0

type Rules[T any] []Rule[T]

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

func (r Rules[T]) Append(rules ...Rule[T]) Rules[T]

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

func (r Rules[T]) Prepend(rules ...Rule[T]) Rules[T]

type StringRule

type StringRule = Rule[string]

func Match added in v1.0.0

func Match(alias string, regexp *regexp.Regexp) StringRule

func NoPrefix added in v1.0.0

func NoPrefix(expected string) StringRule

func NoSuffix added in v1.0.0

func NoSuffix(expected string) StringRule

func NotMatch added in v1.0.0

func NotMatch(alias string, regexp *regexp.Regexp) StringRule

func Prefix added in v1.0.0

func Prefix(expected string) StringRule

func StringBool added in v1.0.0

func StringBool(expectd bool) StringRule

func StringContains added in v1.0.0

func StringContains(expected string) StringRule

func StringMax added in v1.0.0

func StringMax(expected int) StringRule

func StringMin added in v1.0.0

func StringMin(expected int) StringRule

func StringNotContains added in v1.0.0

func StringNotContains(expected string) StringRule

func StringNotRange added in v1.0.0

func StringNotRange(min, max int) StringRule

func StringNotSize added in v1.0.0

func StringNotSize(expected int) StringRule

func StringRange added in v1.0.0

func StringRange(min, max int) StringRule

func StringSize added in v1.0.0

func StringSize(expected int) StringRule

func Suffix added in v1.0.0

func Suffix(expected string) StringRule

type StringRules added in v1.0.0

type StringRules = Rules[string]

type UInt32Rule

type UInt32Rule = Rule[uint32]

type UInt32Rules added in v1.0.0

type UInt32Rules = Rules[uint32]

type UInt64Rule

type UInt64Rule = Rule[uint64]

type UInt64Rules added in v1.0.0

type UInt64Rules = Rules[uint64]

type UInt8Rule

type UInt8Rule = Rule[uint8]

type UInt8Rules added in v1.0.0

type UInt8Rules = Rules[uint8]

type UIntRule

type UIntRule = Rule[uint]

type UIntRules added in v1.0.0

type UIntRules = Rules[uint]

type Validator

type Validator interface {
	Valid() bool
}

type ValidatorRule

type ValidatorRule = Rule[Validator]

Jump to

Keyboard shortcuts

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