assert

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: MIT Imports: 8 Imported by: 0

README

Assertions

TL;DR

Simple, type‑specific fluent assertions for Go (initially inspired by PHP webmozart/assert).

Requires Go (1.18) or later.

assert.Str().Word().LenMax(5).Check(value)

Reasons

If you’ve ever worked with PHP, you probably used the webmozart/assert package. It made it easy to validate method arguments using supplementary criteria in addition to type checking -- for example, verifying that integers were natural or strings were non‑empty. Moreover, combinations of such checks could be used to build more complex validations instead of using heavy validation frameworks that might impose compromises on project design.

In Go, there are already well‑known validation packages, but most of them are designed for broad or complex cases, such as struct validation by tags, validation with network requests, etc. -- this also can be overkill.

Some Go-packages also have not enough friendly interface and weak typing. For example, it can be possible to add length checks to an integer value validation flow, etc. Such things confuse, make interface more complex and increase chances to make an accidental mistake.

So something else was needed...

Something simpler... Something more type-specific... Something more friendly... Something like this package! 😊

Description

This package un autre package de validation, mais avec les cartes à jouer et les femmes fatales provides a simple type-specific fluent interface for building validations like

assert.Str().Word().LenMax(5).Check(value)

The package provides assertions for specific types as well as more abstract assertions for broader type support.

Each assertion supports only specific methods relevant to its value type -- this prevents accidental mistakes. For example, it is impossible to validate integer value via some length-based rule, because Num assertion does not have methods to add such validation to the chain.

Each assertion supports a Custom() check for cases not covered by built-ins.

Specific assertions
General assertions
  • Any -- for any type
  • Cmp -- for any comparable type
  • SliceAny -- for slice-based types with any type of elements
  • SliceCmp -- for slice-based types with comparable type of elements
Getting results

Assertions support a few types of results:

Custom messages

Each rule and result method can optionally take custom message in the customErrMsg argument:

  • If a rule method is customized, the custom message replaces the default message when rule fails.
  • If a result method is customized, the custom message replaces any message when the chain fails.
Shortcuts

The package also provides shortcuts for the most popular assertions in package-level functions with ...Check, ...Must and ...MustGet result variants:

  • NotZero...
  • NotNilDeep...
  • True...
  • False...
  • StrNotEmpty...
  • CmpNotEq...

Examples

Assertion

This is the initial use-case of this package -- ensuring without ugly boilerplate code that method works with valid values of the argument types, not with nil-pointers, etc.

Arguments assertion
package example

import (
	"github.com/selyukovn/go-wm-assert"
	"time"
)

type Account struct{ /* ... */ }
type EventCollection struct{ /* ... */ }

func (a *Account) Deactivate(deactivatedAt time.Time, evs *EventCollection) error {
	assert.Time().NotZero().LessEq(time.Now()).Must(deactivatedAt)
	assert.Cmp[*EventCollection]().NotEq(nil).Must(evs)

	// or with popular shortcut for `evs`
	assert.NotNilDeepMust(evs)

	// ...

	return nil
}
Config assertion
package config

import (
	"github.com/selyukovn/go-wm-assert"
	"os"
)

func LoadEnv() *Env {
	env = &Env{}

	// ...

	env.AppName = assert.Str().Word().MustGet(os.Getenv("APP_NAME"))
	env.IsDebug = assert.Str().In([]string{"0", "1"}).MustGet(os.Getenv("IS_DEBUG"))

	// ...

	return env
}
Validation

This package is not about a form validation, but customizing messages makes possible to use it as a "brick" to build the things you need in a simple and clean way.

Simple validation
package example

import (
	"fmt"
	"github.com/selyukovn/go-wm-assert"
)

type Name struct{ value string }

func NameFromString(value string) (Name, error) {
	// custom error message that overrides any other in the chain */
	err := assert.Str().Word().Check(value, fmt.Sprintf("Name %q is incorrect!", value))

	if err != nil {
		return Name{}, err
	}

	return Name{value: value}, nil
}
Form validation
package example

import "github.com/selyukovn/go-wm-assert"

type SignUpForm struct {
	email     string
	name      string // let it be optional
	age       uint
	agreement bool

	errors map[string][]error
}

// ...

func (f *SignUpForm) Validate() bool {
	f.errors = map[string][]error{
		"email":     {},
		"name":      {},
		"age":       {},
		"agreement": {},
	}

	f.errors["email"] = assert.Str().
		NotEmpty("Email is required!").
		Regexp(
			emailRegexpCompiled,

			// custom error message only for this rule
			// instead of technical "value ... regexp ..."
			"Email is incorrect!",
		).
		Custom(func(v string) error {
			// e.g. check that it is not registered previously
			return nil
		}).
		CheckAll(f.email)

	// optional field, remember?
	if f.name != "" {
		f.errors["name"] = assert.Str().
			Word("Only letters and '-' allowed!").
			RunesMin(2, "Too short, isn't it?").
			RunesMax(255, "Too long, isn't it?").
			NotIn(
				[]string{ /* e.g. some set of bad words or so */ },
				"Is that your real name, friend?",
			).
			CheckAll(f.name)
	}

	f.errors["age"] = assert.Num[uint]().
		GreaterEq(18, "Things are serious -- come back later!").
		Less(65, "Take a rest, friend!").
		CheckAll(f.age)

	f.errors["agreement"] = assert.Bool().
		True("This flag is required!").
		CheckAll(f.agreement)

	return len(f.errors["email"]) == 0 &&
		len(f.errors["name"]) == 0 &&
		len(f.errors["age"]) == 0 &&
		len(f.errors["agreement"]) == 0
}

func (f *SignUpForm) NameErrors() []error {
	return f.errors["name"]
}

// ...

Package Structure

  • b_*.go — basic components
  • s_*.go — specific assertions (Str, Num, etc.)
  • shortcuts.go — shortcuts for the most popular assertions
  • readme_test.go — examples from the Readme (ensures correctness)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var StringRegexpNumeric = regexp.MustCompile("^-?\\d+(\\.\\d+)?$")

StringRegexpNumeric

Public variable to allow re-define globally.

