Documentation ¶
Overview ¶
Package structcheck validates struct constraints.
Checks that constraints on structs are met. Constraints are read as a comma-delimited list on the "checks" tag. Validate constraints by running structcheck.Validate().
See the Checks map for the full list of built-in checks.
Example:
package main import ( "bytes" "encoding/json" "fmt" "github.com/Manbeardo/structcheck" ) type MyJsonObjectType struct { NestedObject struct{ Number int `checks:"Positive"` Pointer *int `checks:"NotNil"` Slice []int `checks:"NotEmpty"` } } var badJson = []byte(`{"NestedObject":{"Number":-1}}`) func main() { var o MyJsonObjectType json.NewDecoder(bytes.NewBuffer(badJson)).Decode(&o) err := structcheck.Validate(o) if err != nil { fmt.Println(err.Error()) } }
Prints:
The following field(s) failed checks: MyJsonObjectType.NestedObject.Number: Positive: (int)(-1) MyJsonObjectType.NestedObject.Pointer: NotNil: (*int)(nil) MyJsonObjectType.NestedObject.Slice: NotEmpty: []int(nil)
Index ¶
- Variables
- func CheckFieldExists(i interface{}, fieldName string) bool
- func CheckFieldsExist(i interface{}, fieldNames []string) error
- func CheckFieldsNotNil(i interface{}, fieldNames []string) error
- func CheckNoNils(i interface{}) error
- func CheckSign(v reflect.Value, s Sign) bool
- func CustomValidate(i interface{}, checkFinder CheckFinder) error
- func Validate(i interface{}) error
- type ByFieldOrder
- type Check
- type CheckFinder
- type ErrorChecksFailed
- type ErrorIllegalCheck
- type ErrorInvalidKind
- type ErrorNilValue
- type Field
- type KindClass
- type Sign
Constants ¶
This section is empty.
Variables ¶
var Container = KindClass{ reflect.String: nil, reflect.Array: nil, reflect.Slice: nil, reflect.Map: nil, reflect.Chan: nil, }
var DefaultChecks = map[string]Check{ "NotNil": func(v reflect.Value) bool { return !(Nilable.Check(v) && v.IsNil()) }, "Nil": func(v reflect.Value) bool { return !(Nilable.Check(v) && !v.IsNil()) }, "Positive": func(v reflect.Value) bool { return CheckSign(v, SignPositive) }, "Negative": func(v reflect.Value) bool { return CheckSign(v, SignNegative) }, "NoSign": func(v reflect.Value) bool { return CheckSign(v, SignNone) }, "NotEmpty": func(v reflect.Value) bool { return !(Container.Check(v) && v.Len() == 0) }, "Empty": func(v reflect.Value) bool { return !(Container.Check(v) && v.Len() != 0) }, "Nilable": func(v reflect.Value) bool { return Nilable.Check(v) }, "Numeric": func(v reflect.Value) bool { return Numeric.Check(v) }, "Container": func(v reflect.Value) bool { return Container.Check(v) }, }
var Nilable = KindClass{ reflect.Ptr: nil, reflect.Interface: nil, reflect.Chan: nil, reflect.Func: nil, reflect.Map: nil, reflect.Slice: nil, }
var Numeric = KindClass{ reflect.Int: nil, reflect.Int8: nil, reflect.Int16: nil, reflect.Int32: nil, reflect.Int64: nil, reflect.Uint: nil, reflect.Uint8: nil, reflect.Uint16: nil, reflect.Uint32: nil, reflect.Uint64: nil, reflect.Float32: nil, reflect.Float64: nil, reflect.Complex64: nil, reflect.Complex128: nil, }
Functions ¶
func CheckFieldExists ¶
func CheckFieldsExist ¶
func CheckFieldsNotNil ¶
checks that the named fields and their parents exist and are not null
func CheckNoNils ¶
func CheckNoNils(i interface{}) error
checks that no fields in the struct are nil
func CustomValidate ¶
func CustomValidate(i interface{}, checkFinder CheckFinder) error
runs Validate with a custom set of checks
Types ¶
type ByFieldOrder ¶
type ByFieldOrder []Field
sorts fields by their natural (in-struct) order
func (ByFieldOrder) Len ¶
func (a ByFieldOrder) Len() int
func (ByFieldOrder) Less ¶
func (a ByFieldOrder) Less(i, j int) bool
func (ByFieldOrder) Swap ¶
func (a ByFieldOrder) Swap(i, j int)
type CheckFinder ¶
func BuildFixedCheckFinder ¶
func BuildFixedCheckFinder(checkNames []string, checkSet map[string]Check) (CheckFinder, error)
Builds a CheckFinder that returns the same set of checks for all fields. checkSet is read at build-time, not check-time.
func BuildStringyCheckFinder ¶
func BuildStringyCheckFinder(field2checks map[string][]string, checkSet map[string]Check) (CheckFinder, error)
Builds a CheckFinder that runs the named checks on the named fields. checkSet and field2checks are copied and verified at build-time
func BuildTagCheckFinder ¶
func BuildTagCheckFinder(checkSet map[string]Check) CheckFinder
type ErrorChecksFailed ¶
returned when checks fail on fields
func (ErrorChecksFailed) Error ¶
func (e ErrorChecksFailed) Error() string
type ErrorIllegalCheck ¶
type ErrorIllegalCheck struct { Reason string // contains filtered or unexported fields }
returned when an illegal (likely misspelled) check is encountered
func (ErrorIllegalCheck) Error ¶
func (e ErrorIllegalCheck) Error() string
type ErrorInvalidKind ¶
returned when the top level object does not drill down to a struct.
func (ErrorInvalidKind) Error ¶
func (e ErrorInvalidKind) Error() string
type ErrorNilValue ¶
type ErrorNilValue struct{}
returned when a top level nil is received
func (ErrorNilValue) Error ¶
func (e ErrorNilValue) Error() string