validol

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

README

validol

github goref

Validation library for golang.

Content

Install

go get -u github.com/cospectrum/validol

Requires Go version 1.22.0 or greater.

Usage

import (
	"errors"
	vd "github.com/cospectrum/validol"
)

type Sex string

func (s Sex) Validate() error {
	return vd.OneOf[Sex]("male", "female", "other")(s)
}

type Email string

func (e Email) Validate() error {
	return vd.All(
		vd.Len[string](vd.Gt(5)),
		vd.Len[string](vd.Lte(100)),
		vd.Email,
	)(string(e))
}

type Info struct {
	Email email
	Sex   Sex
	age   uint
}

func (info Info) Validate() error {
	return errors.Join(
		vd.Walk(info),
		vd.Gte(uint(18))(info.age),
	)
}

func main() {
	var info Info
	if err := info.Validate(); err != nil {
		panic(err)
	}
}

Validators

Type validol.Validator[T] is equivalent to func(T) error.

Name Input Description
Walk any Recursively checks all descendants that have the Validate() error method. The descendants are public struct fields, slice/array elements, map keys/values
Email string Email string
UUID4 string Universally Unique Identifier UUID v4

Combinators

Functions that create a Validator[T].

Name Input Output Description
All ...Validator[T] Validator[T] Checks whether all validations have been completed successfully
Any ...Validator[T] Validator[T] Checks that at least one validation has been completed successfully
Not Validator[T] Validator[T] Logical not
OneOf ...T Validator[T] Checks that the value is equal to one of the arguments
Eq T comparable Validator[T] ==
Ne T comparable Validator[T] !=
Gt T cmp.Ordered Validator[T] >
Gte T cmp.Ordered Validator[T] >=
Lt T cmp.Ordered Validator[T] <
Lte T cmp.Ordered Validator[T] <=
Len Validator[int] Validator[T] Checks that the len passes the specified Validator[int]
StartsWith string Validator[string] Checks if the string starts with the specified prefix
EndsWith string Validator[string] Checks whether the string ends with the specified suffix
Contains string Validator[string] Checks whether the specified substr is within string
ContainsRune rune Validator[string] Checks whether the specified Unicode code point is within string

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Email

func Email(s string) error

func UUID4

func UUID4(s string) error

func Walk

func Walk[T any](t T) error

Types

type Validatable

type Validatable interface {
	Validate() error
}

type Validator

type Validator[Of any] func(Of) error

func All

func All[T any](validators ...Validator[T]) Validator[T]

func Any

func Any[T any](validators ...Validator[T]) Validator[T]

func Contains

func Contains(substr string) Validator[string]

func ContainsRune

func ContainsRune(r rune) Validator[string]

func EndsWith

func EndsWith(suffix string) Validator[string]

func Eq

func Eq[T comparable](val T) Validator[T]

func Gt

func Gt[T cmp.Ordered](val T) Validator[T]

func Gte

func Gte[T cmp.Ordered](val T) Validator[T]

func Len

func Len[T any](validateLen Validator[int]) Validator[T]

func Lt

func Lt[T cmp.Ordered](val T) Validator[T]

func Lte

func Lte[T cmp.Ordered](val T) Validator[T]

func Ne

func Ne[T comparable](val T) Validator[T]

func Not

func Not[T any](fn Validator[T]) Validator[T]

func OneOf

func OneOf[T comparable](vals ...T) Validator[T]

func StartsWith

func StartsWith(prefix string) Validator[string]

Directories

Path Synopsis
examples
map command
slice command

Jump to

Keyboard shortcuts

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