View Source
var StringRegexpWord = regexp.MustCompile("^[A-Za-z](-?[A-Za-z]+)*$")

StringRegexpWord

Public variable to allow re-define globally.

View Source
var StringRegexpWords = regexp.MustCompile("^([A-Za-z](-?[A-Za-z]+)*)([ ]?([A-Za-z](-?[A-Za-z]+)*))+$")

StringRegexpWords

Public variable to allow re-define globally.

Functions

func CmpNotEqCheck added in v0.4.0

func CmpNotEqCheck[T comparable](v T, ne T, customErrMsg ...string) error

CmpNotEqCheck -- see AComparable[T].NotEq()

func CmpNotEqMust added in v0.4.0

func CmpNotEqMust[T comparable](v T, ne T, customErrMsg ...string)

CmpNotEqMust -- see AComparable[T].NotEq()

func CmpNotEqMustGet added in v0.4.0

func CmpNotEqMustGet[T comparable](v T, ne T, customErrMsg ...string) T

CmpNotEqMustGet -- see AComparable[T].NotEq()

func FalseCheck added in v0.3.0

func FalseCheck(v bool, customErrMsg ...string) error

FalseCheck -- see ABool.False()

func FalseMust added in v0.3.0

func FalseMust(v bool, customErrMsg ...string)

FalseMust -- see ABool.False()

func NotNilDeepCheck added in v0.3.0

func NotNilDeepCheck(v any, customErrMsg ...string) error

NotNilDeepCheck -- see AAny.NotNilDeep()

func NotNilDeepMust added in v0.3.0

func NotNilDeepMust(v any, customErrMsg ...string)

NotNilDeepMust -- see AAny.NotNilDeep()

func NotNilDeepMustGet added in v0.3.0

func NotNilDeepMustGet[T any](v T, customErrMsg ...string) T

NotNilDeepMustGet -- see AAny.NotNilDeep()

func NotZeroCheck added in v0.3.0

func NotZeroCheck(v any, customErrMsg ...string) error

NotZeroCheck -- see AAny.NotZero()

func NotZeroMust added in v0.3.0

func NotZeroMust(v any, customErrMsg ...string)

NotZeroMust -- see AAny.NotZero()

func NotZeroMustGet added in v0.3.0

func NotZeroMustGet[T any](v T, customErrMsg ...string) T

NotZeroMustGet -- see AAny.NotZero()

func StrNotEmptyCheck added in v0.4.0

func StrNotEmptyCheck(v string, customErrMsg ...string) error

StrNotEmptyCheck -- see AString.NotEmpty()

func StrNotEmptyMust added in v0.4.0

func StrNotEmptyMust(v string, customErrMsg ...string)

StrNotEmptyMust -- see AString.NotEmpty()

func StrNotEmptyMustGet added in v0.4.0

func StrNotEmptyMustGet(v string, customErrMsg ...string) string

StrNotEmptyMustGet -- see AString.NotEmpty()

func TrueCheck added in v0.3.0

func TrueCheck(v bool, customErrMsg ...string) error

TrueCheck -- see ABool.True()

func TrueMust added in v0.3.0

func TrueMust(v bool, customErrMsg ...string)

TrueMust -- see ABool.True()

Types

type AAny added in v0.3.0

type AAny[T any] struct {
	// contains filtered or unexported fields
}

func Any added in v0.3.0

func Any[T any]() *AAny[T]

func (AAny) Check added in v0.3.0

func (a AAny) Check(v T, customErrMsg ...string) error

Check

Runs registered validation checks one by one against the given value and returns an error from a first failed check. Returns nil, if all checks pass.

func (AAny) CheckAll added in v0.3.0

func (a AAny) CheckAll(v T) []error

CheckAll

Runs registered validation checks one by one against the given value and returns errors from all failed checks. Returns empty slice, if all checks pass.

func (AAny) Custom added in v0.3.0

func (m AAny) Custom(check func(v T) error) A

func (AAny) Must added in v0.3.0

func (a AAny) Must(v T, customErrMsg ...string)

Must

Calls Check and panics with the error if validation fails.

func (AAny) MustAll added in v0.3.0

func (a AAny) MustAll(v T)

MustAll

Calls CheckAll and panics with the error slice if validation fails.

func (AAny) MustAllGet added in v0.3.0

func (a AAny) MustAllGet(v T) T

MustAllGet

Works same as MustAll, but returns the value, if no panic occurred.

func (AAny) MustGet added in v0.3.0

func (a AAny) MustGet(v T, customErrMsg ...string) T

MustGet

Works same as Must, but returns the value, if no panic occurred.

func (*AAny[T]) NotNilDeep added in v0.3.0

func (a *AAny[T]) NotNilDeep(customErrMsg ...string) *AAny[T]

NotNilDeep

ATTENTION! THIS IS NOT THE SAME AS `v != nil`.

For non-nil types, the check succeeds because values of these types are never `nil`.

For nil types except pointers and interfaces, the check is analogous to Go's default behavior `v != nil`.

For Nilable-types check fails, when Nilable.IsNil() returns `true`.

For pointers and interfaces, the check runs recursively -- for example, for `interface|(*int)(nil)` it fails.

func (*AAny[T]) NotZero added in v0.3.0

func (a *AAny[T]) NotZero(customErrMsg ...string) *AAny[T]

NotZero

Fails check, if value is the zero value for its type.

For Zeroable-types check fails, when Zeroable.IsZero() returns `true`.

ATTENTION! THIS IS NOT THE SAME AS `v != nil` FOR INTERFACES: e.g. `interface|(*int)(nil)` fails the check.

type ABool

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

func Bool

func Bool() *ABool

func (ABool) Check

func (a ABool) Check(v T, customErrMsg ...string) error

Check

Runs registered validation checks one by one against the given value and returns an error from a first failed check. Returns nil, if all checks pass.

func (ABool) CheckAll

func (a ABool) CheckAll(v T) []error

CheckAll

Runs registered validation checks one by one against the given value and returns errors from all failed checks. Returns empty slice, if all checks pass.

func (ABool) Custom

func (m ABool) Custom(check func(v T) error) A

func (ABool) Eq

func (m ABool) Eq(eq T, customErrMsg ...string) A

