2019-04-24 21:52:08 +00:00
|
|
|
package schema
|
|
|
|
|
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)
|
2021-01-04 10:28:55 +00:00
|
|
|
PushWarning(err error)
|
2019-04-24 21:52:08 +00:00
|
|
|
HasErrors() bool
|
2021-01-04 10:28:55 +00:00
|
|
|
HasWarnings() bool
|
2019-04-24 21:52:08 +00:00
|
|
|
Errors() []error
|
2021-01-04 10:28:55 +00:00
|
|
|
Warnings() []error
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-01-04 10:28:55 +00:00
|
|
|
errors []error
|
|
|
|
warnings []error
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2021-01-04 10:28:55 +00:00
|
|
|
val.warnings = make([]error, 0)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
// Push an error to the validator.
|
2019-04-24 21:52:08 +00:00
|
|
|
func (v *StructValidator) Push(err error) {
|
|
|
|
v.errors = append(v.errors, err)
|
|
|
|
}
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
// PushWarning error to the validator.
|
|
|
|
func (v *StructValidator) PushWarning(err error) {
|
|
|
|
v.warnings = append(v.warnings, err)
|
|
|
|
}
|
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
// HasErrors checks whether the validator contains errors.
|
|
|
|
func (v *StructValidator) HasErrors() bool {
|
|
|
|
return len(v.errors) > 0
|
|
|
|
}
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
// HasWarnings checks whether the validator contains warning errors.
|
|
|
|
func (v *StructValidator) HasWarnings() bool {
|
|
|
|
return len(v.warnings) > 0
|
|
|
|
}
|
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
// Errors returns the errors.
|
|
|
|
func (v *StructValidator) Errors() []error {
|
|
|
|
return v.errors
|
|
|
|
}
|
2020-03-28 06:10:39 +00:00
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
// Warnings returns the warnings.
|
|
|
|
func (v *StructValidator) Warnings() []error {
|
|
|
|
return v.warnings
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear errors and warnings.
|
2020-03-28 06:10:39 +00:00
|
|
|
func (v *StructValidator) Clear() {
|
|
|
|
v.errors = []error{}
|
2021-01-04 10:28:55 +00:00
|
|
|
v.warnings = []error{}
|
2020-03-28 06:10:39 +00:00
|
|
|
}
|