validol

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 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 User struct {
	Email Email
	Sex   Sex
	age   int
}

func (u User) Validate() error {
	return errors.Join(
		vd.Gte(18)(u.age),
		vd.Walk(u),
	)
}

func main() {
	users := []User{
		{Email: "first_user@mail.com", age: 22},
		{Email: "second_user@mail.com", Sex: "male"},
	}
	if err := vd.Validate(users); err != nil {
		panic(err)
	}
}

Validators

Type Validator[T] is equivalent to func(T) error.
For validation during recursive Walk, you can implement Validatable interface, which requires a single Validate() error method.

Name Input Description
Validate T If T has Validate() error method, then it will call it, otherwise will call Walk
Walk T Recursively checks all descendants that have the Validate() error method. The descendants are public struct fields, slice/array elements, map keys/values
Required T Checks that the value is different from default
Empty T Checks that the value is initialized as default
NotNil T Checks that the value is different from nil
Nil T Checks that the value is nil
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 whether the len of the object 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 Empty added in v0.0.3

func Empty[T any](t T) error

func Nil added in v0.0.3

func Nil[T any](t T) error

func NotNil added in v0.0.3

func NotNil[T any](t T) error

func Required added in v0.0.3

func Required[T any](t T) error

func UUID4

func UUID4(s string) error

func Validate added in v0.0.4

func Validate[T any](t T) 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