Eq

Value expects to be equal to "eq".

func (*ABool) False

func (a *ABool) False(customErrMsg ...string) *ABool

False -- alias to Eq(false)

func (ABool) In

func (m ABool) In(slice []T, customErrMsg ...string) A

In

Value expects to be equal to any of the provided elements.

Fails check, if no elements provided.

func (ABool) Must

func (a ABool) Must(v T, customErrMsg ...string)

Must

Calls Check and panics with the error if validation fails.

func (ABool) MustAll

func (a ABool) MustAll(v T)

MustAll

Calls CheckAll and panics with the error slice if validation fails.

func (ABool) MustAllGet added in v0.3.0

func (a ABool) MustAllGet(v T) T

MustAllGet

Works same as MustAll, but returns the value, if no panic occurred.

func (ABool) MustGet added in v0.3.0

func (a ABool) MustGet(v T, customErrMsg ...string) T

MustGet

Works same as Must, but returns the value, if no panic occurred.

func (ABool) NotEq

func (m ABool) NotEq(notEq T, customErrMsg ...string) A

NotEq

Value expects to be not equal to "notEq".

func (ABool) NotIn

func (m ABool) NotIn(slice []T, customErrMsg ...string) A

NotIn

Value expects to be not equal to each of the provided elements.

Passes check, if no elements provided.

func (*ABool) True

func (a *ABool) True(customErrMsg ...string) *ABool

True -- alias to Eq(true)

type AComparable

type AComparable[T comparable] struct {
	// contains filtered or unexported fields
}

func Cmp added in v0.3.0

func Cmp[T comparable]() *AComparable[T]

func Comparable deprecated

func Comparable[T comparable]() *AComparable[T]

Comparable

Deprecated: use Cmp instead.

func (AComparable) Check

func (a AComparable) Check(v T, customErrMsg ...string) error

Check

Runs registered validation checks one by one against the given value and returns an error from a first failed check. Returns nil, if all checks pass.

func (AComparable) CheckAll

func (a AComparable) CheckAll(v T) []error

CheckAll

Runs registered validation checks one by one against the given value and returns errors from all failed checks. Returns empty slice, if all checks pass.

func (AComparable) Custom

func (m AComparable) Custom(check func(v T) error) A

func (AComparable) Eq

func (m AComparable) Eq(eq T, customErrMsg ...string) A

Eq

Value expects to be equal to "eq".

func (AComparable) In

func (m AComparable) In(slice []T, customErrMsg ...string) A

In

Value expects to be equal to any of the provided elements.

Fails check, if no elements provided.

func (AComparable) Must

func (a AComparable) Must(v T, customErrMsg ...string)

Must

Calls Check and panics with the error if validation fails.

func (AComparable) MustAll

func (a AComparable) MustAll(v T)

MustAll

Calls CheckAll and panics with the error slice if validation fails.

func (AComparable) MustAllGet added in v0.3.0

func (a AComparable) MustAllGet(v T) T

MustAllGet

Works same as MustAll, but returns the value, if no panic occurred.

func (AComparable) MustGet added in v0.3.0

func (a AComparable) MustGet(v T, customErrMsg ...string) T

MustGet

Works same as Must, but returns the value, if no panic occurred.

func (AComparable) NotEq

func (m AComparable) NotEq(notEq T, customErrMsg ...string) A

NotEq

Value expects to be not equal to "notEq".

func (AComparable) NotIn

func (m AComparable) NotIn(slice []T, customErrMsg ...string) A

NotIn

Value expects to be not equal to each of the provided elements.

Passes check, if no elements provided.

type ANumeric

type ANumeric[T NumericTypes] struct {
	// contains filtered or unexported fields
}

func Num added in v0.3.0

func Num[T NumericTypes]() *ANumeric[T]

func Numeric deprecated

func Numeric[T NumericTypes]() *ANumeric[T]

Numeric

Deprecated: use Num() instead.

func (ANumeric) Check

func (a ANumeric) Check(v T, customErrMsg ...string) error

Check

Runs registered validation checks one by one against the given value and returns an error from a first failed check. Returns nil, if all checks pass.

func (ANumeric) CheckAll

func (a ANumeric) CheckAll(v T) []error

CheckAll

Runs registered validation checks one by one against the given value and returns errors from all failed checks. Returns empty slice, if all checks pass.

func (ANumeric) Custom

func (m ANumeric) Custom(check func(v T) error) A

func (ANumeric) Eq

func (m ANumeric) Eq(eq T, customErrMsg ...string) A

Eq

Value expects to be equal to "eq".

func (ANumeric) Greater

func (m ANumeric) Greater(than T, customErrMsg ...string) A

Greater

Value expects to be greater than "than".

func (ANumeric) GreaterAny

func (m ANumeric) GreaterAny(elems []T, customErrMsg ...string) A

GreaterAny

Value expects to be greater than any of provided elements.

Passes check, if no elements provided.

func (ANumeric) GreaterEach

func (m ANumeric) GreaterEach(elems []T, customErrMsg ...string) A

GreaterEach

Value expects to be greater than each of provided elements.

Passes check, if no elements provided.

func (ANumeric) GreaterEq

func (m ANumeric) GreaterEq(than T, customErrMsg ...string) A

GreaterEq

Value expects to be greater or equal to "than".

func (ANumeric) GreaterEqAny

func (m ANumeric) GreaterEqAny(elems []T, customErrMsg ...string) A

GreaterEqAny

Value expects to be greater or equal to any of provided elements.

Passes check, if no elements provided.

func (ANumeric) GreaterEqEach

func (m ANumeric) GreaterEqEach(elems []T, customErrMsg ...string) A

GreaterEqEach

Value expects to be greater or equal to each of provided elements.

Passes check, if no elements provided.

func (ANumeric) In

func (m ANumeric) In(slice []T, customErrMsg ...string) A

In

Value expects to be equal to any of the provided elements.

Fails check, if no elements provided.

func (ANumeric) InRange

func (m ANumeric) InRange(min T, max T, customErrMsg ...string) A

InRange

Value expects to be in range [min, max].

Fails check, if min > max -- it works like empty range.

func (ANumeric) Less

