please

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2025 License: MIT Imports: 9 Imported by: 1

README

please - generic data validation library for Go

GoDoc Go Report Card License

Installation

go get -u github.com/zhassymov/please

please is a library for data validation in the Go programming language, designed to simplify checking various conditions and constraints imposed on data.

Goal

The goal of library is to provide convenient and flexible tools for validating data against specified criteria. This can be useful for validating user input, working with APIs, data processing, and other scenarios that require checking and confirming the format or content of data.

Features

  • compile-time type checking
  • no reflection
  • multiple, complex and configurable validation rules
  • custom validation rules
  • custom error messages
  • nested data validation

Usage

Simple Example
package main

import (
    "fmt"
    "github.com/zhassymov/please"
)

func main() {
    password := "userPassword"
    err := please.Join(password,
        please.StringUTF8(),                            // validate the string is valid UTF-8
        please.StringLenBetween(16, 64),                // validate the string length is between 16 and 64 characters
        please.StringAlphaNumeric(),                    // validate the string contains only alphanumeric characters
        please.StringMinUniqueRuneCount(8),             // validate the string contains at least 8 unique characters
        please.StringContainsAny("123456789"),          // validate the string contains at least one number
        please.StringContainsAny(`!"#$%&'()*+,-./`),    // validate the string contains at least one of the special characters
    )
    if err != nil {
        fmt.Println(err)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abort

func Abort[T any](value T, opts ...Validate[T]) error

Abort returns the first error when executing the validation functions and aborts the execution.

func Collect

func Collect[T any](value T, opts ...Validate[T]) []error

Collect collects all errors when executing the validation functions.

func Join

func Join[T any](value T, opts ...Validate[T]) error

Join joins all errors using the errors.Join function when executing the validation functions.

func JoinFunc

func JoinFunc[T any](value T, join func(...error) error, opts ...Validate[T]) error

JoinFunc joins all errors using the specified join function when executing the validation functions.

Types

type Validate

type Validate[T any] func(T) error

Validate is a generic type for validation functions.

func Between

func Between[T cmp.Ordered](x, y T) Validate[T]

Between returns a validation function that checks whether the value is between the minimal and maximal values.

func Email added in v0.0.3

func Email() Validate[string]

Email returns a validation function that checks whether the string is a valid email address.

func Empty

func Empty[T comparable]() Validate[T]

Empty returns a validation function that checks whether the value is empty.

func Equal

func Equal[T comparable](target T) Validate[T]

Equal returns a validation function that checks whether the value is equal to the target.

func Max

func Max[T cmp.Ordered](maximal T) Validate[T]

Max returns a validation function that checks whether the value is less or equal than the maximal value.

func Min

func Min[T cmp.Ordered](minimal T) Validate[T]

Min returns a validation function that checks whether the value is greater or equal than the minimal value.

func NotBetween

func NotBetween[T cmp.Ordered](x, y T) Validate[T]

NotBetween returns a validation function that checks whether the value is not between the minimal and maximal values.

func NotEmpty

func NotEmpty[T comparable]() Validate[T]

NotEmpty returns a validation function that checks whether the value is not empty.

func NotEqual

func NotEqual[T comparable](target T) Validate[T]

NotEqual returns a validation function that checks whether the value is not equal to the target.

func NotOneIn

func NotOneIn[T comparable](enum map[T]bool) Validate[T]

NotOneIn returns a validation function that checks whether the value does not exist in the enum map keys.

func NotOneOf

func NotOneOf[T comparable](enum ...T) Validate[T]

NotOneOf returns a validation function that checks whether the value does not exist in the enum slice.

func Nothing added in v0.0.2

func Nothing[T any]() Validate[T]

Nothing returns a validation function that always returns nil.

func OneIn

func OneIn[T comparable](enum map[T]bool) Validate[T]

OneIn returns a validation function that checks whether the value exists in the enum map keys.

func OneOf

func OneOf[T comparable](enum ...T) Validate[T]

OneOf returns a validation function that checks whether the value exists in the enum slice.

func SliceContain added in v0.0.3

func SliceContain[S ~[]E, E comparable](value E) Validate[S]

SliceContain returns a validation function that checks whether the slice contains the specified value.

func SliceEach added in v0.0.3

func SliceEach[S ~[]E, E any](opts ...Validate[E]) Validate[S]

SliceEach returns a validation function that checks whether each element in the slice satisfies the specified validation functions.

func SliceLen added in v0.0.3

func SliceLen[S ~[]E, E any](n int) Validate[S]

SliceLen returns a validation function that checks whether the length of the slice is equal to the specified number.

func SliceLenBetween added in v0.0.3

func SliceLenBetween[S ~[]E, E any](x, y int) Validate[S]

SliceLenBetween returns a validation function that checks whether the length of the slice is between the specified numbers.

func SliceLenNotBetween added in v0.0.3

func SliceLenNotBetween[S ~[]E, E any](x, y int) Validate[S]

SliceLenNotBetween returns a validation function that checks whether the length of the slice is not between the specified numbers.

func SliceMaxLen added in v0.0.3

func SliceMaxLen[S ~[]E, E any](n int) Validate[S]

SliceMaxLen returns a validation function that checks whether the length of the slice is at most the specified number.

func SliceMinLen added in v0.0.3

func SliceMinLen[S ~[]E, E any](n int) Validate[S]

SliceMinLen returns a validation function that checks whether the length of the slice is at least the specified number.

func SliceNotContain added in v0.0.3

func SliceNotContain[S ~[]E, E comparable](value E) Validate[S]

SliceNotContain returns a validation function that checks whether the slice does not contain the specified value.

func StringAllow added in v0.0.3

func StringAllow(charset string) Validate[string]

StringAllow returns a validation function that checks whether the string contains only allowed characters.

func StringAlpha added in v0.0.3

func StringAlpha() Validate[string]

StringAlpha returns a validation function that checks whether the string contains only alphabet characters.

func StringAlphaNumeric added in v0.0.3

func StringAlphaNumeric() Validate[string]

StringAlphaNumeric returns a validation function that checks whether the string contains only alphanumeric characters.

func StringContains added in v0.0.3

func StringContains(substr string) Validate[string]

StringContains returns a validation function that checks whether the string contains the specified substring.

func StringContainsAny added in v0.0.3

func StringContainsAny(charset string) Validate[string]

func StringHasPrefix added in v0.0.3

func StringHasPrefix(prefix string) Validate[string]

StringHasPrefix returns a validation function that checks whether the string begins with prefix.

func StringHasSuffix added in v0.0.3

func StringHasSuffix(suffix string) Validate[string]

StringHasSuffix returns a validation function that checks whether the string ends with suffix.

func StringLen added in v0.0.3

func StringLen(n int) Validate[string]

StringLen returns a validation function that checks whether the length of the string is equal to the specified number.

func StringLenBetween added in v0.0.3

func StringLenBetween(x, y int) Validate[string]

StringLenBetween returns a validation function that checks whether the length of the string is between the specified number.

func StringLenNotBetween added in v0.0.3

func StringLenNotBetween(x, y int) Validate[string]

StringLenNotBetween returns a validation function that checks whether the length of the string is not between the specified number.

func StringMaxLen added in v0.0.3

func StringMaxLen(n int) Validate[string]

StringMaxLen returns a validation function that checks whether the length of the string is at most the specified number.

func StringMaxRuneCount added in v0.0.3

func StringMaxRuneCount(n int) Validate[string]

StringMaxRuneCount returns a validation function that checks whether the number of runes in the string is at most the specified number.

func StringMaxUniqueRuneCount added in v0.0.3

func StringMaxUniqueRuneCount(n int) Validate[string]

StringMaxUniqueRuneCount returns a validation function that checks whether the number of unique runes in the string is at most the specified number.

func StringMinLen added in v0.0.3

func StringMinLen(n int) Validate[string]

StringMinLen returns a validation function that checks whether the length of the string is at least the specified number.

func StringMinRuneCount added in v0.0.3

func StringMinRuneCount(n int) Validate[string]

StringMinRuneCount returns a validation function that checks whether the number of runes in the string is at least the specified number.

func StringMinUniqueRuneCount added in v0.0.3

func StringMinUniqueRuneCount(n int) Validate[string]

StringMinUniqueRuneCount returns a validation function that checks whether the number of unique runes in the string is at least the specified number.

func StringNotAllow added in v0.0.3

func StringNotAllow(charset string) Validate[string]

StringNotAllow returns a validation function that checks whether the string does not contain disallowed characters.

func StringNotContains added in v0.0.3

func StringNotContains(substr string) Validate[string]

StringNotContains returns a validation function that checks whether the string does not contain the specified substring.

func StringNotHasPrefix added in v0.0.3

func StringNotHasPrefix(prefix string) Validate[string]

StringNotHasPrefix returns a validation function that checks whether the string does not begin with prefix.

func StringNotHasSuffix added in v0.0.3

func StringNotHasSuffix(suffix string) Validate[string]

StringNotHasSuffix returns a validation function that checks whether the string does not end with suffix.

func StringNumeric added in v0.0.3

func StringNumeric() Validate[string]

StringNumeric returns a validation function that checks whether the string contains only numeric characters.

func StringPrintableASCII added in v0.0.5

func StringPrintableASCII() Validate[string]

StringASCII returns a validation function that checks whether the string contains only ASCII printable characters.

func StringRuneCount added in v0.0.3

func StringRuneCount(n int) Validate[string]

StringRuneCount returns a validation function that checks whether the number of runes in the string is exactly equal to the specified number.

func StringRuneCountBetween added in v0.0.3

func StringRuneCountBetween(x, y int) Validate[string]

StringRuneCountBetween returns a validation function that checks whether the number of runes in the string is between the specified numbers.

func StringRuneCountNotBetween added in v0.0.3

func StringRuneCountNotBetween(x, y int) Validate[string]

StringRuneCountNotBetween returns a validation function that checks whether the number of runes in the string is not between the specified numbers.

func StringUTF8 added in v0.0.3

func StringUTF8() Validate[string]

StringUTF8 returns a validation function that checks whether the string is a valid UTF-8 string.

func StringUnicodeDigits added in v0.0.3

func StringUnicodeDigits() Validate[string]

StringUnicodeDigits returns a validation function that checks whether the string contains only unicode digits.

func StringUnicodeLetters added in v0.0.3

func StringUnicodeLetters() Validate[string]

StringUnicodeLetters returns a validation function that checks whether the string contains only unicode letters.

func StringUniqueRuneCount added in v0.0.3

func StringUniqueRuneCount(n int) Validate[string]

StringUniqueRuneCount returns a validation function that checks whether the number of unique runes in the string is exactly equal to the specified number.

func StringUniqueRuneCountBetween added in v0.0.3

func StringUniqueRuneCountBetween(x, y int) Validate[string]

StringUniqueRuneCountBetween returns a validation function that checks whether the number of unique runes in the string is between the specified numbers.

func StringUniqueRuneCountNotBetween added in v0.0.3

func StringUniqueRuneCountNotBetween(x, y int) Validate[string]

StringUniqueRuneCountNotBetween returns a validation function that checks whether the number of unique runes in the string is not between the specified numbers.

func UUID added in v0.0.4

func UUID() Validate[string]

func (Validate[T]) WithError

func (v Validate[T]) WithError(cause error) Validate[T]

WithError returns a new validation function that wraps the original validation function and returns the specified error.

func (Validate[T]) WrapError added in v0.0.3

func (v Validate[T]) WrapError(cause error) Validate[T]

WrapError returns a new validation function that wraps the original validation function and returns the wrapped error.

Jump to

Keyboard shortcuts

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