validol

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 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), // to continue the `Walk` using the descendants' `Validate` methods
	)
}

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

Validators

Type Validator[T] is equivalent to func(T) error.
Validatable interface requires Validate() error method.

Name Input Description
Validate T If T is Validatable, then it will call Validate method, otherwise will call Walk
Walk T Recursively calls Validate method for descendants of T. The descendants of the Validatable descendant will not be checked automatically, instead the type must continue Walk manually (inside its own Validate). The descendants are public struct fields, embedded types, 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
True bool Checks that the value is true
False bool Checks that the value is false
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
And Validator[T], Validator[T] Validator[T] Checks that both validations have been completed successfully
Or Validator[T], Validator[T] Validator[T] Checks the success of one of the two validations
OneOf ...T Validator[T] Checks that the value is equal to one of the specified
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 False added in v0.0.6

func False(b bool) 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 True added in v0.0.6

func True(b bool) 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 And added in v0.0.5

func And[T any](first, second 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 Or added in v0.0.5

func Or[T any](first, second Validator[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