Categorygithub.com/gonobo/validator
modulepackage
0.0.6
Repository: https://github.com/gonobo/validator.git
Documentation: pkg.go.dev

# README

validator

Test GoDoc Release

A simple library for validating conditions. Conditions are defined with rules; If the rule fails, an error is returned.

Installation

go get github.com/gonobo/validator

Usage

err := validator.Validate(
	validator.Rule(false, "must be false"),
)

if errors.Is(err, validator.ErrInvalid) {
	// handle validation error
}

The validator package also provides a function Any() that returns the first error encountered, or nil if all rules pass.

err := validator.Validate(
	validator.Any(
		validator.Rule(false, "must be false"),
		validator.Rule(true, "must be true"),
	),
) // returns "validation error: must be false"

The validator package also provides a function All() that evaluates all rules in the list:

err := validator.Validate(
	validator.All(
		validator.Rule(true, "must be true"),
		validator.Rule(false, "must be false"),
	),
) // returns "validation error: must be false"

if errors.Is(err, validator.ErrInvalid) {
	// handle validation error
}

# Functions

All evaluates all rules in the list, and returns the first error encountered, or nil if all rules pass.
Any evaluates all rules in the list and returns the first error encountered, or nil if all rules pass.
If returns a rule that is only executed if the given condition is true.
Rule returns a validation rule that checks if the given condition evaluates to true.
Validate evaluates the given validation rule.

# Variables

ErrInvalid is the sentinel error that is wrapped by any error returned by Validate().

# Type aliases

ValidationRule functions return an error if the validation fails.