func (m ANumeric) Less(than T, customErrMsg ...string) A

Less

Value expects to be less than "than".

func (ANumeric) LessAny

func (m ANumeric) LessAny(elems []T, customErrMsg ...string) A

LessAny

Value expects to be less than any of provided elements.

Passes check, if no elements provided.

func (ANumeric) LessEach

func (m ANumeric) LessEach(elems []T, customErrMsg ...string) A

LessEach

Value expects to be less than each of provided elements.

Passes check, if no elements provided.

func (ANumeric) LessEq

func (m ANumeric) LessEq(than T, customErrMsg ...string) A

LessEq

Value expects to be less or equal to "than".

func (ANumeric) LessEqAny

func (m ANumeric) LessEqAny(elems []T, customErrMsg ...string) A

LessEqAny

Value expects to be less or equal to any of provided elements.

Passes check, if no elements provided.

func (ANumeric) LessEqEach

func (m ANumeric) LessEqEach(elems []T, customErrMsg ...string) A

LessEqEach

Value expects to be less or equal to each of provided elements.

Passes check, if no elements provided.

func (ANumeric) Must

func (a ANumeric) Must(v T, customErrMsg ...string)

Must

Calls Check and panics with the error if validation fails.

func (ANumeric) MustAll

func (a ANumeric) MustAll(v T)

MustAll

Calls CheckAll and panics with the error slice if validation fails.

func (ANumeric) MustAllGet added in v0.3.0

func (a ANumeric) MustAllGet(v T) T

MustAllGet

Works same as MustAll, but returns the value, if no panic occurred.

func (ANumeric) MustGet added in v0.3.0

func (a ANumeric) MustGet(v T, customErrMsg ...string) T

MustGet

Works same as Must, but returns the value, if no panic occurred.

func (*ANumeric[T]) Negative

func (a *ANumeric[T]) Negative(customErrMsg ...string) *ANumeric[T]

Negative -- alias to Less(0)

func (ANumeric) NotEq

func (m ANumeric) NotEq(notEq T, customErrMsg ...string) A

NotEq

Value expects to be not equal to "notEq".

func (ANumeric) NotIn

func (m ANumeric) NotIn(slice []T, customErrMsg ...string) A

NotIn

Value expects to be not equal to each of the provided elements.

Passes check, if no elements provided.

func (ANumeric) NotInRange

func (m ANumeric) NotInRange(min T, max T, customErrMsg ...string) A

NotInRange

Value expects to be not in range [min, max] -- i.e. to be in ranges [PossibleMin, min) or (max, PossibleMax]

Passes check, if min > max -- it works like empty range.

func (*ANumeric[T]) NotZero

func (a *ANumeric[T]) NotZero(customErrMsg ...string) *ANumeric[T]

NotZero -- alias to NotEq(0)

func (*ANumeric[T]) Positive

func (a *ANumeric[T]) Positive(customErrMsg ...string) *ANumeric[T]

Positive -- alias to Greater(0)

func (*ANumeric[T]) Zero

func (a *ANumeric[T]) Zero(customErrMsg ...string) *ANumeric[T]

Zero -- alias to Eq(0)

type ASliceAny added in v0.2.0

type ASliceAny[S sliceType[E], E any] struct {
	// contains filtered or unexported fields
}

func SliceAny added in v0.2.0

func SliceAny[S sliceType[E], E any]() *ASliceAny[S, E]

func (ASliceAny) Check added in v0.2.0

func (a ASliceAny) Check(v T, customErrMsg ...string) error

Check

Runs registered validation checks one by one against the given value and returns an error from a first failed check. Returns nil, if all checks pass.

func (ASliceAny) CheckAll added in v0.2.0

func (a ASliceAny) CheckAll(v T) []error

CheckAll

Runs registered validation checks one by one against the given value and returns errors from all failed checks. Returns empty slice, if all checks pass.

func (ASliceAny) Custom added in v0.2.0

func (m ASliceAny) Custom(check func(v T) error) A

func (ASliceAny) CustomElementAny added in v0.2.0

func (m ASliceAny) CustomElementAny(
	conditionName string,
	conditionFn func(e E) bool,
	customErrMsg ...string,
) A

CustomElementAny

Expects the slice with any element matched to custom condition.

Passes check, if the slice is empty. If it confuses, just add the NotEmpty() rule to the chain.

func (ASliceAny) CustomElementEach added in v0.2.0

func (m ASliceAny) CustomElementEach(
	conditionName string,
	conditionFn func(e E) bool,
	customErrMsg ...string,
) A

CustomElementEach

Expects the slice with each element matched to custom condition.

Passes check, if the slice is empty. If it confuses, just add the NotEmpty() rule to the chain.

func (ASliceAny) CustomElementNone added in v0.2.0

func (m ASliceAny) CustomElementNone(
	conditionName string,
	conditionFn func(e E) bool,
	customErrMsg ...string,
) A

CustomElementNone

Expects the slice with none element matched to custom condition.

Passes check, if the slice is empty. If it confuses, just add the NotEmpty() rule to the chain.

func (ASliceAny) Empty added in v0.2.0

func (m ASliceAny) Empty(customErrMsg ...string) A

func (ASliceAny) Must added in v0.2.0

func (a ASliceAny) Must(v T, customErrMsg ...string)

Must

Calls Check and panics with the error if validation fails.

func (ASliceAny) MustAll added in v0.2.0

func (a ASliceAny) MustAll(v T)

MustAll

Calls CheckAll and panics with the error slice if validation fails.

func (ASliceAny) MustAllGet added in v0.3.0

func (a ASliceAny) MustAllGet(v T) T

MustAllGet

Works same as MustAll, but returns the value, if no panic occurred.

func (ASliceAny) MustGet added in v0.3.0

func (a ASliceAny) MustGet(v T, customErrMsg ...string) T

MustGet

Works same as Must, but returns the value, if no panic occurred.

func (ASliceAny) NotEmpty added in v0.2.0

func (m ASliceAny) NotEmpty(customErrMsg ...string) A

type ASliceCmp added in v0.2.0

type ASliceCmp[S sliceType[E], E comparable] struct {
	// contains filtered or unexported fields
}

