assert

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2025 License: MIT Imports: 7 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.String().Word().LenMax(5).Check(value)

Reasons

If you’ve ever worked with php, you probably used webmozart/assert package. It made it easy to validate method arguments with 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 et les femmes fatales provides a simple type-specific fluent interface to build validations in a way like

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

The package provides assertions for specific types:

as well as more abstract assertions for broader type support:

  • Comparable -- for any comparable type
  • SliceAny -- for slice-based types with any type of elements
  • SliceCmp -- for slice-based types with comparable type of elements

Each assertion supports only specific methods related to its value type -- this protects you from accidental mistakes. For example, it is impossible to validate integer value via some length-based rule, because Numeric 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.

Assertions support two types of results: panic (via the Must() or MustAll() methods) and returning errors (via the Check() or CheckAll() methods).

Error messages can be customized for any rule as well as for the whole chain.

Examples

Method arguments assertion (primary use-case)

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.

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.Comparable[*EventCollection]().NotEq(nil).Must(evs)

	// ...

	return nil
}
Simple validation (secondary use-case)

Looks the same argument assertion, but customizing messages makes possible to use this package in outer layers, closer to end-user interaction code, etc.

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.String().Word().Check(value, fmt.Sprintf("Name %q is incorrect!", value))

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

	return Name{value: value}, nil
}
Form validation (possible use-case)

This package is not about a form validation, but it can be used as a "brick" to build things you need without heavy validation libraries.

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.String().
		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.String().
			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.Numeric[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 (String, Numeric, etc.)
  • readme_test.go — examples from the Readme to be sure they really work :)

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.

Functions

This section is empty.

Types

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) 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 Comparable

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

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) 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 Numeric

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

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[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) 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) 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 String

func String() *AString

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) 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

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) 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 TimeDuration

func TimeDuration() *ATimeDuration

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) 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 NumericTypes

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

Jump to

Keyboard shortcuts

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