assert

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2025 License: MIT Imports: 4 Imported by: 0

README

Assertions

TL;DR

Simple, type‑specific fluent assertions for Go -- initially inspired by php webmozart/assert.

Here's the thing

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, so something else is needed...

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

So yes -- this package un autre package de validation, mais avec les cartes et les femmes fatales provides a type-specific fluent interface to build validations in a way like assert.String().Word().LenMax(5).Check(value).

As is usual in Go, it supports two types of results: panic (via the Must() or MustAll() methods) and returning errors (via the Check() or CheckAll() methods).

The package provides popular type‑specific assertions -- such as String(), Numeric[T NumericTypes](), Time(), etc. -- as well as the ability to work with more abstract assertions like Comparable[T comparable]() for broader type support.

Each assertion also supports a Custom() check for specific cases.

Examples
package example

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

func NameFromString(value string) (Name, error) {
	err := assert.String().
		Word().
		LenMax(100, "Too long, isn’t it?" /* <-- custom error message only for this fail */).
		NotIn([]string{"some", "bad", "words"}).
		Custom(func(value string) error {
			// if ... { return err }
			return nil
		}).
		Check(value, "Name is incorrect" /* <-- custom error message that overrides any other in the chain */)

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

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

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

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

func (a *Account) Deactivate(now time.Time, evs *EventCollection) error {
	assert.Time().NotZero().Must(now)
	assert.Comparable[*EventCollection]().NotEq(nil).Must(evs)

	if a.IsDeactivated() {
		return common.NewErrorAlreadyDone()
	}

	a.deactivatedAt = now

	evs.Add(NewEventDeactivated(now, a.id))

	return nil
}

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

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

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

func (ABool) NotIn

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

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

func (AComparable) In

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

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

func (AComparable) NotIn

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

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

func (ANumeric) Greater

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

func (ANumeric) GreaterAny

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

func (ANumeric) GreaterEach

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

func (ANumeric) GreaterEq

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

func (ANumeric) GreaterEqAny

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

func (ANumeric) GreaterEqEach

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

func (ANumeric) In

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

func (ANumeric) InRange

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

func (ANumeric) Less

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

func (ANumeric) LessAny

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

func (ANumeric) LessEach

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

func (ANumeric) LessEq

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

func (ANumeric) LessEqAny

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

func (ANumeric) LessEqEach

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

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

func (ANumeric) NotIn

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

func (ANumeric) NotInRange

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

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

func (AString) In

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

func (*AString) LenEq

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

func (*AString) LenInRange

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

func (*AString) LenMax

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

func (*AString) LenMin

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

func (*AString) LenNotEq

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

func (*AString) LenNotInRange

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

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

func (AString) NotIn

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

func (*AString) Numeric

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

Numeric

See StringRegexpNumeric

func (*AString) Regexp

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

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

func (ATime) Greater

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

func (ATime) GreaterAny

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

func (ATime) GreaterEach

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

func (ATime) GreaterEq

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

func (ATime) GreaterEqAny

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

func (ATime) GreaterEqEach

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

func (ATime) In

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

func (ATime) InRange

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

func (ATime) Less

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

func (ATime) LessAny

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

func (ATime) LessEach

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

func (ATime) LessEq

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

func (ATime) LessEqAny

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

func (ATime) LessEqEach

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

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

func (ATime) NotIn

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

func (ATime) NotInRange

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

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

func (ATimeDuration) Greater

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

func (ATimeDuration) GreaterAny

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

func (ATimeDuration) GreaterEach

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

func (ATimeDuration) GreaterEq

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

func (ATimeDuration) GreaterEqAny

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

func (ATimeDuration) GreaterEqEach

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

func (ATimeDuration) In

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

func (ATimeDuration) InRange

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

func (ATimeDuration) Less

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

func (ATimeDuration) LessAny

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

func (ATimeDuration) LessEach

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

func (ATimeDuration) LessEq

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

func (ATimeDuration) LessEqAny

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

func (ATimeDuration) LessEqEach

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

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

func (ATimeDuration) NotIn

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

func (ATimeDuration) NotInRange

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

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