func SliceCmp added in v0.2.0

func SliceCmp[S sliceType[E], E comparable]() *ASliceCmp[S, E]

func (ASliceCmp) Check added in v0.2.0

func (a ASliceCmp) Check(v T, customErrMsg ...string) error

Check

Runs registered validation checks one by one against the given value and returns an error from a first failed check. Returns nil, if all checks pass.

func (ASliceCmp) CheckAll added in v0.2.0

func (a ASliceCmp) CheckAll(v T) []error

CheckAll

Runs registered validation checks one by one against the given value and returns errors from all failed checks. Returns empty slice, if all checks pass.

func (ASliceCmp) Contains added in v0.2.0

func (m ASliceCmp) Contains(e E, customErrMsg ...string) A

func (ASliceCmp) ContainsAny added in v0.2.0

func (m ASliceCmp) ContainsAny(s S, customErrMsg ...string) A

func (ASliceCmp) ContainsEach added in v0.2.0

func (m ASliceCmp) ContainsEach(s S, customErrMsg ...string) A

func (ASliceCmp) ContainsNone added in v0.2.0

func (m ASliceCmp) ContainsNone(s S, customErrMsg ...string) A

func (ASliceCmp) Custom added in v0.2.0

func (m ASliceCmp) Custom(check func(v T) error) A

func (ASliceCmp) Must added in v0.2.0

func (a ASliceCmp) Must(v T, customErrMsg ...string)

Must

Calls Check and panics with the error if validation fails.

func (ASliceCmp) MustAll added in v0.2.0

func (a ASliceCmp) MustAll(v T)

MustAll

Calls CheckAll and panics with the error slice if validation fails.

func (ASliceCmp) MustAllGet added in v0.3.0

func (a ASliceCmp) MustAllGet(v T) T

MustAllGet

Works same as MustAll, but returns the value, if no panic occurred.

func (ASliceCmp) MustGet added in v0.3.0

func (a ASliceCmp) MustGet(v T, customErrMsg ...string) T

MustGet

Works same as Must, but returns the value, if no panic occurred.

func (ASliceCmp) NotContains added in v0.2.0

func (m ASliceCmp) NotContains(e E, customErrMsg ...string) A

func (ASliceCmp) Uniques added in v0.2.0

func (m ASliceCmp) Uniques(customErrMsg ...string) A

Uniques

Expects the slice to have no duplicated elements -- i.e. expects that all elements of the slice are unique.

Passes check, if the slice is empty. If it confuses, just add the NotEmpty() rule to the chain.

func (ASliceCmp) UniquesLenEq added in v0.2.0

func (m ASliceCmp) UniquesLenEq(eq int, customErrMsg ...string) A

UniquesLenEq

Length of unique elements sub-slice expects to be equal to "eq".

Logically incorrect params (e.g. negative values, etc.) are processed as usual.

func (ASliceCmp) UniquesLenInRange added in v0.2.0

func (m ASliceCmp) UniquesLenInRange(min, max int, customErrMsg ...string) A

UniquesLenInRange

Length of unique elements sub-slice expects to be in range [min, max].

Fails check, if min > max -- it works like empty range. Logically incorrect params (e.g. negative values, etc.) are processed as usual.

func (ASliceCmp) UniquesLenMax added in v0.2.0

func (m ASliceCmp) UniquesLenMax(max int, customErrMsg ...string) A

UniquesLenMax

Length of unique elements sub-slice expects to be less or equal to "max".

Logically incorrect params (e.g. negative values, etc.) are processed as usual.

func (ASliceCmp) UniquesLenMin added in v0.2.0

func (m ASliceCmp) UniquesLenMin(min int, customErrMsg ...string) A

UniquesLenMin

Length of unique elements sub-slice expects to be greater or equal to "min".

Logically incorrect params (e.g. negative values, etc.) are processed as usual.

func (ASliceCmp) UniquesLenNotEq added in v0.2.0

func (m ASliceCmp) UniquesLenNotEq(notEq int, customErrMsg ...string) A

UniquesLenNotEq

Length of unique elements sub-slice expects to be not equal to "notEq".

Logically incorrect params (e.g. negative values, etc.) are processed as usual.

func (ASliceCmp) UniquesLenNotInRange added in v0.2.0

func (m ASliceCmp) UniquesLenNotInRange(min, max int, customErrMsg ...string) A

UniquesLenNotInRange

Length of unique elements sub-slice expects to be not in range [min, max] -- i.e. to be in ranges [0, min) or (max, MaxInt].

Passes check, if min > max -- it works like empty range. Logically incorrect params (e.g. negative values, etc.) are processed as usual.

type AString

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

func Str added in v0.3.0

func Str() *AString

func String deprecated

func String() *AString

String

Deprecated: use Str() instead.

func (AString) Check

func (a AString) Check(v T, customErrMsg ...string) error

Check

Runs registered validation checks one by one against the given value and returns an error from a first failed check. Returns nil, if all checks pass.

func (AString) CheckAll

func (a AString) CheckAll(v T) []error

CheckAll

Runs registered validation checks one by one against the given value and returns errors from all failed checks. Returns empty slice, if all checks pass.

func (*AString) ContainsStr added in v0.2.0

func (a *AString) ContainsStr(s string, customErrMsg ...string) *AString

ContainsStr

Value expects to contain provided substring.

Passes check, if empty substring provided.

See strings.Contains.

func (*AString) ContainsStrAny added in v0.2.0

func (a *AString) ContainsStrAny(ss []string, customErrMsg ...string) *AString

ContainsStrAny

Value expects to contain any of provided substrings.

Passes check, if provided set of substrings is empty.

See strings.Contains.

func (*AString) ContainsStrEach added in v0.2.0

func (a *AString) ContainsStrEach(ss []string, customErrMsg ...string) *AString

ContainsStrEach

Value expects to contain each of provided substrings.

Passes check, if provided set of substrings is empty.

See strings.Contains.

func (*AString) ContainsStrNone added in v0.2.0

func (a *AString) ContainsStrNone(ss []string, customErrMsg ...string) *AString

ContainsStrNone

Value expects to contain none of provided substrings.

Passes check, if provided set of substrings is empty.

