2019-04-25 04:52:08 +07:00
|
|
|
package schema
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// ErrorContainer represents a container where we can add errors and retrieve them.
|
2019-04-25 04:52:08 +07:00
|
|
|
type ErrorContainer interface {
|
|
|
|
Push(err error)
|
2021-01-04 17:28:55 +07:00
|
|
|
PushWarning(err error)
|
2019-04-25 04:52:08 +07:00
|
|
|
HasErrors() bool
|
2021-01-04 17:28:55 +07:00
|
|
|
HasWarnings() bool
|
2019-04-25 04:52:08 +07:00
|
|
|
Errors() []error
|
2021-01-04 17:28:55 +07:00
|
|
|
Warnings() []error
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// StructValidator is a validator for structs.
|
2019-04-25 04:52:08 +07:00
|
|
|
type StructValidator struct {
|
2021-01-04 17:28:55 +07:00
|
|
|
errors []error
|
|
|
|
warnings []error
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// NewStructValidator is a constructor of struct validator.
|
2019-04-25 04:52:08 +07:00
|
|
|
func NewStructValidator() *StructValidator {
|
|
|
|
val := new(StructValidator)
|
|
|
|
val.errors = make([]error, 0)
|
2021-01-04 17:28:55 +07:00
|
|
|
val.warnings = make([]error, 0)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
// Push an error to the validator.
|
2019-04-25 04:52:08 +07:00
|
|
|
func (v *StructValidator) Push(err error) {
|
|
|
|
v.errors = append(v.errors, err)
|
|
|
|
}
|
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
// PushWarning error to the validator.
|
|
|
|
func (v *StructValidator) PushWarning(err error) {
|
|
|
|
v.warnings = append(v.warnings, err)
|
|
|
|
}
|
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
// HasErrors checks whether the validator contains errors.
|
|
|
|
func (v *StructValidator) HasErrors() bool {
|
|
|
|
return len(v.errors) > 0
|
|
|
|
}
|
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
// HasWarnings checks whether the validator contains warning errors.
|
|
|
|
func (v *StructValidator) HasWarnings() bool {
|
|
|
|
return len(v.warnings) > 0
|
|
|
|
}
|
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
// Errors returns the errors.
|
|
|
|
func (v *StructValidator) Errors() []error {
|
|
|
|
return v.errors
|
|
|
|
}
|
2020-03-28 13:10:39 +07:00
|
|
|
|
2021-01-04 17:28:55 +07:00
|
|
|
// Warnings returns the warnings.
|
|
|
|
func (v *StructValidator) Warnings() []error {
|
|
|
|
return v.warnings
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear errors and warnings.
|
2020-03-28 13:10:39 +07:00
|
|
|
func (v *StructValidator) Clear() {
|
|
|
|
v.errors = []error{}
|
2021-01-04 17:28:55 +07:00
|
|
|
v.warnings = []error{}
|
2020-03-28 13:10:39 +07:00
|
|
|
}
|