Documentation
¶
Overview ¶
Package validation provides configurable and extensible data validation capabilities with use of generics.
Example (Config) ¶
package main
import (
"encoding/json"
"fmt"
"os"
"codeberg.org/go-toolbox/validation"
isint "codeberg.org/go-toolbox/validation/is/int"
isstr "codeberg.org/go-toolbox/validation/is/str"
)
type Config struct {
Logger LoggerConfig
Database DatabaseConfig
}
func (cfg *Config) Validate() error {
return validation.All(
validation.PtrOf(&cfg.Logger, "logger").With(validation.Custom),
validation.PtrOf(&cfg.Database, "database").With(validation.Custom),
)
}
type LoggerConfig struct {
Level string
}
func (cfg *LoggerConfig) Validate() error {
return validation.All(
validation.StringOf(cfg.Level, "level").Required(true).In("debug", "info", "warn", "error"),
)
}
type DatabaseConfig struct {
Backend string
Postgres PostgresConfig
SQLite SQLiteConfig
}
func (cfg *DatabaseConfig) Validate() error {
return validation.All(
validation.StringOf(cfg.Backend, "backend").Required(true).In("postgres", "sqlite"),
func() validation.Validator {
switch cfg.Backend {
case "postgres":
return validation.PtrOf(&cfg.Postgres, "postgres").With(validation.Custom)
case "sqlite":
return validation.PtrOf(&cfg.SQLite, "sqlite").With(validation.Custom)
default:
return nil
}
}(),
)
}
type PostgresConfig struct {
Host string
Port int
}
func (cfg *PostgresConfig) Validate() error {
return validation.All(
validation.StringOf(cfg.Host, "host").Required(true).With(isstr.Host),
validation.NumberOf(cfg.Port, "port").Required(true).With(isint.Port),
)
}
type SQLiteConfig struct {
Path string
}
func (cfg *SQLiteConfig) Validate() error {
return validation.All(
validation.StringOf(cfg.Path, "path").Required(true),
)
}
func main() {
sqlite := DatabaseConfig{
Backend: "sqlite",
}
fmt.Println(sqlite.Validate())
postgres := DatabaseConfig{
Backend: "postgres",
}
fmt.Println(postgres.Validate())
config := Config{
Logger: LoggerConfig{
Level: "panic",
},
Database: DatabaseConfig{
Backend: "postgres",
},
}
b, _ := json.MarshalIndent(config.Validate(), "", " ")
os.Stdout.Write(b)
}
Output:
Example (Request) ¶
package main
import (
"encoding/json"
"os"
"time"
"codeberg.org/go-toolbox/validation"
)
type GeneralInfo struct {
UserID [16]byte `json:"user_id"`
Device Device `json:"device"`
AppVersion string `json:"app_version"`
}
func (info *GeneralInfo) Validate() error {
return validation.All(
validation.ComparableOf(info.UserID, "user_id").Required(true),
validation.PtrOf(&info.Device, "device").With(validation.Custom),
validation.StringOf(info.AppVersion, "app_version").Required(true),
)
}
type Device struct {
Manufacturer string `json:"manufacturer"`
Model string `json:"model"`
BuildNumber string `json:"build_number"`
OS string `json:"os"`
OSVersion string `json:"os_version"`
ScreenWidth uint32 `json:"screen_width"`
ScreenHeight uint32 `json:"screen_height"`
}
func (d *Device) Validate() error {
return validation.All(
validation.StringOf(d.Manufacturer, "manufacturer").Required(true),
validation.StringOf(d.Model, "model").Required(true),
validation.StringOf(d.BuildNumber, "build_number").Required(true),
)
}
type Telemetry struct {
Action string `json:"action"`
Data map[string]any `json:"data"`
Timestamp time.Time `json:"timestamp"`
}
func (t *Telemetry) Validate() error {
return validation.All(
validation.StringOf(t.Action, "action").Required(true),
validation.MapOf(t.Data, "data").NotNil(true),
validation.TimeOf(t.Timestamp, "timestamp").Required(true),
)
}
type TrackRequest struct {
Info GeneralInfo `json:"info"`
Data []Telemetry `json:"data"`
}
func (tr *TrackRequest) Validate() error {
return validation.All(
validation.PtrOf(&tr.Info, "info").With(validation.Custom),
validation.SliceOf(tr.Data, "data").NotNil(true).ValuesPtrWith(validation.Custom),
)
}
func main() {
tr := TrackRequest{}
str, _ := json.Marshal(tr.Validate())
os.Stdout.Write(str)
}
Output:
Index ¶
- Variables
- func All(validators ...Validator) error
- func Custom[T Validatable](v T) error
- func Empty[T comparable](v T) error
- func EmptyMap[M ~map[K]V, K comparable, V any](m M) error
- func EmptySlice[S ~[]T, T any](s S) error
- func EmptyTime(t time.Time) error
- func NilMap[M ~map[K]V, K comparable, V any](m M) error
- func NilOrNotEmptyMap[M ~map[K]V, K comparable, V any](m M) error
- func NilOrNotEmptySlice[S ~[]T, T any](s S) error
- func NilPtr[T any](p *T) error
- func NilSlice[S ~[]T, T any](s S) error
- func NotNilMap[M ~map[K]V, K comparable, V any](m M) error
- func NotNilPtr[T any](p *T) error
- func NotNilSlice[S ~[]T, T any](s S) error
- func Required[T comparable](v T) error
- func RequiredMap[M ~map[K]V, K comparable, V any](m M) error
- func RequiredSlice[S ~[]T, T any](s S) error
- func RequiredTime(t time.Time) error
- type AnyRule
- type AnyRuleFunc
- func BetweenAny[T any](cmp func(a, b T) int, a, b T) AnyRuleFunc[T]
- func BetweenEqualAny[T any](cmp func(a, b T) int, a, b T) AnyRuleFunc[T]
- func CustomRule[T Validatable]() AnyRuleFunc[T]
- func EqualAny[T any](eq func(a, b T) bool, b T) AnyRuleFunc[T]
- func GreaterAny[T any](cmp func(a, b T) int, b T) AnyRuleFunc[T]
- func GreaterEqualAny[T any](cmp func(a, b T) int, b T) AnyRuleFunc[T]
- func InAny[T any](eq func(a, b T) bool, elements ...T) AnyRuleFunc[T]
- func LessAny[T any](cmp func(a, b T) int, b T) AnyRuleFunc[T]
- func LessEqualAny[T any](cmp func(a, b T) int, b T) AnyRuleFunc[T]
- func NotInAny[T any](eq func(a, b T) bool, elements ...T) AnyRuleFunc[T]
- func RequiredAny[T any](isZero func(a T) bool) AnyRuleFunc[T]
- type AnyValidator
- func (av AnyValidator[T]) Between(cmp func(a, b T) int, a, b T) AnyValidator[T]
- func (av AnyValidator[T]) BetweenEqual(cmp func(a, b T) int, a, b T) AnyValidator[T]
- func (av AnyValidator[T]) By(rules ...AnyRule[T]) AnyValidator[T]
- func (av AnyValidator[T]) Equal(eq func(a, b T) bool, v T) AnyValidator[T]
- func (av AnyValidator[T]) Err() error
- func (av AnyValidator[T]) Greater(cmp func(a, b T) int, v T) AnyValidator[T]
- func (av AnyValidator[T]) GreaterEqual(cmp func(a, b T) int, v T) AnyValidator[T]
- func (av AnyValidator[T]) In(eq func(a, b T) bool, elements ...T) AnyValidator[T]
- func (av AnyValidator[T]) Less(cmp func(a, b T) int, v T) AnyValidator[T]
- func (av AnyValidator[T]) LessEqual(cmp func(a, b T) int, v T) AnyValidator[T]
- func (av AnyValidator[T]) NotIn(eq func(a, b T) bool, elements ...T) AnyValidator[T]
- func (av AnyValidator[T]) Required(condition bool, isDefault func(v T) bool) AnyValidator[T]
- func (av AnyValidator[T]) Validate(v T) error
- func (av AnyValidator[T]) With(fns ...func(v T) error) AnyValidator[T]
- type ComparableRule
- type ComparableRuleFunc
- type ComparableValidator
- func (cv ComparableValidator[T]) By(rules ...ComparableRule[T]) ComparableValidator[T]
- func (cv ComparableValidator[T]) Equal(v T) ComparableValidator[T]
- func (cv ComparableValidator[T]) Err() error
- func (cv ComparableValidator[T]) In(elements ...T) ComparableValidator[T]
- func (cv ComparableValidator[T]) NotIn(elements ...T) ComparableValidator[T]
- func (cv ComparableValidator[T]) Required(condition bool) ComparableValidator[T]
- func (cv ComparableValidator[T]) Validate(v T) error
- func (cv ComparableValidator[T]) With(fns ...func(v T) error) ComparableValidator[T]
- type Errors
- type IndexError
- type MapRule
- type MapRuleFunc
- type MapValidator
- func (mv MapValidator[M, K, V]) By(rules ...MapRule[M, K, V]) MapValidator[M, K, V]
- func (mv MapValidator[M, K, V]) Empty(condition bool) MapValidator[M, K, V]
- func (mv MapValidator[M, K, V]) Err() error
- func (mv MapValidator[M, K, V]) KeysBy(rules ...AnyRule[K]) MapValidator[M, K, V]
- func (mv MapValidator[M, K, V]) KeysWith(fns ...func(k K) error) MapValidator[M, K, V]
- func (mv MapValidator[M, K, V]) Length(min, max int) MapValidator[M, K, V]
- func (mv MapValidator[M, K, V]) Nil(condition bool) MapValidator[M, K, V]
- func (mv MapValidator[M, K, V]) NilOrNotEmpty(condition bool) MapValidator[M, K, V]
- func (mv MapValidator[M, K, V]) NotNil(condition bool) MapValidator[M, K, V]
- func (mv MapValidator[M, K, V]) Required(condition bool) MapValidator[M, K, V]
- func (mv MapValidator[M, K, V]) Validate(m M) error
- func (mv MapValidator[M, K, V]) ValuesBy(rules ...AnyRule[V]) MapValidator[M, K, V]
- func (mv MapValidator[M, K, V]) ValuesWith(fns ...func(v V) error) MapValidator[M, K, V]
- func (mv MapValidator[M, K, V]) With(fns ...func(s M) error) MapValidator[M, K, V]
- type NumberRule
- type NumberRuleFunc
- type NumberValidator
- func (nv NumberValidator[N]) Between(a, b N) NumberValidator[N]
- func (nv NumberValidator[N]) BetweenEqual(a, b N) NumberValidator[N]
- func (nv NumberValidator[N]) By(rules ...NumberRule[N]) NumberValidator[N]
- func (nv NumberValidator[N]) Equal(v N) NumberValidator[N]
- func (nv NumberValidator[N]) Err() error
- func (nv NumberValidator[N]) Greater(v N) NumberValidator[N]
- func (nv NumberValidator[N]) GreaterEqual(v N) NumberValidator[N]
- func (nv NumberValidator[N]) In(elements ...N) NumberValidator[N]
- func (nv NumberValidator[N]) Less(v N) NumberValidator[N]
- func (nv NumberValidator[N]) LessEqual(v N) NumberValidator[N]
- func (nv NumberValidator[N]) NotIn(elements ...N) NumberValidator[N]
- func (nv NumberValidator[N]) Required(condition bool) NumberValidator[N]
- func (nv NumberValidator[N]) Validate(v N) error
- func (nv NumberValidator[N]) With(fns ...func(n N) error) NumberValidator[N]
- type OrderedRule
- type OrderedRuleFunc
- func Between[T cmp.Ordered](a, b T) OrderedRuleFunc[T]
- func BetweenEqual[T cmp.Ordered](a, b T) OrderedRuleFunc[T]
- func Greater[T cmp.Ordered](b T) OrderedRuleFunc[T]
- func GreaterEqual[T cmp.Ordered](b T) OrderedRuleFunc[T]
- func Less[T cmp.Ordered](b T) OrderedRuleFunc[T]
- func LessEqual[T cmp.Ordered](b T) OrderedRuleFunc[T]
- type PtrRule
- type PtrRuleFunc
- type PtrValidator
- func (pv PtrValidator[T]) By(rules ...PtrRule[T]) PtrValidator[T]
- func (pv PtrValidator[T]) Err() error
- func (pv PtrValidator[T]) Nil(condition bool) PtrValidator[T]
- func (pv PtrValidator[T]) NotNil(condition bool) PtrValidator[T]
- func (pv PtrValidator[T]) Validate(v *T) error
- func (pv PtrValidator[T]) ValueBy(rules ...AnyRule[T]) PtrValidator[T]
- func (pv PtrValidator[T]) ValueWith(fns ...func(p T) error) PtrValidator[T]
- func (pv PtrValidator[T]) With(fns ...func(p *T) error) PtrValidator[T]
- type RuleError
- type SliceRule
- type SliceRuleFunc
- type SliceValidator
- func (sv SliceValidator[S, T]) By(rules ...SliceRule[S, T]) SliceValidator[S, T]
- func (sv SliceValidator[S, T]) Empty(condition bool) SliceValidator[S, T]
- func (sv SliceValidator[S, T]) Err() error
- func (sv SliceValidator[S, T]) Length(min, max int) SliceValidator[S, T]
- func (sv SliceValidator[S, T]) Nil(condition bool) SliceValidator[S, T]
- func (sv SliceValidator[S, T]) NilOrNotEmpty(condition bool) SliceValidator[S, T]
- func (sv SliceValidator[S, T]) NotNil(condition bool) SliceValidator[S, T]
- func (sv SliceValidator[S, T]) Required(condition bool) SliceValidator[S, T]
- func (sv SliceValidator[S, T]) Validate(v []T) error
- func (sv SliceValidator[S, T]) ValuesBy(rules ...AnyRule[T]) SliceValidator[S, T]
- func (sv SliceValidator[S, T]) ValuesPtrBy(rules ...AnyRule[*T]) SliceValidator[S, T]
- func (sv SliceValidator[S, T]) ValuesPtrWith(fns ...func(v *T) error) SliceValidator[S, T]
- func (sv SliceValidator[S, T]) ValuesWith(fns ...func(v T) error) SliceValidator[S, T]
- func (sv SliceValidator[S, T]) With(fns ...func(s S) error) SliceValidator[S, T]
- type StringRule
- type StringRuleFunc
- type StringValidator
- func (sv StringValidator[S]) Between(a, b S) StringValidator[S]
- func (sv StringValidator[S]) BetweenEqual(a, b S) StringValidator[S]
- func (sv StringValidator[S]) By(rules ...StringRule[S]) StringValidator[S]
- func (sv StringValidator[S]) Equal(v S) StringValidator[S]
- func (sv StringValidator[S]) Err() error
- func (sv StringValidator[S]) Greater(v S) StringValidator[S]
- func (sv StringValidator[S]) GreaterEqual(v S) StringValidator[S]
- func (sv StringValidator[S]) In(elements ...S) StringValidator[S]
- func (sv StringValidator[S]) Length(min, max int) StringValidator[S]
- func (sv StringValidator[S]) Less(v S) StringValidator[S]
- func (sv StringValidator[S]) LessEqual(v S) StringValidator[S]
- func (sv StringValidator[S]) Match(expr string) StringValidator[S]
- func (sv StringValidator[S]) NotIn(elements ...S) StringValidator[S]
- func (sv StringValidator[S]) NotMatch(expr string) StringValidator[S]
- func (sv StringValidator[S]) Required(condition bool) StringValidator[S]
- func (sv StringValidator[S]) Validate(v S) error
- func (sv StringValidator[S]) With(fns ...func(s S) error) StringValidator[S]
- type TimeRule
- type TimeRuleFunc
- func BetweenEqualTime(a, b time.Time) TimeRuleFunc
- func BetweenTime(a, b time.Time) TimeRuleFunc
- func EqualTime(v time.Time) TimeRuleFunc
- func GreaterEqualTime(v time.Time) TimeRuleFunc
- func GreaterTime(v time.Time) TimeRuleFunc
- func InTime(elements ...time.Time) TimeRuleFunc
- func LessEqualTime(v time.Time) TimeRuleFunc
- func LessTime(v time.Time) TimeRuleFunc
- func NotInTime(elements ...time.Time) TimeRuleFunc
- type TimeValidator
- func (tv TimeValidator) Between(a, b time.Time) TimeValidator
- func (tv TimeValidator) BetweenEqual(a, b time.Time) TimeValidator
- func (tv TimeValidator) By(rules ...TimeRule) TimeValidator
- func (tv TimeValidator) Equal(v time.Time) TimeValidator
- func (tv TimeValidator) Err() error
- func (tv TimeValidator) Greater(v time.Time) TimeValidator
- func (tv TimeValidator) GreaterEqual(v time.Time) TimeValidator
- func (tv TimeValidator) In(elements ...time.Time) TimeValidator
- func (tv TimeValidator) Less(v time.Time) TimeValidator
- func (tv TimeValidator) LessEqual(v time.Time) TimeValidator
- func (tv TimeValidator) NotIn(elements ...time.Time) TimeValidator
- func (tv TimeValidator) Required(condition bool) TimeValidator
- func (tv TimeValidator) Validate(v time.Time) error
- func (tv TimeValidator) With(fns ...func(v time.Time) error) TimeValidator
- type Validatable
- type Validator
- type ValueError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrNotEmpty = NewRuleError("not_empty", "must be blank") ErrNotNil = NewRuleError("not_nil", "must be blank") )
View Source
var ( ErrMatch = NewRuleError("match", "must be a valid value") ErrNoMatch = NewRuleError("no_match", "must be a valid value") )
View Source
var ErrEmpty = NewRuleError("empty", "cannot be blank")
View Source
var ErrIn = NewRuleError("in", "must be a valid value")
View Source
var ErrNil = NewRuleError("nil", "is required")
View Source
var ErrNotIn = NewRuleError("not_in", "must be a valid value")
Functions ¶
func Custom ¶
func Custom[T Validatable](v T) error
func Empty ¶
func Empty[T comparable](v T) error
func EmptyMap ¶
func EmptyMap[M ~map[K]V, K comparable, V any](m M) error
func EmptySlice ¶
func NilMap ¶
func NilMap[M ~map[K]V, K comparable, V any](m M) error
func NilOrNotEmptyMap ¶
func NilOrNotEmptyMap[M ~map[K]V, K comparable, V any](m M) error
func NilOrNotEmptySlice ¶
func NotNilMap ¶
func NotNilMap[M ~map[K]V, K comparable, V any](m M) error
func NotNilSlice ¶
func Required ¶
func Required[T comparable](v T) error
func RequiredMap ¶
func RequiredMap[M ~map[K]V, K comparable, V any](m M) error
func RequiredSlice ¶
func RequiredTime ¶
Types ¶
type AnyRuleFunc ¶
func BetweenAny ¶
func BetweenAny[T any](cmp func(a, b T) int, a, b T) AnyRuleFunc[T]
func BetweenEqualAny ¶
func BetweenEqualAny[T any](cmp func(a, b T) int, a, b T) AnyRuleFunc[T]
func CustomRule ¶
func CustomRule[T Validatable]() AnyRuleFunc[T]
func EqualAny ¶
func EqualAny[T any](eq func(a, b T) bool, b T) AnyRuleFunc[T]
func GreaterAny ¶
func GreaterAny[T any](cmp func(a, b T) int, b T) AnyRuleFunc[T]
func GreaterEqualAny ¶
func GreaterEqualAny[T any](cmp func(a, b T) int, b T) AnyRuleFunc[T]
func InAny ¶
func InAny[T any](eq func(a, b T) bool, elements ...T) AnyRuleFunc[T]
func LessAny ¶
func LessAny[T any](cmp func(a, b T) int, b T) AnyRuleFunc[T]
func LessEqualAny ¶
func LessEqualAny[T any](cmp func(a, b T) int, b T) AnyRuleFunc[T]
func NotInAny ¶
func NotInAny[T any](eq func(a, b T) bool, elements ...T) AnyRuleFunc[T]
func RequiredAny ¶
func RequiredAny[T any](isZero func(a T) bool) AnyRuleFunc[T]
func (AnyRuleFunc[T]) Validate ¶
func (fn AnyRuleFunc[T]) Validate(v T) error
type AnyValidator ¶
type AnyValidator[T any] struct { // contains filtered or unexported fields }
func Any ¶
func Any[T any]() AnyValidator[T]
func AnyOf ¶ added in v0.2.0
func AnyOf[T any](v T, name string) AnyValidator[T]
func (AnyValidator[T]) Between ¶
func (av AnyValidator[T]) Between(cmp func(a, b T) int, a, b T) AnyValidator[T]
func (AnyValidator[T]) BetweenEqual ¶
func (av AnyValidator[T]) BetweenEqual(cmp func(a, b T) int, a, b T) AnyValidator[T]
func (AnyValidator[T]) By ¶
func (av AnyValidator[T]) By(rules ...AnyRule[T]) AnyValidator[T]
func (AnyValidator[T]) Equal ¶
func (av AnyValidator[T]) Equal(eq func(a, b T) bool, v T) AnyValidator[T]
func (AnyValidator[T]) Err ¶ added in v0.5.0
func (av AnyValidator[T]) Err() error
func (AnyValidator[T]) Greater ¶
func (av AnyValidator[T]) Greater(cmp func(a, b T) int, v T) AnyValidator[T]
func (AnyValidator[T]) GreaterEqual ¶
func (av AnyValidator[T]) GreaterEqual(cmp func(a, b T) int, v T) AnyValidator[T]
func (AnyValidator[T]) In ¶
func (av AnyValidator[T]) In(eq func(a, b T) bool, elements ...T) AnyValidator[T]
func (AnyValidator[T]) Less ¶
func (av AnyValidator[T]) Less(cmp func(a, b T) int, v T) AnyValidator[T]
func (AnyValidator[T]) LessEqual ¶
func (av AnyValidator[T]) LessEqual(cmp func(a, b T) int, v T) AnyValidator[T]
func (AnyValidator[T]) NotIn ¶
func (av AnyValidator[T]) NotIn(eq func(a, b T) bool, elements ...T) AnyValidator[T]
func (AnyValidator[T]) Required ¶
func (av AnyValidator[T]) Required(condition bool, isDefault func(v T) bool) AnyValidator[T]
func (AnyValidator[T]) Validate ¶
func (av AnyValidator[T]) Validate(v T) error
func (AnyValidator[T]) With ¶
func (av AnyValidator[T]) With(fns ...func(v T) error) AnyValidator[T]
type ComparableRule ¶
type ComparableRule[T comparable] interface { Validate(v T) error }
type ComparableRuleFunc ¶
type ComparableRuleFunc[T comparable] func(v T) error
func Equal ¶
func Equal[T comparable](b T) ComparableRuleFunc[T]
func In ¶
func In[T comparable](elements ...T) ComparableRuleFunc[T]
func NotIn ¶
func NotIn[T comparable](elements ...T) ComparableRuleFunc[T]
func (ComparableRuleFunc[T]) Validate ¶
func (fn ComparableRuleFunc[T]) Validate(v T) error
type ComparableValidator ¶
type ComparableValidator[T comparable] struct { // contains filtered or unexported fields }
func Comparable ¶
func Comparable[T comparable]() ComparableValidator[T]
func ComparableOf ¶ added in v0.2.0
func ComparableOf[T comparable](v T, name string) ComparableValidator[T]
func (ComparableValidator[T]) By ¶
func (cv ComparableValidator[T]) By(rules ...ComparableRule[T]) ComparableValidator[T]
func (ComparableValidator[T]) Equal ¶
func (cv ComparableValidator[T]) Equal(v T) ComparableValidator[T]
func (ComparableValidator[T]) Err ¶ added in v0.5.0
func (cv ComparableValidator[T]) Err() error
func (ComparableValidator[T]) In ¶
func (cv ComparableValidator[T]) In(elements ...T) ComparableValidator[T]
func (ComparableValidator[T]) NotIn ¶
func (cv ComparableValidator[T]) NotIn(elements ...T) ComparableValidator[T]
func (ComparableValidator[T]) Required ¶
func (cv ComparableValidator[T]) Required(condition bool) ComparableValidator[T]
func (ComparableValidator[T]) Validate ¶
func (cv ComparableValidator[T]) Validate(v T) error
func (ComparableValidator[T]) With ¶
func (cv ComparableValidator[T]) With(fns ...func(v T) error) ComparableValidator[T]
type IndexError ¶
func NewIndexError ¶
func NewIndexError(index int, nested error) IndexError
type MapRule ¶
type MapRule[M ~map[K]V, K comparable, V any] interface { Validate(m M) error }
type MapRuleFunc ¶
type MapRuleFunc[M ~map[K]V, K comparable, V any] func(m M) error
func LengthMap ¶
func LengthMap[M ~map[K]V, K comparable, V any](min, max int) MapRuleFunc[M, K, V]
func (MapRuleFunc[M, K, V]) Validate ¶
func (fn MapRuleFunc[M, K, V]) Validate(m M) error
type MapValidator ¶
type MapValidator[M ~map[K]V, K comparable, V any] struct { // contains filtered or unexported fields }
func Map ¶
func Map[M ~map[K]V, K comparable, V any]() MapValidator[M, K, V]
func MapOf ¶ added in v0.2.0
func MapOf[M ~map[K]V, K comparable, V any](m M, name string) MapValidator[M, K, V]
func (MapValidator[M, K, V]) By ¶
func (mv MapValidator[M, K, V]) By(rules ...MapRule[M, K, V]) MapValidator[M, K, V]
func (MapValidator[M, K, V]) Empty ¶
func (mv MapValidator[M, K, V]) Empty(condition bool) MapValidator[M, K, V]
func (MapValidator[M, K, V]) Err ¶ added in v0.5.0
func (mv MapValidator[M, K, V]) Err() error
func (MapValidator[M, K, V]) KeysBy ¶ added in v0.2.0
func (mv MapValidator[M, K, V]) KeysBy(rules ...AnyRule[K]) MapValidator[M, K, V]
func (MapValidator[M, K, V]) KeysWith ¶ added in v0.2.0
func (mv MapValidator[M, K, V]) KeysWith(fns ...func(k K) error) MapValidator[M, K, V]
func (MapValidator[M, K, V]) Length ¶
func (mv MapValidator[M, K, V]) Length(min, max int) MapValidator[M, K, V]
func (MapValidator[M, K, V]) Nil ¶
func (mv MapValidator[M, K, V]) Nil(condition bool) MapValidator[M, K, V]
func (MapValidator[M, K, V]) NilOrNotEmpty ¶
func (mv MapValidator[M, K, V]) NilOrNotEmpty(condition bool) MapValidator[M, K, V]
func (MapValidator[M, K, V]) NotNil ¶
func (mv MapValidator[M, K, V]) NotNil(condition bool) MapValidator[M, K, V]
func (MapValidator[M, K, V]) Required ¶
func (mv MapValidator[M, K, V]) Required(condition bool) MapValidator[M, K, V]
func (MapValidator[M, K, V]) Validate ¶
func (mv MapValidator[M, K, V]) Validate(m M) error
func (MapValidator[M, K, V]) ValuesBy ¶ added in v0.2.0
func (mv MapValidator[M, K, V]) ValuesBy(rules ...AnyRule[V]) MapValidator[M, K, V]
func (MapValidator[M, K, V]) ValuesWith ¶ added in v0.2.0
func (mv MapValidator[M, K, V]) ValuesWith(fns ...func(v V) error) MapValidator[M, K, V]
func (MapValidator[M, K, V]) With ¶
func (mv MapValidator[M, K, V]) With(fns ...func(s M) error) MapValidator[M, K, V]
type NumberRule ¶
type NumberRule[T constraints.Integer | constraints.Float] interface { Validate(n T) error }
type NumberRuleFunc ¶
type NumberRuleFunc[T constraints.Integer | constraints.Float] func(n T) error
func (NumberRuleFunc[T]) Validate ¶
func (fn NumberRuleFunc[T]) Validate(n T) error
type NumberValidator ¶
type NumberValidator[N constraints.Integer | constraints.Float] struct { // contains filtered or unexported fields }
func Number ¶
func Number[N constraints.Integer | constraints.Float]() NumberValidator[N]
func NumberOf ¶ added in v0.2.0
func NumberOf[N constraints.Integer | constraints.Float](n N, name string) NumberValidator[N]
func (NumberValidator[N]) Between ¶
func (nv NumberValidator[N]) Between(a, b N) NumberValidator[N]
func (NumberValidator[N]) BetweenEqual ¶
func (nv NumberValidator[N]) BetweenEqual(a, b N) NumberValidator[N]
func (NumberValidator[N]) By ¶
func (nv NumberValidator[N]) By(rules ...NumberRule[N]) NumberValidator[N]
func (NumberValidator[N]) Equal ¶
func (nv NumberValidator[N]) Equal(v N) NumberValidator[N]
func (NumberValidator[N]) Err ¶ added in v0.5.0
func (nv NumberValidator[N]) Err() error
func (NumberValidator[N]) Greater ¶
func (nv NumberValidator[N]) Greater(v N) NumberValidator[N]
func (NumberValidator[N]) GreaterEqual ¶
func (nv NumberValidator[N]) GreaterEqual(v N) NumberValidator[N]
func (NumberValidator[N]) In ¶
func (nv NumberValidator[N]) In(elements ...N) NumberValidator[N]
func (NumberValidator[N]) Less ¶
func (nv NumberValidator[N]) Less(v N) NumberValidator[N]
func (NumberValidator[N]) LessEqual ¶
func (nv NumberValidator[N]) LessEqual(v N) NumberValidator[N]
func (NumberValidator[N]) NotIn ¶
func (nv NumberValidator[N]) NotIn(elements ...N) NumberValidator[N]
func (NumberValidator[N]) Required ¶
func (nv NumberValidator[N]) Required(condition bool) NumberValidator[N]
func (NumberValidator[N]) Validate ¶
func (nv NumberValidator[N]) Validate(v N) error
func (NumberValidator[N]) With ¶
func (nv NumberValidator[N]) With(fns ...func(n N) error) NumberValidator[N]
type OrderedRule ¶ added in v0.3.0
type OrderedRuleFunc ¶ added in v0.3.0
func Between ¶
func Between[T cmp.Ordered](a, b T) OrderedRuleFunc[T]
func BetweenEqual ¶
func BetweenEqual[T cmp.Ordered](a, b T) OrderedRuleFunc[T]
func Greater ¶
func Greater[T cmp.Ordered](b T) OrderedRuleFunc[T]
func GreaterEqual ¶
func GreaterEqual[T cmp.Ordered](b T) OrderedRuleFunc[T]
func Less ¶
func Less[T cmp.Ordered](b T) OrderedRuleFunc[T]
func LessEqual ¶
func LessEqual[T cmp.Ordered](b T) OrderedRuleFunc[T]
func (OrderedRuleFunc[T]) Validate ¶ added in v0.3.0
func (fn OrderedRuleFunc[T]) Validate(v T) error
type PtrRuleFunc ¶
func (PtrRuleFunc[T]) Validate ¶
func (fn PtrRuleFunc[T]) Validate(p *T) error
type PtrValidator ¶
type PtrValidator[T any] struct { // contains filtered or unexported fields }
func Ptr ¶
func Ptr[T any]() PtrValidator[T]
func PtrOf ¶ added in v0.2.0
func PtrOf[T any](p *T, name string) PtrValidator[T]
func (PtrValidator[T]) By ¶
func (pv PtrValidator[T]) By(rules ...PtrRule[T]) PtrValidator[T]
func (PtrValidator[T]) Err ¶ added in v0.5.0
func (pv PtrValidator[T]) Err() error
func (PtrValidator[T]) Nil ¶
func (pv PtrValidator[T]) Nil(condition bool) PtrValidator[T]
func (PtrValidator[T]) NotNil ¶
func (pv PtrValidator[T]) NotNil(condition bool) PtrValidator[T]
func (PtrValidator[T]) Validate ¶
func (pv PtrValidator[T]) Validate(v *T) error
func (PtrValidator[T]) ValueBy ¶
func (pv PtrValidator[T]) ValueBy(rules ...AnyRule[T]) PtrValidator[T]
func (PtrValidator[T]) ValueWith ¶
func (pv PtrValidator[T]) ValueWith(fns ...func(p T) error) PtrValidator[T]
func (PtrValidator[T]) With ¶
func (pv PtrValidator[T]) With(fns ...func(p *T) error) PtrValidator[T]
type RuleError ¶
func NewRuleError ¶
type SliceRuleFunc ¶
func LengthSlice ¶
func LengthSlice[S ~[]T, T any](min, max int) SliceRuleFunc[S, T]
func (SliceRuleFunc[S, T]) Validate ¶
func (fn SliceRuleFunc[S, T]) Validate(s S) error
type SliceValidator ¶
type SliceValidator[S ~[]T, T any] struct { // contains filtered or unexported fields }
func Slice ¶
func Slice[S ~[]T, T any]() SliceValidator[S, T]
func SliceOf ¶ added in v0.2.0
func SliceOf[S ~[]T, T any](s S, name string) SliceValidator[S, T]
func (SliceValidator[S, T]) By ¶
func (sv SliceValidator[S, T]) By(rules ...SliceRule[S, T]) SliceValidator[S, T]
func (SliceValidator[S, T]) Empty ¶
func (sv SliceValidator[S, T]) Empty(condition bool) SliceValidator[S, T]
func (SliceValidator[S, T]) Err ¶ added in v0.5.0
func (sv SliceValidator[S, T]) Err() error
func (SliceValidator[S, T]) Length ¶
func (sv SliceValidator[S, T]) Length(min, max int) SliceValidator[S, T]
func (SliceValidator[S, T]) Nil ¶
func (sv SliceValidator[S, T]) Nil(condition bool) SliceValidator[S, T]
func (SliceValidator[S, T]) NilOrNotEmpty ¶
func (sv SliceValidator[S, T]) NilOrNotEmpty(condition bool) SliceValidator[S, T]
func (SliceValidator[S, T]) NotNil ¶
func (sv SliceValidator[S, T]) NotNil(condition bool) SliceValidator[S, T]
func (SliceValidator[S, T]) Required ¶
func (sv SliceValidator[S, T]) Required(condition bool) SliceValidator[S, T]
func (SliceValidator[S, T]) Validate ¶
func (sv SliceValidator[S, T]) Validate(v []T) error
func (SliceValidator[S, T]) ValuesBy ¶
func (sv SliceValidator[S, T]) ValuesBy(rules ...AnyRule[T]) SliceValidator[S, T]
func (SliceValidator[S, T]) ValuesPtrBy ¶
func (sv SliceValidator[S, T]) ValuesPtrBy(rules ...AnyRule[*T]) SliceValidator[S, T]
func (SliceValidator[S, T]) ValuesPtrWith ¶
func (sv SliceValidator[S, T]) ValuesPtrWith(fns ...func(v *T) error) SliceValidator[S, T]
func (SliceValidator[S, T]) ValuesWith ¶
func (sv SliceValidator[S, T]) ValuesWith(fns ...func(v T) error) SliceValidator[S, T]
func (SliceValidator[S, T]) With ¶
func (sv SliceValidator[S, T]) With(fns ...func(s S) error) SliceValidator[S, T]
type StringRule ¶
type StringRuleFunc ¶
func LengthString ¶
func LengthString[S ~string](min, max int) StringRuleFunc[S]
func LengthStringRune ¶
func LengthStringRune[S ~string](min, max int) StringRuleFunc[S]
func Match ¶
func Match[S ~string](expr string) StringRuleFunc[S]
func NotMatch ¶
func NotMatch[S ~string](expr string) StringRuleFunc[S]
func (StringRuleFunc[T]) Validate ¶
func (fn StringRuleFunc[T]) Validate(s T) error
type StringValidator ¶
type StringValidator[S ~string] struct { // contains filtered or unexported fields }
func String ¶
func String[S ~string]() StringValidator[S]
func StringOf ¶ added in v0.2.0
func StringOf[S ~string](s S, name string) StringValidator[S]
func (StringValidator[S]) Between ¶
func (sv StringValidator[S]) Between(a, b S) StringValidator[S]
func (StringValidator[S]) BetweenEqual ¶
func (sv StringValidator[S]) BetweenEqual(a, b S) StringValidator[S]
func (StringValidator[S]) By ¶
func (sv StringValidator[S]) By(rules ...StringRule[S]) StringValidator[S]
func (StringValidator[S]) Equal ¶
func (sv StringValidator[S]) Equal(v S) StringValidator[S]
func (StringValidator[S]) Err ¶ added in v0.5.0
func (sv StringValidator[S]) Err() error
func (StringValidator[S]) Greater ¶
func (sv StringValidator[S]) Greater(v S) StringValidator[S]
func (StringValidator[S]) GreaterEqual ¶
func (sv StringValidator[S]) GreaterEqual(v S) StringValidator[S]
func (StringValidator[S]) In ¶
func (sv StringValidator[S]) In(elements ...S) StringValidator[S]
func (StringValidator[S]) Length ¶
func (sv StringValidator[S]) Length(min, max int) StringValidator[S]
func (StringValidator[S]) Less ¶
func (sv StringValidator[S]) Less(v S) StringValidator[S]
func (StringValidator[S]) LessEqual ¶
func (sv StringValidator[S]) LessEqual(v S) StringValidator[S]
func (StringValidator[S]) Match ¶
func (sv StringValidator[S]) Match(expr string) StringValidator[S]
func (StringValidator[S]) NotIn ¶
func (sv StringValidator[S]) NotIn(elements ...S) StringValidator[S]
func (StringValidator[S]) NotMatch ¶
func (sv StringValidator[S]) NotMatch(expr string) StringValidator[S]
func (StringValidator[S]) Required ¶
func (sv StringValidator[S]) Required(condition bool) StringValidator[S]
func (StringValidator[S]) Validate ¶
func (sv StringValidator[S]) Validate(v S) error
func (StringValidator[S]) With ¶
func (sv StringValidator[S]) With(fns ...func(s S) error) StringValidator[S]
type TimeRuleFunc ¶
func BetweenEqualTime ¶
func BetweenEqualTime(a, b time.Time) TimeRuleFunc
func BetweenTime ¶
func BetweenTime(a, b time.Time) TimeRuleFunc
func EqualTime ¶
func EqualTime(v time.Time) TimeRuleFunc
func GreaterEqualTime ¶
func GreaterEqualTime(v time.Time) TimeRuleFunc
func GreaterTime ¶
func GreaterTime(v time.Time) TimeRuleFunc
func InTime ¶
func InTime(elements ...time.Time) TimeRuleFunc
func LessEqualTime ¶
func LessEqualTime(v time.Time) TimeRuleFunc
func LessTime ¶
func LessTime(v time.Time) TimeRuleFunc
func NotInTime ¶
func NotInTime(elements ...time.Time) TimeRuleFunc
type TimeValidator ¶
type TimeValidator struct {
// contains filtered or unexported fields
}
func Time ¶
func Time() TimeValidator
func (TimeValidator) Between ¶
func (tv TimeValidator) Between(a, b time.Time) TimeValidator
func (TimeValidator) BetweenEqual ¶
func (tv TimeValidator) BetweenEqual(a, b time.Time) TimeValidator
func (TimeValidator) By ¶
func (tv TimeValidator) By(rules ...TimeRule) TimeValidator
func (TimeValidator) Equal ¶
func (tv TimeValidator) Equal(v time.Time) TimeValidator
func (TimeValidator) Err ¶ added in v0.5.0
func (tv TimeValidator) Err() error
func (TimeValidator) Greater ¶
func (tv TimeValidator) Greater(v time.Time) TimeValidator
func (TimeValidator) GreaterEqual ¶
func (tv TimeValidator) GreaterEqual(v time.Time) TimeValidator
func (TimeValidator) In ¶
func (tv TimeValidator) In(elements ...time.Time) TimeValidator
func (TimeValidator) Less ¶
func (tv TimeValidator) Less(v time.Time) TimeValidator
func (TimeValidator) LessEqual ¶
func (tv TimeValidator) LessEqual(v time.Time) TimeValidator
func (TimeValidator) NotIn ¶
func (tv TimeValidator) NotIn(elements ...time.Time) TimeValidator
func (TimeValidator) Required ¶
func (tv TimeValidator) Required(condition bool) TimeValidator
func (TimeValidator) With ¶
func (tv TimeValidator) With(fns ...func(v time.Time) error) TimeValidator
type Validatable ¶
type Validatable interface {
Validate() error
}
type ValueError ¶
func NewValueError ¶
func NewValueError(name string, nested error) ValueError
Source Files
¶
Click to show internal directories.
Click to hide internal directories.