See strings.Contains.

func (AString) Custom

func (m AString) Custom(check func(v T) error) A

func (*AString) Empty

func (a *AString) Empty(customErrMsg ...string) *AString

Empty -- alias to Eq("")

func (AString) Eq

func (m AString) Eq(eq T, customErrMsg ...string) A

Eq

Value expects to be equal to "eq".

func (AString) In

func (m AString) In(slice []T, customErrMsg ...string) A

In

Value expects to be equal to any of the provided elements.

Fails check, if no elements provided.

func (AString) LenEq

func (m AString) LenEq(eq int, customErrMsg ...string) A

LenEq

Length expects to be equal to "eq".

Logically incorrect params (e.g. negative values, etc.) are processed as usual.

func (AString) LenInRange

func (m AString) LenInRange(min, max int, customErrMsg ...string) A

LenInRange

Length expects to be in range [min, max].

Fails check, if min > max -- it works like empty range.

Logically incorrect params (e.g. negative values, etc.) are processed as usual.

func (AString) LenMax

func (m AString) LenMax(max int, customErrMsg ...string) A

LenMax

Length expects to be less or equal to "max".

Logically incorrect params (e.g. negative values, etc.) are processed as usual.

func (AString) LenMin

func (m AString) LenMin(min int, customErrMsg ...string) A

LenMin

Length expects to be greater or equal to "min".

Logically incorrect params (e.g. negative values, etc.) are processed as usual.

func (AString) LenNotEq

func (m AString) LenNotEq(notEq int, customErrMsg ...string) A

LenNotEq

Length expects to be not equal to "notEq".

Logically incorrect params (e.g. negative values, etc.) are processed as usual.

func (AString) LenNotInRange

func (m AString) LenNotInRange(min, max int, customErrMsg ...string) A

LenNotInRange

Length expects to be not in range [min, max] -- i.e. to be in ranges [0, min) or (max, MaxInt].

Passes check, if min > max -- it works like empty range.

Logically incorrect params (e.g. negative values, etc.) are processed as usual.

func (AString) Must

func (a AString) Must(v T, customErrMsg ...string)

Must

Calls Check and panics with the error if validation fails.

func (AString) MustAll

func (a AString) MustAll(v T)

MustAll

Calls CheckAll and panics with the error slice if validation fails.

func (AString) MustAllGet added in v0.3.0

func (a AString) MustAllGet(v T) T

MustAllGet

Works same as MustAll, but returns the value, if no panic occurred.

func (AString) MustGet added in v0.3.0

func (a AString) MustGet(v T, customErrMsg ...string) T

MustGet

Works same as Must, but returns the value, if no panic occurred.

func (*AString) NotContainsStr added in v0.2.0

func (a *AString) NotContainsStr(s string, customErrMsg ...string) *AString

NotContainsStr

Value expects to not contain provided substring.

Fails check, if empty substring provided.

See strings.Contains.

func (*AString) NotEmpty

func (a *AString) NotEmpty(customErrMsg ...string) *AString

NotEmpty -- alias to NotEq(time.Time{})

func (AString) NotEq

func (m AString) NotEq(notEq T, customErrMsg ...string) A

NotEq

Value expects to be not equal to "notEq".

func (AString) NotIn

func (m AString) NotIn(slice []T, customErrMsg ...string) A

NotIn

Value expects to be not equal to each of the provided elements.

Passes check, if no elements provided.

func (*AString) Numeric

func (a *AString) Numeric(customErrMsg ...string) *AString

Numeric

See StringRegexpNumeric

func (*AString) PrefixEq added in v0.2.0

func (a *AString) PrefixEq(eq string, customErrMsg ...string) *AString

PrefixEq

Value expects to have prefix equal to "eq".

Passes check, if empty prefix provided.

See strings.HasPrefix.

func (*AString) PrefixIn added in v0.2.0

func (a *AString) PrefixIn(in []string, customErrMsg ...string) *AString

PrefixIn

Value expects to have any of provided prefixes.

Fails check, if no prefixes provided.

See strings.HasPrefix.

func (*AString) PrefixNotEq added in v0.2.0

func (a *AString) PrefixNotEq(notEq string, customErrMsg ...string) *AString

PrefixNotEq

Value expects to have prefix not equal to "notEq".

Fails check, if empty prefix provided.

See strings.HasPrefix.

func (*AString) PrefixNotIn added in v0.2.0

func (a *AString) PrefixNotIn(notIn []string, customErrMsg ...string) *AString

PrefixNotIn

Value expects to have none of provided prefixes.

Passes check, if no prefixes provided.

See strings.HasPrefix.

func (*AString) Regexp

func (a *AString) Regexp(r *regexp.Regexp, customErrMsg ...string) *AString

func (*AString) RunesEq added in v0.2.0

func (a *AString) RunesEq(eq int, customErrMsg ...string) *AString

RunesEq

Runes count of the value expects to be equal to "eq".

Logically incorrect params (e.g. negative values, etc.) are processed as usual.

func (*AString) RunesInRange added in v0.2.0

func (a *AString) RunesInRange(min, max int, customErrMsg ...string) *AString

RunesInRange

Runes count of the value expects to be in range [min, max].

Fails check, if min > max -- it works like empty range.

Logically incorrect params (e.g. negative values, etc.) are processed as usual.

func (*AString) RunesMax added in v0.2.0

func (a *AString) RunesMax(max int, customErrMsg ...string) *AString

RunesMax

Runes count of the value expects to be less or equal to "max".

Logically incorrect params (e.g. negative values, etc.) are processed as usual.

func (*AString) RunesMin added in v0.2.0

func (a *AString) RunesMin(min int, customErrMsg ...string) *AString

RunesMin

Runes count of the value expects to be greater or equal to "min".

Logically incorrect params (e.g. negative values, etc.) are processed as usual.

func (*AString) RunesNotEq added in v0.2.0

func (a *AString) RunesNotEq(notEq int, customErrMsg ...string) *AString

RunesNotEq

Runes count of the value expects to be not equal to "notEq".

Logically incorrect params (e.g. negative values, etc.) are processed as usual.

func (*AString) RunesNotInRange added in v0.2.0

