2019-04-24 21:52:08 +00:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/Workiva/go-datastructures/queue"
|
|
|
|
)
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// ErrorContainer represents a container where we can add errors and retrieve them.
|
2019-04-24 21:52:08 +00:00
|
|
|
type ErrorContainer interface {
|
|
|
|
Push(err error)
|
|
|
|
HasErrors() bool
|
|
|
|
Errors() []error
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Validator represents the validator interface.
|
2019-04-24 21:52:08 +00:00
|
|
|
type Validator struct {
|
|
|
|
errors map[string][]error
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// NewValidator create a validator.
|
2019-04-24 21:52:08 +00:00
|
|
|
func NewValidator() *Validator {
|
|
|
|
validator := new(Validator)
|
|
|
|
validator.errors = make(map[string][]error)
|
|
|
|
return validator
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueueItem an item representing a struct field and its path.
|
|
|
|
type QueueItem struct {
|
|
|
|
value reflect.Value
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2020-04-09 01:05:17 +00:00
|
|
|
func (v *Validator) validateOne(item QueueItem, q *queue.Queue) error { //nolint:unparam
|
2019-04-24 21:52:08 +00:00
|
|
|
if item.value.Type().Kind() == reflect.Ptr {
|
|
|
|
if item.value.IsNil() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
elem := item.value.Elem()
|
2020-04-22 03:33:14 +00:00
|
|
|
q.Put(QueueItem{ //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
2019-04-24 21:52:08 +00:00
|
|
|
value: elem,
|
|
|
|
path: item.path,
|
|
|
|
})
|
|
|
|
} else if item.value.Kind() == reflect.Struct {
|
|
|
|
numFields := item.value.Type().NumField()
|
|
|
|
|
|
|
|
validateFn := item.value.Addr().MethodByName("Validate")
|
|
|
|
|
|
|
|
if validateFn.IsValid() {
|
|
|
|
structValidator := NewStructValidator()
|
|
|
|
validateFn.Call([]reflect.Value{reflect.ValueOf(structValidator)})
|
|
|
|
v.errors[item.path] = structValidator.Errors()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < numFields; i++ {
|
|
|
|
field := item.value.Type().Field(i)
|
|
|
|
value := item.value.Field(i)
|
|
|
|
|
2020-04-22 03:33:14 +00:00
|
|
|
q.Put(QueueItem{ //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
2019-04-24 21:52:08 +00:00
|
|
|
value: value,
|
|
|
|
path: item.path + "." + field.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Validate validate a struct.
|
2019-04-24 21:52:08 +00:00
|
|
|
func (v *Validator) Validate(s interface{}) error {
|
|
|
|
q := queue.New(40)
|
2020-04-22 03:33:14 +00:00
|
|
|
q.Put(QueueItem{value: reflect.ValueOf(s), path: "root"}) //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
for !q.Empty() {
|
|
|
|
val, err := q.Get(1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
item, ok := val[0].(QueueItem)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Cannot convert item into QueueItem")
|
|
|
|
}
|
2020-04-22 03:33:14 +00:00
|
|
|
v.validateOne(item, q) //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// PrintErrors display the errors thrown during validation.
|
2019-04-24 21:52:08 +00:00
|
|
|
func (v *Validator) PrintErrors() {
|
|
|
|
for path, errs := range v.errors {
|
|
|
|
fmt.Printf("Errors at %s:\n", path)
|
|
|
|
for _, err := range errs {
|
|
|
|
fmt.Printf("--> %s\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Errors return the errors thrown during validation.
|
2019-04-24 21:52:08 +00:00
|
|
|
func (v *Validator) Errors() map[string][]error {
|
|
|
|
return v.errors
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// StructValidator is a validator for structs.
|
2019-04-24 21:52:08 +00:00
|
|
|
type StructValidator struct {
|
|
|
|
errors []error
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// NewStructValidator is a constructor of struct validator.
|
2019-04-24 21:52:08 +00:00
|
|
|
func NewStructValidator() *StructValidator {
|
|
|
|
val := new(StructValidator)
|
|
|
|
val.errors = make([]error, 0)
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push an error in the validator.
|
|
|
|
func (v *StructValidator) Push(err error) {
|
|
|
|
v.errors = append(v.errors, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasErrors checks whether the validator contains errors.
|
|
|
|
func (v *StructValidator) HasErrors() bool {
|
|
|
|
return len(v.errors) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Errors returns the errors.
|
|
|
|
func (v *StructValidator) Errors() []error {
|
|
|
|
return v.errors
|
|
|
|
}
|
2020-03-28 06:10:39 +00:00
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Clear errors.
|
2020-03-28 06:10:39 +00:00
|
|
|
func (v *StructValidator) Clear() {
|
|
|
|
v.errors = []error{}
|
|
|
|
}
|