mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-12-21 04:11:16 +00:00
72 lines
2 KiB
Go
72 lines
2 KiB
Go
|
package plugins
|
||
|
|
||
|
import (
|
||
|
"reflect"
|
||
|
|
||
|
"github.com/pkg/errors"
|
||
|
)
|
||
|
|
||
|
type (
|
||
|
// ActorKit contains some common validation functions to be used
|
||
|
// when implementing actors
|
||
|
ActorKit struct{}
|
||
|
)
|
||
|
|
||
|
// ValidateRequireNonEmpty checks whether the fields are gettable
|
||
|
// (not returning ErrValueNotSet) and does not contain zero value
|
||
|
// recognized by reflect (to just check whether the field is set
|
||
|
// but allow zero values use HasAll on the FieldCollection)
|
||
|
func (ActorKit) ValidateRequireNonEmpty(attrs *FieldCollection, fields ...string) error {
|
||
|
for _, field := range fields {
|
||
|
v, err := attrs.Any(field)
|
||
|
if err != nil {
|
||
|
return errors.Wrapf(err, "getting field %s", field)
|
||
|
}
|
||
|
|
||
|
if reflect.ValueOf(v).IsZero() {
|
||
|
return errors.Errorf("field %s has zero-value", field)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// ValidateRequireValidTemplate checks whether fields are gettable
|
||
|
// as strings and do have a template which validates (this does not
|
||
|
// check for empty strings as an empty template is indeed valid)
|
||
|
func (ActorKit) ValidateRequireValidTemplate(tplValidator TemplateValidatorFunc, attrs *FieldCollection, fields ...string) error {
|
||
|
for _, field := range fields {
|
||
|
v, err := attrs.String(field)
|
||
|
if err != nil {
|
||
|
return errors.Wrapf(err, "getting string field %s", field)
|
||
|
}
|
||
|
|
||
|
if err = tplValidator(v); err != nil {
|
||
|
return errors.Wrapf(err, "validaging template field %s", field)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// ValidateRequireValidTemplateIfSet checks whether the field is
|
||
|
// either not set or a valid template (this does not
|
||
|
// check for empty strings as an empty template is indeed valid)
|
||
|
func (ActorKit) ValidateRequireValidTemplateIfSet(tplValidator TemplateValidatorFunc, attrs *FieldCollection, fields ...string) error {
|
||
|
for _, field := range fields {
|
||
|
v, err := attrs.String(field)
|
||
|
if err != nil {
|
||
|
if errors.Is(err, ErrValueNotSet) {
|
||
|
continue
|
||
|
}
|
||
|
return errors.Wrapf(err, "getting string field %s", field)
|
||
|
}
|
||
|
|
||
|
if err = tplValidator(v); err != nil {
|
||
|
return errors.Wrapf(err, "validaging template field %s", field)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|