func (a *AString) RunesNotInRange(min, max int, customErrMsg ...string) *AString

RunesNotInRange

Runes count of the value expects to be not in range [min, max] -- i.e. to be in ranges [0, min) or (max, MaxInt].

Passes check, if min > max -- it works like empty range.

Logically incorrect params (e.g. negative values, etc.) are processed as usual.

func (*AString) SuffixEq added in v0.2.0

func (a *AString) SuffixEq(eq string, customErrMsg ...string) *AString

SuffixEq

Value expects to have suffix equal to "eq".

Passes check, if empty suffix provided.

See strings.HasSuffix.

func (*AString) SuffixIn added in v0.2.0

func (a *AString) SuffixIn(in []string, customErrMsg ...string) *AString

SuffixIn

Value expects to have any of provided suffixes.

Fails check, if no suffixes provided.

See strings.HasSuffix.

func (*AString) SuffixNotEq added in v0.2.0

func (a *AString) SuffixNotEq(notEq string, customErrMsg ...string) *AString

SuffixNotEq

Value expects to have suffix not equal to "notEq".

Fails check, if empty suffix provided.

See strings.HasSuffix.

func (*AString) SuffixNotIn added in v0.2.0

func (a *AString) SuffixNotIn(notIn []string, customErrMsg ...string) *AString

SuffixNotIn

Value expects to have none of provided suffixes.

Passes check, if no suffixes provided.

See strings.HasSuffix.

func (*AString) Word

func (a *AString) Word(customErrMsg ...string) *AString

Word

See StringRegexpWord

func (*AString) Words added in v0.4.0

func (a *AString) Words(customErrMsg ...string) *AString

Words

See StringRegexpWords

type ATime

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

func Time

func Time() *ATime

func (ATime) Check

func (a ATime) Check(v T, customErrMsg ...string) error

Check

Runs registered validation checks one by one against the given value and returns an error from a first failed check. Returns nil, if all checks pass.

func (ATime) CheckAll

func (a ATime) CheckAll(v T) []error

CheckAll

Runs registered validation checks one by one against the given value and returns errors from all failed checks. Returns empty slice, if all checks pass.

func (ATime) Custom

func (m ATime) Custom(check func(v T) error) A

func (ATime) Eq

func (m ATime) Eq(eq T, customErrMsg ...string) A

Eq

Value expects to be equal to "eq".

func (ATime) Greater

func (m ATime) Greater(than T, customErrMsg ...string) A

Greater

Value expects to be greater than "than".

func (ATime) GreaterAny

func (m ATime) GreaterAny(elems []T, customErrMsg ...string) A

GreaterAny

Value expects to be greater than any of provided elements.

Passes check, if no elements provided.

func (ATime) GreaterEach

func (m ATime) GreaterEach(elems []T, customErrMsg ...string) A

GreaterEach

Value expects to be greater than each of provided elements.

Passes check, if no elements provided.

func (ATime) GreaterEq

func (m ATime) GreaterEq(than T, customErrMsg ...string) A

GreaterEq

Value expects to be greater or equal to "than".

func (ATime) GreaterEqAny

func (m ATime) GreaterEqAny(elems []T, customErrMsg ...string) A

GreaterEqAny

Value expects to be greater or equal to any of provided elements.

Passes check, if no elements provided.

func (ATime) GreaterEqEach

func (m ATime) GreaterEqEach(elems []T, customErrMsg ...string) A

GreaterEqEach

Value expects to be greater or equal to each of provided elements.

Passes check, if no elements provided.

func (ATime) In

func (m ATime) In(slice []T, customErrMsg ...string) A

In

Value expects to be equal to any of the provided elements.

Fails check, if no elements provided.

func (ATime) InRange

func (m ATime) InRange(min T, max T, customErrMsg ...string) A

InRange

Value expects to be in range [min, max].

Fails check, if min > max -- it works like empty range.

func (ATime) Less

func (m ATime) Less(than T, customErrMsg ...string) A

Less

Value expects to be less than "than".

func (ATime) LessAny

func (m ATime) LessAny(elems []T, customErrMsg ...string) A

LessAny

Value expects to be less than any of provided elements.

Passes check, if no elements provided.

func (ATime) LessEach

func (m ATime) LessEach(elems []T, customErrMsg ...string) A

LessEach

Value expects to be less than each of provided elements.

Passes check, if no elements provided.

func (ATime) LessEq

func (m ATime) LessEq(than T, customErrMsg ...string) A

LessEq

Value expects to be less or equal to "than".

func (ATime) LessEqAny

func (m ATime) LessEqAny(elems []T, customErrMsg ...string) A

LessEqAny

Value expects to be less or equal to any of provided elements.

Passes check, if no elements provided.

func (ATime) LessEqEach

func (m ATime) LessEqEach(elems []T, customErrMsg ...string) A

LessEqEach

Value expects to be less or equal to each of provided elements.

Passes check, if no elements provided.

func (ATime) Must

func (a ATime) Must(v T, customErrMsg ...string)

Must

Calls Check and panics with the error if validation fails.

func (ATime) MustAll

func (a ATime) MustAll(v T)

MustAll

Calls CheckAll and panics with the error slice if validation fails.

func (ATime) MustAllGet added in v0.3.0

func (a ATime) MustAllGet(v T) T

MustAllGet

Works same as MustAll, but returns the value, if no panic occurred.

func (ATime) MustGet added in v0.3.0

func (a ATime) MustGet(v T, customErrMsg ...string) T

MustGet

Works same as Must, but returns the value, if no panic occurred.

func (ATime) NotEq

func (m ATime) NotEq(notEq T, customErrMsg ...string) A

NotEq

Value expects to be not equal to "notEq".

func (ATime) NotIn

func (m ATime) NotIn(slice []T, customErrMsg ...string) A

NotIn

Value expects to be not equal to each of the provided elements.

Passes check, if no elements provided.

func (ATime) NotInRange

func (m ATime) NotInRange(min T, max T, customErrMsg ...string) A

NotInRange

Value expects to be not in range [min, max] -- i.e. to be in ranges [PossibleMin, min) or (max, PossibleMax]

Passes check, if min > max -- it works like empty range.

