Documentation
¶
Overview ¶
Package validation provides small validation helpers.
Notification collects multiple validation errors into one error value. Specification and its combinators model reusable boolean predicates.
Index ¶
- type AllSpecification
- type AndNotSpecification
- type AndSpecification
- type AnySpecification
- type ErrChain
- type NotSpecification
- type Notification
- type OrNotSpecification
- type OrSpecification
- type Specification
- func NewAllSpecification(specs ...Specification) Specification
- func NewAndNotSpecification(s1, s2 Specification) Specification
- func NewAndSpecification(s1, s2 Specification) Specification
- func NewAnySpecification(specs ...Specification) Specification
- func NewNotSpecification(spec Specification) Specification
- func NewOrNotSpecification(s1, s2 Specification) Specification
- func NewOrSpecification(s1, s2 Specification) Specification
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AllSpecification ¶
type AllSpecification struct {
// contains filtered or unexported fields
}
func (*AllSpecification) IsSatisfiedBy ¶
func (all *AllSpecification) IsSatisfiedBy(candidate interface{}) bool
func (*AllSpecification) String ¶
func (all *AllSpecification) String() string
type AndNotSpecification ¶
type AndNotSpecification struct {
// contains filtered or unexported fields
}
func (*AndNotSpecification) IsSatisfiedBy ¶
func (an *AndNotSpecification) IsSatisfiedBy(candidate interface{}) bool
func (*AndNotSpecification) String ¶
func (an *AndNotSpecification) String() string
type AndSpecification ¶
type AndSpecification struct {
// contains filtered or unexported fields
}
func (*AndSpecification) IsSatisfiedBy ¶
func (and *AndSpecification) IsSatisfiedBy(candidate interface{}) bool
func (*AndSpecification) String ¶
func (and *AndSpecification) String() string
type AnySpecification ¶
type AnySpecification struct {
// contains filtered or unexported fields
}
func (*AnySpecification) IsSatisfiedBy ¶
func (any *AnySpecification) IsSatisfiedBy(candidate interface{}) bool
func (*AnySpecification) String ¶
func (any *AnySpecification) String() string
type ErrChain ¶
type ErrChain struct {
// contains filtered or unexported fields
}
ErrChain implement error interface
type NotSpecification ¶
type NotSpecification struct {
// contains filtered or unexported fields
}
func (*NotSpecification) IsSatisfiedBy ¶
func (not *NotSpecification) IsSatisfiedBy(candidate interface{}) bool
func (*NotSpecification) String ¶
func (not *NotSpecification) String() string
type Notification ¶
Notification define a interface to abstract notification pattern
func NewSimpleNotification ¶
func NewSimpleNotification() Notification
Example ¶
package main
import (
"errors"
"fmt"
"github.com/go-jimu/components/validation"
)
func main() {
notification := validation.NewSimpleNotification()
notification.Add(errors.New("name is required"))
notification.Add(nil)
notification.Add(errors.New("age must be positive"))
fmt.Println(notification.Err() != nil)
}
Output: true
type OrNotSpecification ¶
type OrNotSpecification struct {
// contains filtered or unexported fields
}
func (*OrNotSpecification) IsSatisfiedBy ¶
func (on *OrNotSpecification) IsSatisfiedBy(candidate interface{}) bool
func (*OrNotSpecification) String ¶
func (on *OrNotSpecification) String() string
type OrSpecification ¶
type OrSpecification struct {
// contains filtered or unexported fields
}
func (*OrSpecification) IsSatisfiedBy ¶
func (or *OrSpecification) IsSatisfiedBy(candidate interface{}) bool
func (*OrSpecification) String ¶
func (or *OrSpecification) String() string
type Specification ¶
type Specification interface {
// // String print the specification details
fmt.Stringer
// IsSatisfiedBy check if candidate matched the specification
IsSatisfiedBy(candidate interface{}) bool
}
Specification define a interface
Example ¶
package main
import (
"fmt"
"github.com/go-jimu/components/validation"
)
type intSpecification struct {
name string
match func(int) bool
}
func (s intSpecification) IsSatisfiedBy(candidate interface{}) bool {
value, ok := candidate.(int)
return ok && s.match(value)
}
func (s intSpecification) String() string {
return s.name
}
func main() {
spec := validation.NewAndSpecification(
intSpecification{name: ">= 10", match: func(value int) bool { return value >= 10 }},
intSpecification{name: "< 80", match: func(value int) bool { return value < 80 }},
)
fmt.Println(spec.String())
fmt.Println(spec.IsSatisfiedBy(42))
fmt.Println(spec.IsSatisfiedBy(7))
}
Output: (>= 10) AND (< 80) true false
func NewAllSpecification ¶
func NewAllSpecification(specs ...Specification) Specification
func NewAndNotSpecification ¶
func NewAndNotSpecification(s1, s2 Specification) Specification
func NewAndSpecification ¶
func NewAndSpecification(s1, s2 Specification) Specification
func NewAnySpecification ¶
func NewAnySpecification(specs ...Specification) Specification
func NewNotSpecification ¶
func NewNotSpecification(spec Specification) Specification
func NewOrNotSpecification ¶
func NewOrNotSpecification(s1, s2 Specification) Specification
func NewOrSpecification ¶
func NewOrSpecification(s1, s2 Specification) Specification
Click to show internal directories.
Click to hide internal directories.