check

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2023 License: MPL-2.0 Imports: 2 Imported by: 0

README ¶

check

checks pkg.go.dev goreportcard codecov

Convenience helpers to perform validations of any kind

📦 Install

go get go-simpler.org/check

📋 Usage

Use That/Thatf to write conditions to check, multiple calls can be chained. The last call in the chain must be FirstError, AllErrors, or JoinErrors.

err := check.
    That(user.Name != "", errEmptyName).
    Thatf(user.Age >= 18, "%d y.o. is too young", user.Age).
    Thatf(isEmail(user.Email), "%s is invalid email", user.Email).
    JoinErrors() // or FirstError() / AllErrors().

Documentation ¶

Overview ¶

Package check provides convenience helpers to perform validations of any kind.

Use That/Thatf to write conditions to check, multiple calls can be chained. The last call in the chain must be FirstError, AllErrors, or JoinErrors.

Example ¶
package main

import (
	"errors"
	"fmt"

	"go-simpler.org/check"
)

var user = struct {
	Name  string
	Age   int
	Email string
}{
	Name:  "",
	Age:   10,
	Email: "user@email",
}

func isEmail(string) bool { return false }

var errEmptyName = errors.New("name must not be empty")

func main() {
	err := check.
		That(user.Name != "", errEmptyName).
		Thatf(user.Age >= 18, "%d y.o. is too young", user.Age).
		Thatf(isEmail(user.Email), "%s is invalid email", user.Email).
		JoinErrors() // or FirstError() / AllErrors().

	fmt.Println(err)
}
Output:

name must not be empty
10 y.o. is too young
user@email is invalid email

Index ¶

Examples ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

This section is empty.

Types ¶

type State ¶

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

State holds the recorded errors. It is exported only for the purpose of documentation.

func That ¶

func That(cond bool, err error) *State

That checks whether the condition is true, and if not, records the error.

func Thatf ¶

func Thatf(cond bool, format string, args ...any) *State

Thatf checks whether the condition is true, and if not, creates an error from format and args, then records it.

func (*State) AllErrors ¶

func (s *State) AllErrors() []error

AllErrors returns all the recorded errors.

func (*State) FirstError ¶

func (s *State) FirstError() error

FirstError returns the first recorded error.

func (*State) JoinErrors ¶ added in v0.4.0

func (s *State) JoinErrors() error

JoinErrors returns all the recorded errors joined via errors.Join.

func (*State) That ¶

func (s *State) That(cond bool, err error) *State

That checks whether the condition is true, and if not, records the error.

func (*State) Thatf ¶

func (s *State) Thatf(cond bool, format string, args ...any) *State

Thatf checks whether the condition is true, and if not, creates an error from format and args, then records it.

Jump to

Keyboard shortcuts

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