func (*ATime) NotZero

func (a *ATime) NotZero(customErrMsg ...string) *ATime

NotZero -- alias to NotEq(time.Time{})

func (*ATime) Zero

func (a *ATime) Zero(customErrMsg ...string) *ATime

Zero -- alias to Eq(time.Time{})

type ATimeDuration

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

func TimeDur added in v0.3.0

func TimeDur() *ATimeDuration

func TimeDuration deprecated

func TimeDuration() *ATimeDuration

TimeDuration

Deprecated: use TimeDur() instead

func (ATimeDuration) Check

func (a ATimeDuration) Check(v T, customErrMsg ...string) error

Check

Runs registered validation checks one by one against the given value and returns an error from a first failed check. Returns nil, if all checks pass.

func (ATimeDuration) CheckAll

func (a ATimeDuration) CheckAll(v T) []error

CheckAll

Runs registered validation checks one by one against the given value and returns errors from all failed checks. Returns empty slice, if all checks pass.

func (ATimeDuration) Custom

func (m ATimeDuration) Custom(check func(v T) error) A

func (ATimeDuration) Eq

func (m ATimeDuration) Eq(eq T, customErrMsg ...string) A

Eq

Value expects to be equal to "eq".

func (ATimeDuration) Greater

func (m ATimeDuration) Greater(than T, customErrMsg ...string) A

Greater

Value expects to be greater than "than".

func (ATimeDuration) GreaterAny

func (m ATimeDuration) GreaterAny(elems []T, customErrMsg ...string) A

GreaterAny

Value expects to be greater than any of provided elements.

Passes check, if no elements provided.

func (ATimeDuration) GreaterEach

func (m ATimeDuration) GreaterEach(elems []T, customErrMsg ...string) A

GreaterEach

Value expects to be greater than each of provided elements.

Passes check, if no elements provided.

func (ATimeDuration) GreaterEq

func (m ATimeDuration) GreaterEq(than T, customErrMsg ...string) A

GreaterEq

Value expects to be greater or equal to "than".

func (ATimeDuration) GreaterEqAny

func (m ATimeDuration) GreaterEqAny(elems []T, customErrMsg ...string) A

GreaterEqAny

Value expects to be greater or equal to any of provided elements.

Passes check, if no elements provided.

func (ATimeDuration) GreaterEqEach

func (m ATimeDuration) GreaterEqEach(elems []T, customErrMsg ...string) A

GreaterEqEach

Value expects to be greater or equal to each of provided elements.

Passes check, if no elements provided.

func (ATimeDuration) In

func (m ATimeDuration) In(slice []T, customErrMsg ...string) A

In

Value expects to be equal to any of the provided elements.

Fails check, if no elements provided.

func (ATimeDuration) InRange

func (m ATimeDuration) InRange(min T, max T, customErrMsg ...string) A

InRange

Value expects to be in range [min, max].

Fails check, if min > max -- it works like empty range.

func (ATimeDuration) Less

func (m ATimeDuration) Less(than T, customErrMsg ...string) A

Less

Value expects to be less than "than".

func (ATimeDuration) LessAny

func (m ATimeDuration) LessAny(elems []T, customErrMsg ...string) A

LessAny

Value expects to be less than any of provided elements.

Passes check, if no elements provided.

func (ATimeDuration) LessEach

func (m ATimeDuration) LessEach(elems []T, customErrMsg ...string) A

LessEach

Value expects to be less than each of provided elements.

Passes check, if no elements provided.

func (ATimeDuration) LessEq

func (m ATimeDuration) LessEq(than T, customErrMsg ...string) A

LessEq

Value expects to be less or equal to "than".

func (ATimeDuration) LessEqAny

func (m ATimeDuration) LessEqAny(elems []T, customErrMsg ...string) A

LessEqAny

Value expects to be less or equal to any of provided elements.

Passes check, if no elements provided.

func (ATimeDuration) LessEqEach

func (m ATimeDuration) LessEqEach(elems []T, customErrMsg ...string) A

LessEqEach

Value expects to be less or equal to each of provided elements.

Passes check, if no elements provided.

func (ATimeDuration) Must

func (a ATimeDuration) Must(v T, customErrMsg ...string)

Must

Calls Check and panics with the error if validation fails.

func (ATimeDuration) MustAll

func (a ATimeDuration) MustAll(v T)

MustAll

Calls CheckAll and panics with the error slice if validation fails.

func (ATimeDuration) MustAllGet added in v0.3.0

func (a ATimeDuration) MustAllGet(v T) T

MustAllGet

Works same as MustAll, but returns the value, if no panic occurred.

func (ATimeDuration) MustGet added in v0.3.0

func (a ATimeDuration) MustGet(v T, customErrMsg ...string) T

MustGet

Works same as Must, but returns the value, if no panic occurred.

func (ATimeDuration) NotEq

func (m ATimeDuration) NotEq(notEq T, customErrMsg ...string) A

NotEq

Value expects to be not equal to "notEq".

func (ATimeDuration) NotIn

func (m ATimeDuration) NotIn(slice []T, customErrMsg ...string) A

NotIn

Value expects to be not equal to each of the provided elements.

Passes check, if no elements provided.

func (ATimeDuration) NotInRange

func (m ATimeDuration) NotInRange(min T, max T, customErrMsg ...string) A

NotInRange

Value expects to be not in range [min, max] -- i.e. to be in ranges [PossibleMin, min) or (max, PossibleMax]

Passes check, if min > max -- it works like empty range.

func (*ATimeDuration) NotZero

func (a *ATimeDuration) NotZero(customErrMsg ...string) *ATimeDuration

NotZero -- alias to NotEq(time.Duration(0))

func (*ATimeDuration) Zero

func (a *ATimeDuration) Zero(customErrMsg ...string) *ATimeDuration

Zero -- alias to Eq(time.Duration(0))

type Nilable added in v0.4.0

type Nilable interface {
	IsNil() bool
}

type NumericTypes

type NumericTypes interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
		~float32 | ~float64
}

type Zeroable added in v0.4.0

type Zeroable interface {
	IsZero() bool
}

Jump to

Keyboard shortcuts

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