Documentation ¶
Index ¶
- Variables
- func NewErrors(m map[string]error) error
- func Value[T any](v *T) (t T)
- type Errors
- type FuncMap
- type NumberValidator
- func (nv *NumberValidator[T]) Between(lo, hi T) *NumberValidator[T]
- func (nv *NumberValidator[T]) Func(fn func(T) error) *NumberValidator[T]
- func (nv *NumberValidator[T]) FuncMap(fm FuncMap[T]) *NumberValidator[T]
- func (nv *NumberValidator[T]) Funcs(fns ...func(T) error) *NumberValidator[T]
- func (nv *NumberValidator[T]) Is(n T) *NumberValidator[T]
- func (nv *NumberValidator[T]) Latitude() *NumberValidator[T]
- func (nv *NumberValidator[T]) Longitude() *NumberValidator[T]
- func (nv *NumberValidator[T]) Max(n T) *NumberValidator[T]
- func (nv *NumberValidator[T]) Min(n T) *NumberValidator[T]
- func (nv *NumberValidator[T]) Negative() *NumberValidator[T]
- func (nv *NumberValidator[T]) OneOf(ns ...T) *NumberValidator[T]
- func (nv *NumberValidator[T]) Optional() *NumberValidator[T]
- func (nv *NumberValidator[T]) Parse(exprs string) *NumberValidator[T]
- func (nv *NumberValidator[T]) Positive() *NumberValidator[T]
- func (nv *NumberValidator[T]) Validate(v T) error
- type Numeric
- type ParserFunc
- type SliceValidator
- func (sv *SliceValidator[T]) Each(fn Validator[T]) *SliceValidator[T]
- func (sv *SliceValidator[T]) EachFunc(fn func(T) error) *SliceValidator[T]
- func (sv *SliceValidator[T]) Func(fn func([]T) error) *SliceValidator[T]
- func (sv *SliceValidator[T]) FuncMap(fm FuncMap[[]T]) *SliceValidator[T]
- func (sv *SliceValidator[T]) Funcs(fns ...func([]T) error) *SliceValidator[T]
- func (sv *SliceValidator[T]) Len(n int) *SliceValidator[T]
- func (sv *SliceValidator[T]) Max(n int) *SliceValidator[T]
- func (sv *SliceValidator[T]) Min(n int) *SliceValidator[T]
- func (sv *SliceValidator[T]) Optional() *SliceValidator[T]
- func (sv *SliceValidator[T]) Parse(exprs string) *SliceValidator[T]
- func (sv *SliceValidator[T]) Validate(vs []T) error
- type StringValidator
- func (sv *StringValidator) Alpha() *StringValidator
- func (sv *StringValidator) AlphaNumeric() *StringValidator
- func (sv *StringValidator) Email() *StringValidator
- func (sv *StringValidator) EndsWith(suffix string) *StringValidator
- func (sv *StringValidator) Func(fn func(string) error) *StringValidator
- func (sv *StringValidator) FuncMap(fm FuncMap[string]) *StringValidator
- func (sv *StringValidator) Funcs(fns ...func(string) error) *StringValidator
- func (sv *StringValidator) IP() *StringValidator
- func (sv *StringValidator) Is(str string) *StringValidator
- func (sv *StringValidator) Len(n int) *StringValidator
- func (sv *StringValidator) Max(n int) *StringValidator
- func (sv *StringValidator) Min(n int) *StringValidator
- func (sv *StringValidator) Numeric() *StringValidator
- func (sv *StringValidator) OneOf(vals ...string) *StringValidator
- func (sv *StringValidator) Optional() *StringValidator
- func (sv *StringValidator) Parse(exprs string) *StringValidator
- func (sv *StringValidator) Regexp(name string, pattern string) *StringValidator
- func (sv *StringValidator) StartsWith(prefix string) *StringValidator
- func (sv *StringValidator) URL() *StringValidator
- func (sv *StringValidator) Validate(s string) error
- type Validator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrSkip = errors.New("skip")
Functions ¶
Types ¶
type FuncMap ¶
type FuncMap[T any] map[string]ParserFunc[T]
type NumberValidator ¶
type NumberValidator[T Numeric] struct { // contains filtered or unexported fields }
func Number ¶
func Number[T Numeric]() *NumberValidator[T]
func NumberExpr ¶
func NumberExpr[T Numeric](expr string, fms ...FuncMap[T]) *NumberValidator[T]
Example ¶
package main import ( "fmt" "github.com/alextanhongpin/core/validator" ) var ageField = validator.NumberExpr[int]("min=13") type User struct { Age int } func (u *User) Valid() error { return validator.NewErrors(map[string]error{ "age": ageField.Validate(u.Age), }) } func main() { zero := &User{Age: 0} minor := &User{Age: 12} adult := &User{Age: 13} fmt.Printf("%v => %v\n", zero.Age, zero.Valid()) fmt.Printf("%v => %v\n", minor.Age, minor.Valid()) fmt.Printf("%v => %v\n", adult.Age, adult.Valid()) }
Output: 0 => age: must not be zero 12 => age: min 13 13 => <nil>
func (*NumberValidator[T]) Between ¶
func (nv *NumberValidator[T]) Between(lo, hi T) *NumberValidator[T]
func (*NumberValidator[T]) Func ¶
func (nv *NumberValidator[T]) Func(fn func(T) error) *NumberValidator[T]
func (*NumberValidator[T]) FuncMap ¶
func (nv *NumberValidator[T]) FuncMap(fm FuncMap[T]) *NumberValidator[T]
func (*NumberValidator[T]) Funcs ¶
func (nv *NumberValidator[T]) Funcs(fns ...func(T) error) *NumberValidator[T]
func (*NumberValidator[T]) Is ¶
func (nv *NumberValidator[T]) Is(n T) *NumberValidator[T]
func (*NumberValidator[T]) Latitude ¶
func (nv *NumberValidator[T]) Latitude() *NumberValidator[T]
func (*NumberValidator[T]) Longitude ¶
func (nv *NumberValidator[T]) Longitude() *NumberValidator[T]
func (*NumberValidator[T]) Max ¶
func (nv *NumberValidator[T]) Max(n T) *NumberValidator[T]
func (*NumberValidator[T]) Min ¶
func (nv *NumberValidator[T]) Min(n T) *NumberValidator[T]
func (*NumberValidator[T]) Negative ¶
func (nv *NumberValidator[T]) Negative() *NumberValidator[T]
func (*NumberValidator[T]) OneOf ¶
func (nv *NumberValidator[T]) OneOf(ns ...T) *NumberValidator[T]
func (*NumberValidator[T]) Optional ¶
func (nv *NumberValidator[T]) Optional() *NumberValidator[T]
func (*NumberValidator[T]) Parse ¶
func (nv *NumberValidator[T]) Parse(exprs string) *NumberValidator[T]
func (*NumberValidator[T]) Positive ¶
func (nv *NumberValidator[T]) Positive() *NumberValidator[T]
func (*NumberValidator[T]) Validate ¶
func (nv *NumberValidator[T]) Validate(v T) error
type Numeric ¶
type Numeric interface { constraints.Integer | constraints.Float }
type ParserFunc ¶
type SliceValidator ¶
type SliceValidator[T any] struct { // contains filtered or unexported fields }
func Slice ¶
func Slice[T any]() *SliceValidator[T]
func SliceExpr ¶
func SliceExpr[T any](expr string, fms ...FuncMap[[]T]) *SliceValidator[T]
Example ¶
package main import ( "fmt" "github.com/alextanhongpin/core/validator" ) var ( urlField = validator.StringExpr("url,min=3") linksField = validator.SliceExpr[Link]("min=2").EachFunc((Link).Valid) ) type Link struct { URL string } func (l Link) Valid() error { return validator.NewErrors(map[string]error{ "url": urlField.Validate(l.URL), }) } type Page struct { Links []Link } func (p *Page) Valid() error { return validator.NewErrors(map[string]error{ "links": linksField.Validate(p.Links), }) } func main() { invalid := &Page{Links: []Link{ {"http://localhost 8080"}, {"456"}, }} less := &Page{Links: []Link{ {"http://localhost"}, }} valid := &Page{Links: []Link{ {"http://localhost"}, {"http://localhost:8080"}, }} fmt.Printf("%v => %v\n", invalid.Links, invalid.Valid()) fmt.Printf("%v => %v\n", less.Links, less.Valid()) fmt.Printf("%v => %v\n", valid.Links, valid.Valid()) }
Output: [{http://localhost 8080} {456}] => links: url: invalid url [{http://localhost}] => links: min items is 2 [{http://localhost} {http://localhost:8080}] => <nil>
func (*SliceValidator[T]) Each ¶
func (sv *SliceValidator[T]) Each(fn Validator[T]) *SliceValidator[T]
func (*SliceValidator[T]) EachFunc ¶
func (sv *SliceValidator[T]) EachFunc(fn func(T) error) *SliceValidator[T]
func (*SliceValidator[T]) Func ¶
func (sv *SliceValidator[T]) Func(fn func([]T) error) *SliceValidator[T]
func (*SliceValidator[T]) FuncMap ¶
func (sv *SliceValidator[T]) FuncMap(fm FuncMap[[]T]) *SliceValidator[T]
func (*SliceValidator[T]) Funcs ¶
func (sv *SliceValidator[T]) Funcs(fns ...func([]T) error) *SliceValidator[T]
func (*SliceValidator[T]) Len ¶
func (sv *SliceValidator[T]) Len(n int) *SliceValidator[T]
func (*SliceValidator[T]) Max ¶
func (sv *SliceValidator[T]) Max(n int) *SliceValidator[T]
func (*SliceValidator[T]) Min ¶
func (sv *SliceValidator[T]) Min(n int) *SliceValidator[T]
func (*SliceValidator[T]) Optional ¶
func (sv *SliceValidator[T]) Optional() *SliceValidator[T]
func (*SliceValidator[T]) Parse ¶
func (sv *SliceValidator[T]) Parse(exprs string) *SliceValidator[T]
func (*SliceValidator[T]) Validate ¶
func (sv *SliceValidator[T]) Validate(vs []T) error
type StringValidator ¶
type StringValidator struct {
// contains filtered or unexported fields
}
func String ¶
func String() *StringValidator
func StringExpr ¶
func StringExpr(expr string, fms ...FuncMap[string]) *StringValidator
Example ¶
package main import ( "errors" "fmt" "github.com/alextanhongpin/core/validator" ) func adminOnly(email string) error { if email == "john.doe@mail.com" { return nil } return errors.New("admin only") } var ( emailField = validator.StringExpr("email,ends_with=@mail.com").Func(adminOnly) maritalStatusField = validator.StringExpr("optional,oneof=single married divorced") ) type Account struct { Email string MaritalStatus *string } func (a *Account) String() string { email := "n/a" status := "n/a" if a.Email != "" { email = a.Email } if a.MaritalStatus != nil { status = *a.MaritalStatus } return fmt.Sprintf("email: %s, marital_status: %s", email, status) } func (u *Account) Valid() error { return validator.NewErrors(map[string]error{ "email": emailField.Validate(u.Email), "marital_status": maritalStatusField.Validate(validator.Value(u.MaritalStatus)), }) } func main() { noemail := &Account{Email: ""} nonadmin := &Account{Email: "jane.doe@mail.com"} email := &Account{Email: "john.doe@mail.com"} unknownStatus := "unknown" unknown := &Account{Email: "john.doe@mail.com", MaritalStatus: &unknownStatus} invalidUnknown := &Account{Email: "jane.doe@mail.com", MaritalStatus: &unknownStatus} fmt.Printf("%s => %v\n", noemail, noemail.Valid()) fmt.Printf("%s => %v\n", nonadmin, nonadmin.Valid()) fmt.Printf("%s => %v\n", email, email.Valid()) fmt.Printf("%s => %v\n", unknown, unknown.Valid()) fmt.Printf("%s => %v\n", invalidUnknown, invalidUnknown.Valid()) var ve validator.Errors fmt.Println(errors.As(invalidUnknown.Valid(), &ve)) fmt.Println(ve["email"]) fmt.Println(ve["marital_status"]) }
Output: email: n/a, marital_status: n/a => email: must not be empty email: jane.doe@mail.com, marital_status: n/a => email: admin only email: john.doe@mail.com, marital_status: n/a => <nil> email: john.doe@mail.com, marital_status: unknown => marital_status: must be one of single, married, divorced email: jane.doe@mail.com, marital_status: unknown => email: admin only marital_status: must be one of single, married, divorced true admin only must be one of single, married, divorced
func (*StringValidator) Alpha ¶
func (sv *StringValidator) Alpha() *StringValidator
func (*StringValidator) AlphaNumeric ¶
func (sv *StringValidator) AlphaNumeric() *StringValidator
func (*StringValidator) Email ¶
func (sv *StringValidator) Email() *StringValidator
func (*StringValidator) EndsWith ¶
func (sv *StringValidator) EndsWith(suffix string) *StringValidator
func (*StringValidator) Func ¶
func (sv *StringValidator) Func(fn func(string) error) *StringValidator
func (*StringValidator) FuncMap ¶
func (sv *StringValidator) FuncMap(fm FuncMap[string]) *StringValidator
func (*StringValidator) Funcs ¶
func (sv *StringValidator) Funcs(fns ...func(string) error) *StringValidator
func (*StringValidator) IP ¶
func (sv *StringValidator) IP() *StringValidator
func (*StringValidator) Is ¶
func (sv *StringValidator) Is(str string) *StringValidator
func (*StringValidator) Len ¶
func (sv *StringValidator) Len(n int) *StringValidator
func (*StringValidator) Max ¶
func (sv *StringValidator) Max(n int) *StringValidator
func (*StringValidator) Min ¶
func (sv *StringValidator) Min(n int) *StringValidator
func (*StringValidator) Numeric ¶
func (sv *StringValidator) Numeric() *StringValidator
func (*StringValidator) OneOf ¶
func (sv *StringValidator) OneOf(vals ...string) *StringValidator
func (*StringValidator) Optional ¶
func (sv *StringValidator) Optional() *StringValidator
func (*StringValidator) Parse ¶
func (sv *StringValidator) Parse(exprs string) *StringValidator
func (*StringValidator) Regexp ¶
func (sv *StringValidator) Regexp(name string, pattern string) *StringValidator
func (*StringValidator) StartsWith ¶
func (sv *StringValidator) StartsWith(prefix string) *StringValidator
func (*StringValidator) URL ¶
func (sv *StringValidator) URL() *StringValidator
func (*StringValidator) Validate ¶
func (sv *StringValidator) Validate(s string) error
Click to show internal directories.
Click to hide internal directories.