Documentation
ΒΆ
Overview ΒΆ
Package kvalid is lightweight validation library, based on Go 1.18+ Generics.
Index ΒΆ
- Variables
- func IsEmail(email string) bool
- func IsURL(url string) bool
- type EmailValidator
- type Error
- type Errors
- type FieldFuncValidator
- type IgnoreValidator
- type MaxNullIntValidator
- func (p *MaxNullIntValidator) HTMLCompatible() bool
- func (p *MaxNullIntValidator) MarshalJSON() ([]byte, error)
- func (p *MaxNullIntValidator) Name() string
- func (p *MaxNullIntValidator) SetMessage(msg string) Validator
- func (p *MaxNullIntValidator) SetName(name string)
- func (p *MaxNullIntValidator) Validate(value null.Int) Error
- type MaxNumValidator
- func (p *MaxNumValidator[N]) HTMLCompatible() bool
- func (p *MaxNumValidator[N]) MarshalJSON() ([]byte, error)
- func (p *MaxNumValidator[N]) Name() string
- func (p *MaxNumValidator[N]) SetMessage(msg string) Validator
- func (p *MaxNumValidator[N]) SetName(name string)
- func (p *MaxNumValidator[N]) Validate(value N) Error
- type MaxStrValidator
- func (p *MaxStrValidator) HTMLCompatible() bool
- func (p *MaxStrValidator) MarshalJSON() ([]byte, error)
- func (p *MaxStrValidator) Name() string
- func (p *MaxStrValidator) SetMessage(msg string) Validator
- func (p *MaxStrValidator) SetName(name string)
- func (p *MaxStrValidator) Validate(value string) Error
- type MinNullIntValidator
- func (p *MinNullIntValidator) HTMLCompatible() bool
- func (p *MinNullIntValidator) MarshalJSON() ([]byte, error)
- func (p *MinNullIntValidator) Name() string
- func (p *MinNullIntValidator) Optional() Validator
- func (p *MinNullIntValidator) SetMessage(msg string) Validator
- func (p *MinNullIntValidator) SetName(name string)
- func (p *MinNullIntValidator) Validate(value null.Int) Error
- type MinNumValidator
- func (p *MinNumValidator[N]) HTMLCompatible() bool
- func (p *MinNumValidator[N]) MarshalJSON() ([]byte, error)
- func (p *MinNumValidator[N]) Name() string
- func (p *MinNumValidator[N]) Optional() Validator
- func (p *MinNumValidator[N]) SetMessage(msg string) Validator
- func (p *MinNumValidator[N]) SetName(name string)
- func (p *MinNumValidator[N]) Validate(value N) Error
- type MinStrValidator
- func (p *MinStrValidator) HTMLCompatible() bool
- func (p *MinStrValidator) MarshalJSON() ([]byte, error)
- func (p *MinStrValidator) Name() string
- func (p *MinStrValidator) Optional() Validator
- func (p *MinStrValidator) SetMessage(msg string) Validator
- func (p *MinStrValidator) SetName(name string)
- func (p *MinStrValidator) Validate(value string) Error
- type Number
- type PatternValidator
- func (p *PatternValidator) HTMLCompatible() bool
- func (p *PatternValidator) MarshalJSON() ([]byte, error)
- func (p *PatternValidator) Name() string
- func (p *PatternValidator) Optional() Validator
- func (p *PatternValidator) SetMessage(msg string) Validator
- func (p *PatternValidator) SetName(name string)
- func (p *PatternValidator) Validate(value any) Error
- type RequiredValidator
- func (p *RequiredValidator) HTMLCompatible() bool
- func (p *RequiredValidator) MarshalJSON() ([]byte, error)
- func (p *RequiredValidator) Name() string
- func (p *RequiredValidator) SetMessage(msg string) Validator
- func (p *RequiredValidator) SetName(name string)
- func (p *RequiredValidator) Validate(value any) Error
- type RuleHolder
- type Rules
- func (p *Rules[T]) Bind(source, target T) error
- func (p *Rules[T]) Field(fieldPtr any, validators ...Validator) *Rules[T]
- func (p *Rules[T]) MarshalJSON() ([]byte, error)
- func (p *Rules[T]) OnlyFor(name string) *Rules[T]
- func (p *Rules[T]) Struct(validators ...Validator) *Rules[T]
- func (p *Rules[T]) Validate(subject T) error
- func (p *Rules[T]) Validators() []Validator
- type StructFuncValidator
- type URLValidator
- type ValidJSONer
- type Validator
Examples ΒΆ
Constants ΒΆ
This section is empty.
Variables ΒΆ
Functions ΒΆ
func IsEmail ΒΆ
IsEmail returns true if the string is an email.
Example ΒΆ
package main import ( "fmt" "github.com/xuender/kvalid" ) func main() { fmt.Println(kvalid.IsEmail("test@example.com")) fmt.Println(kvalid.IsEmail("test")) }
Output: true false
func IsURL ΒΆ added in v1.0.7
IsURL returns true if the string is an URL.
Example ΒΆ
package main import ( "fmt" "github.com/xuender/kvalid" ) func main() { fmt.Println(kvalid.IsURL("http//:example.com")) fmt.Println(kvalid.IsURL("http://www.example.com")) fmt.Println(kvalid.IsURL("ftp://aa:pp@www.example.com")) fmt.Println(kvalid.IsURL("http://localhost:4200")) fmt.Println(kvalid.IsURL("http://127.0.0.1:4200")) }
Output: false true true true true
Types ΒΆ
type EmailValidator ΒΆ
type EmailValidator struct {
PatternValidator
}
EmailValidator field must be a valid email address.
func (*EmailValidator) MarshalJSON ΒΆ
func (p *EmailValidator) MarshalJSON() ([]byte, error)
MarshalJSON for this validator.
type FieldFuncValidator ΒΆ
type FieldFuncValidator[T any] struct { // contains filtered or unexported fields }
FieldFuncValidator for validating with custom function.
func (*FieldFuncValidator[T]) HTMLCompatible ΒΆ
func (p *FieldFuncValidator[T]) HTMLCompatible() bool
HTMLCompatible for this validator.
func (*FieldFuncValidator[T]) Name ΒΆ
func (p *FieldFuncValidator[T]) Name() string
Name of the field.
func (*FieldFuncValidator[T]) SetMessage ΒΆ
func (p *FieldFuncValidator[T]) SetMessage(_ string) Validator
SetMessage set error message.
func (*FieldFuncValidator[T]) SetName ΒΆ
func (p *FieldFuncValidator[T]) SetName(name string)
SetName of the field.
func (*FieldFuncValidator[T]) Validate ΒΆ
func (p *FieldFuncValidator[T]) Validate(value T) Error
Validate the value.
type IgnoreValidator ΒΆ added in v1.0.5
type IgnoreValidator struct {
// contains filtered or unexported fields
}
IgnoreValidator only for bind.
func (*IgnoreValidator) HTMLCompatible ΒΆ added in v1.0.5
func (p *IgnoreValidator) HTMLCompatible() bool
HTMLCompatible for this validator.
func (*IgnoreValidator) MarshalJSON ΒΆ added in v1.0.11
func (p *IgnoreValidator) MarshalJSON() ([]byte, error)
func (*IgnoreValidator) Name ΒΆ added in v1.0.5
func (p *IgnoreValidator) Name() string
Name of the field.
func (*IgnoreValidator) SetMessage ΒΆ added in v1.0.5
func (p *IgnoreValidator) SetMessage(message string) Validator
SetMessage set error message.
func (*IgnoreValidator) SetName ΒΆ added in v1.0.5
func (p *IgnoreValidator) SetName(name string)
SetName of the field.
func (*IgnoreValidator) Validate ΒΆ added in v1.0.5
func (p *IgnoreValidator) Validate(_ any)
Validate the value.
type MaxNullIntValidator ΒΆ
type MaxNullIntValidator struct {
// contains filtered or unexported fields
}
MaxNullIntValidator field have minimum value.
func MaxNullInt ΒΆ
func MaxNullInt(max int64) *MaxNullIntValidator
MaxNullInt field have minimum value.
func (*MaxNullIntValidator) HTMLCompatible ΒΆ
func (p *MaxNullIntValidator) HTMLCompatible() bool
HTMLCompatible for this validator.
func (*MaxNullIntValidator) MarshalJSON ΒΆ
func (p *MaxNullIntValidator) MarshalJSON() ([]byte, error)
MarshalJSON for this validator.
func (*MaxNullIntValidator) SetMessage ΒΆ
func (p *MaxNullIntValidator) SetMessage(msg string) Validator
SetMessage set error message.
func (*MaxNullIntValidator) SetName ΒΆ
func (p *MaxNullIntValidator) SetName(name string)
SetName of the field.
func (*MaxNullIntValidator) Validate ΒΆ
func (p *MaxNullIntValidator) Validate(value null.Int) Error
Validate the value.
type MaxNumValidator ΒΆ
type MaxNumValidator[N Number] struct { // contains filtered or unexported fields }
MaxNumValidator field have maximum value.
func (*MaxNumValidator[N]) HTMLCompatible ΒΆ
func (p *MaxNumValidator[N]) HTMLCompatible() bool
HTMLCompatible for this validator.
func (*MaxNumValidator[N]) MarshalJSON ΒΆ
func (p *MaxNumValidator[N]) MarshalJSON() ([]byte, error)
MarshalJSON for this validator.
func (*MaxNumValidator[N]) SetMessage ΒΆ
func (p *MaxNumValidator[N]) SetMessage(msg string) Validator
SetMessage set error message.
func (*MaxNumValidator[N]) SetName ΒΆ
func (p *MaxNumValidator[N]) SetName(name string)
SetName of the field.
func (*MaxNumValidator[N]) Validate ΒΆ
func (p *MaxNumValidator[N]) Validate(value N) Error
Validate the value.
type MaxStrValidator ΒΆ
type MaxStrValidator struct {
// contains filtered or unexported fields
}
MaxStrValidator field have maximum length.
func (*MaxStrValidator) HTMLCompatible ΒΆ
func (p *MaxStrValidator) HTMLCompatible() bool
HTMLCompatible for this validator.
func (*MaxStrValidator) MarshalJSON ΒΆ
func (p *MaxStrValidator) MarshalJSON() ([]byte, error)
MarshalJSON for this validator.
func (*MaxStrValidator) SetMessage ΒΆ
func (p *MaxStrValidator) SetMessage(msg string) Validator
SetMessage set error message.
func (*MaxStrValidator) SetName ΒΆ
func (p *MaxStrValidator) SetName(name string)
SetName of the field.
func (*MaxStrValidator) Validate ΒΆ
func (p *MaxStrValidator) Validate(value string) Error
Validate the value.
type MinNullIntValidator ΒΆ
type MinNullIntValidator struct {
// contains filtered or unexported fields
}
MinNullIntValidator field have minimum value.
func MinNullInt ΒΆ
func MinNullInt(min int64) *MinNullIntValidator
MinNullInt field have minimum value.
func (*MinNullIntValidator) HTMLCompatible ΒΆ
func (p *MinNullIntValidator) HTMLCompatible() bool
HTMLCompatible for this validator.
func (*MinNullIntValidator) MarshalJSON ΒΆ
func (p *MinNullIntValidator) MarshalJSON() ([]byte, error)
MarshalJSON for this validator.
func (*MinNullIntValidator) Optional ΒΆ
func (p *MinNullIntValidator) Optional() Validator
Optional don't validate if the value is zero.
func (*MinNullIntValidator) SetMessage ΒΆ
func (p *MinNullIntValidator) SetMessage(msg string) Validator
SetMessage set error message.
func (*MinNullIntValidator) SetName ΒΆ
func (p *MinNullIntValidator) SetName(name string)
SetName of the field.
func (*MinNullIntValidator) Validate ΒΆ
func (p *MinNullIntValidator) Validate(value null.Int) Error
Validate the value.
type MinNumValidator ΒΆ
type MinNumValidator[N Number] struct { // contains filtered or unexported fields }
MinNumValidator field have minimum value.
func (*MinNumValidator[N]) HTMLCompatible ΒΆ
func (p *MinNumValidator[N]) HTMLCompatible() bool
HTMLCompatible for this validator.
func (*MinNumValidator[N]) MarshalJSON ΒΆ
func (p *MinNumValidator[N]) MarshalJSON() ([]byte, error)
MarshalJSON for this validator.
func (*MinNumValidator[N]) Optional ΒΆ
func (p *MinNumValidator[N]) Optional() Validator
Optional don't validate if the value is zero.
func (*MinNumValidator[N]) SetMessage ΒΆ
func (p *MinNumValidator[N]) SetMessage(msg string) Validator
SetMessage set error message.
func (*MinNumValidator[N]) SetName ΒΆ
func (p *MinNumValidator[N]) SetName(name string)
SetName of the field.
func (*MinNumValidator[N]) Validate ΒΆ
func (p *MinNumValidator[N]) Validate(value N) Error
Validate the value.
type MinStrValidator ΒΆ
type MinStrValidator struct {
// contains filtered or unexported fields
}
MinStrValidator field must have minimum length.
func (*MinStrValidator) HTMLCompatible ΒΆ
func (p *MinStrValidator) HTMLCompatible() bool
HTMLCompatible for this validator.
func (*MinStrValidator) MarshalJSON ΒΆ
func (p *MinStrValidator) MarshalJSON() ([]byte, error)
MarshalJSON for this validator.
func (*MinStrValidator) Optional ΒΆ
func (p *MinStrValidator) Optional() Validator
Optional don't validate if the value is zero.
func (*MinStrValidator) SetMessage ΒΆ
func (p *MinStrValidator) SetMessage(msg string) Validator
SetMessage set error message.
func (*MinStrValidator) SetName ΒΆ
func (p *MinStrValidator) SetName(name string)
SetName of the field.
func (*MinStrValidator) Validate ΒΆ
func (p *MinStrValidator) Validate(value string) Error
Validate the value.
type Number ΒΆ
type Number interface { constraints.Integer | constraints.Float }
type PatternValidator ΒΆ
type PatternValidator struct {
// contains filtered or unexported fields
}
PatternValidator field must match regexp.
func (*PatternValidator) HTMLCompatible ΒΆ
func (p *PatternValidator) HTMLCompatible() bool
HTMLCompatible for this validator.
func (*PatternValidator) MarshalJSON ΒΆ
func (p *PatternValidator) MarshalJSON() ([]byte, error)
MarshalJSON for this validator.
func (*PatternValidator) Optional ΒΆ
func (p *PatternValidator) Optional() Validator
Optional don't validate if the value is zero.
func (*PatternValidator) SetMessage ΒΆ
func (p *PatternValidator) SetMessage(msg string) Validator
SetMessage set error message.
func (*PatternValidator) SetName ΒΆ
func (p *PatternValidator) SetName(name string)
SetName of the field.
func (*PatternValidator) Validate ΒΆ
func (p *PatternValidator) Validate(value any) Error
Validate the value.
type RequiredValidator ΒΆ
type RequiredValidator struct {
// contains filtered or unexported fields
}
RequiredValidator field must not be zero.
func (*RequiredValidator) HTMLCompatible ΒΆ
func (p *RequiredValidator) HTMLCompatible() bool
HTMLCompatible for this validator.
func (*RequiredValidator) MarshalJSON ΒΆ
func (p *RequiredValidator) MarshalJSON() ([]byte, error)
MarshalJSON for this validator.
func (*RequiredValidator) SetMessage ΒΆ
func (p *RequiredValidator) SetMessage(msg string) Validator
SetMessage set error message.
func (*RequiredValidator) SetName ΒΆ
func (p *RequiredValidator) SetName(name string)
SetName of the field.
func (*RequiredValidator) Validate ΒΆ
func (p *RequiredValidator) Validate(value any) Error
Validate the value.
type RuleHolder ΒΆ added in v1.0.2
RuleHolder needs to be Rules.
type Rules ΒΆ
type Rules[T any] struct { // contains filtered or unexported fields }
Rules for creating a chain of rules for validating a struct.
func (*Rules[T]) Bind ΒΆ added in v1.0.1
Example ΒΆ
package main import ( "fmt" "net/http" "github.com/xuender/kvalid" ) type Book struct { Title string `json:"title"` Author string `json:"author,omitempty"` Amount float64 Num int Enabled bool } // nolint: gomnd func (p *Book) Validation(method string) *kvalid.Rules[*Book] { switch method { case http.MethodPut: return kvalid.New(p). Field(&p.Amount, kvalid.Required().SetMessage("amount required"), kvalid.MinNum(10.3).Optional().SetMessage("amount min 10.3"), kvalid.MaxNum(2000.0).SetMessage("amount max 2000"), ). Field(&p.Enabled, kvalid.Ignore()). Field(&p.Num, kvalid.Ignore()) case http.MethodPost: return kvalid.New(p). Field(&p.Title, kvalid.Required().SetMessage("title required"), kvalid.MaxStr(200).SetMessage("title max 200"), ). Field(&p.Author, kvalid.Required().SetMessage("author required"), kvalid.MaxStr(100).SetMessage("author max 100"), ) default: panic("illegal method:" + method) } } func (p *Book) Validate(method string) error { return p.Validation(method).Validate(p) } func main() { source := &Book{Title: "Hello World", Amount: 99.9, Num: 3, Enabled: true} target := &Book{} postRules := source.Validation(http.MethodPost) putRules := source.Validation(http.MethodPut) fmt.Println(postRules.Bind(source, target)) source.Author = "ender" fmt.Println(postRules.Bind(source, target)) fmt.Println(target.Title) fmt.Println(target.Amount) fmt.Println(putRules.Bind(source, target)) fmt.Println(target.Amount) fmt.Println(target.Num) fmt.Println(target.Enabled) }
Output: author required. <nil> Hello World 0 <nil> 99.9 3 true
func (*Rules[T]) MarshalJSON ΒΆ
func (*Rules[T]) Validate ΒΆ
Validate a struct and return Errors.
Example ΒΆ
nolint: lll
package main import ( "encoding/json" "fmt" "net/http" "github.com/xuender/kvalid" ) type Book struct { Title string `json:"title"` Author string `json:"author,omitempty"` Amount float64 Num int Enabled bool } // nolint: gomnd func (p *Book) Validation(method string) *kvalid.Rules[*Book] { switch method { case http.MethodPut: return kvalid.New(p). Field(&p.Amount, kvalid.Required().SetMessage("amount required"), kvalid.MinNum(10.3).Optional().SetMessage("amount min 10.3"), kvalid.MaxNum(2000.0).SetMessage("amount max 2000"), ). Field(&p.Enabled, kvalid.Ignore()). Field(&p.Num, kvalid.Ignore()) case http.MethodPost: return kvalid.New(p). Field(&p.Title, kvalid.Required().SetMessage("title required"), kvalid.MaxStr(200).SetMessage("title max 200"), ). Field(&p.Author, kvalid.Required().SetMessage("author required"), kvalid.MaxStr(100).SetMessage("author max 100"), ) default: panic("illegal method:" + method) } } func (p *Book) Validate(method string) error { return p.Validation(method).Validate(p) } func main() { book := &Book{} fmt.Println(book.Validate(http.MethodPost)) book.Title = "Hello World" fmt.Println(book.Validate(http.MethodPost)) book.Author = "ender" fmt.Println(book.Validate(http.MethodPost)) fmt.Println(book.Validate(http.MethodPut)) data, _ := json.Marshal(book.Validation(http.MethodPut)) fmt.Println(string(data)) }
Output: title required. author required. author required. <nil> amount required. {"Amount":[{"rule":"required","msg":"amount required"},{"rule":"minNum","min":10.3,"msg":"amount min 10.3"},{"rule":"maxNum","max":2000,"msg":"amount max 2000"}],"Enabled":[{"rule":"ignore"}],"Num":[{"rule":"ignore"}]}
func (*Rules[T]) Validators ΒΆ
Validators for this chain.
type StructFuncValidator ΒΆ
type StructFuncValidator[T any] struct { // contains filtered or unexported fields }
StructFuncValidator validate struct with custom function.
func (*StructFuncValidator[T]) HTMLCompatible ΒΆ
func (c *StructFuncValidator[T]) HTMLCompatible() bool
HTMLCompatible for this validator.
func (*StructFuncValidator[T]) Name ΒΆ
func (c *StructFuncValidator[T]) Name() string
Name of the field.
func (*StructFuncValidator[T]) SetMessage ΒΆ
func (c *StructFuncValidator[T]) SetMessage(_ string) Validator
SetMessage set error message.
func (*StructFuncValidator[T]) SetName ΒΆ
func (c *StructFuncValidator[T]) SetName(name string)
SetName of the field.
func (*StructFuncValidator[T]) Validate ΒΆ
func (c *StructFuncValidator[T]) Validate(value T) Error
Validate the value.
type URLValidator ΒΆ added in v1.0.7
type URLValidator struct {
PatternValidator
}
URLValidator field must be a valid URL.
func (*URLValidator) MarshalJSON ΒΆ added in v1.0.7
func (p *URLValidator) MarshalJSON() ([]byte, error)
MarshalJSON for this validator.
type ValidJSONer ΒΆ added in v1.0.4
ValidJSONer to implement a Validator JSON map.
type Validator ΒΆ
type Validator interface { SetName(name string) Name() string HTMLCompatible() bool SetMessage(message string) Validator }
Validator to implement a rule.
func StructFunc ΒΆ
StructFunc validate struct with custom function.