mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-12-20 11:51:17 +00:00
[core] Enforce field validation on config
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
8c2c4e7c62
commit
8154a50351
25 changed files with 57 additions and 6 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
|
@ -132,12 +133,15 @@ func (ActorScript) Name() string { return "script" }
|
|||
|
||||
// Validate implements actor interface
|
||||
func (ActorScript) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
|
||||
cmd, err := attrs.StringSlice("command")
|
||||
if err != nil || len(cmd) == 0 {
|
||||
return errors.New("command must be slice of strings with length > 0")
|
||||
if err = attrs.ValidateSchema(
|
||||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "command", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeStringSlice}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "skip_cooldown_on_error", Type: fieldcollection.SchemaFieldTypeBool}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
}
|
||||
|
||||
for i, el := range cmd {
|
||||
for i, el := range attrs.MustStringSlice("command", nil) {
|
||||
if err = tplValidator(el); err != nil {
|
||||
return errors.Wrapf(err, "validating cmd template (element %d)", i)
|
||||
}
|
||||
|
|
|
@ -113,6 +113,8 @@ func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldco
|
|||
if err = attrs.ValidateSchema(
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "channel", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "creator", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "add_delay", Type: fieldcollection.SchemaFieldTypeBool}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
helpers.SchemaValidateTemplateField(tplValidator, "channel", "creator"),
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
|
|
|
@ -81,6 +81,7 @@ func (actor) Name() string { return actorName }
|
|||
func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
|
||||
if err = attrs.ValidateSchema(
|
||||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "duration", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
helpers.SchemaValidateTemplateField(tplValidator, "duration"),
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
|
|
|
@ -263,6 +263,7 @@ func (actorCounter) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *
|
|||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "counter", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "counter_step", Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "counter_set", Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
helpers.SchemaValidateTemplateField(tplValidator, "counter", "counter_step", "counter_set"),
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
package delay
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
|
@ -72,6 +73,13 @@ func (actor) Execute(_ *irc.Client, _ *irc.Message, _ *plugins.Rule, _ *fieldcol
|
|||
func (actor) IsAsync() bool { return false }
|
||||
func (actor) Name() string { return actorName }
|
||||
|
||||
func (actor) Validate(plugins.TemplateValidatorFunc, *fieldcollection.FieldCollection) (err error) {
|
||||
func (actor) Validate(_ plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
|
||||
if err = attrs.ValidateSchema(
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "delay", Type: fieldcollection.SchemaFieldTypeDuration}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "jitter", Type: fieldcollection.SchemaFieldTypeDuration}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -75,6 +75,7 @@ func (actor) Name() string { return actorName }
|
|||
func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
|
||||
if err = attrs.ValidateSchema(
|
||||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "fields", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
helpers.SchemaValidateTemplateField(tplValidator, "fields"),
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
|
|
|
@ -120,6 +120,7 @@ func (actor) Name() string { return actorName }
|
|||
func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
|
||||
if err = attrs.ValidateSchema(
|
||||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "source", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
helpers.SchemaValidateTemplateField(tplValidator, "source"),
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
package linkdetector
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gopkg.in/irc.v4"
|
||||
|
||||
"github.com/Luzifer/go_helpers/v2/fieldcollection"
|
||||
|
@ -64,6 +66,13 @@ func (Actor) IsAsync() bool { return false }
|
|||
func (Actor) Name() string { return actorName }
|
||||
|
||||
// Validate implements the actor interface
|
||||
func (Actor) Validate(plugins.TemplateValidatorFunc, *fieldcollection.FieldCollection) error {
|
||||
func (Actor) Validate(_ plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
|
||||
if err = attrs.ValidateSchema(
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "heuristic", Type: fieldcollection.SchemaFieldTypeBool}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -223,6 +223,13 @@ func (actor) Validate(_ plugins.TemplateValidatorFunc, attrs *fieldcollection.Fi
|
|||
if err = attrs.ValidateSchema(
|
||||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "action", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "reason", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "allowed_links", Type: fieldcollection.SchemaFieldTypeStringSlice}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "disallowed_links", Type: fieldcollection.SchemaFieldTypeStringSlice}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "allowed_clip_channels", Type: fieldcollection.SchemaFieldTypeStringSlice}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "disallowed_clip_channels", Type: fieldcollection.SchemaFieldTypeStringSlice}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "stop_on_action", Type: fieldcollection.SchemaFieldTypeBool}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "stop_on_no_action", Type: fieldcollection.SchemaFieldTypeBool}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
func(attrs, _ *fieldcollection.FieldCollection) error {
|
||||
if len(attrs.MustStringSlice("allowed_links", helpers.Ptr([]string{})))+
|
||||
len(attrs.MustStringSlice("disallowed_links", helpers.Ptr([]string{})))+
|
||||
|
|
|
@ -65,6 +65,7 @@ func (actor) Name() string { return "log" }
|
|||
func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
|
||||
if err = attrs.ValidateSchema(
|
||||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "message", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
helpers.SchemaValidateTemplateField(tplValidator, "message"),
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
|
|
|
@ -125,6 +125,7 @@ func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldco
|
|||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "channel", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "game", Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "title", Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
helpers.SchemaValidateTemplateField(tplValidator, "channel", "game", "title"),
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
|
|
|
@ -239,6 +239,7 @@ func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldco
|
|||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "match", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "action", Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "scan", Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
helpers.SchemaValidateTemplateField(tplValidator, "scan", "action", "match"),
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
|
|
|
@ -233,6 +233,7 @@ func (actorPunish) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *f
|
|||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "cooldown", Type: fieldcollection.SchemaFieldTypeDuration}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "reason", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "uuid", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
helpers.SchemaValidateTemplateField(tplValidator, "user"),
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
|
@ -266,6 +267,7 @@ func (actorResetPunish) Validate(tplValidator plugins.TemplateValidatorFunc, att
|
|||
if err = attrs.ValidateSchema(
|
||||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "user", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "uuid", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
helpers.SchemaValidateTemplateField(tplValidator, "user"),
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
|
|
|
@ -198,6 +198,7 @@ func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldco
|
|||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "quote", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "index", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "format", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
helpers.SchemaValidateTemplateField(tplValidator, "index", "quote", "format"),
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
|
|
|
@ -72,6 +72,7 @@ func (actor) Name() string { return actorName }
|
|||
func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
|
||||
if err = attrs.ValidateSchema(
|
||||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "message", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
helpers.SchemaValidateTemplateField(tplValidator, "message"),
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
|
|
|
@ -151,6 +151,7 @@ func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldco
|
|||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "fallback", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "as_reply", Type: fieldcollection.SchemaFieldTypeBool}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "to_channel", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
helpers.SchemaValidateTemplateField(tplValidator, "message", "fallback"),
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
|
|
|
@ -65,6 +65,7 @@ func (actor) Name() string { return actorName }
|
|||
func (actor) Validate(_ plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
|
||||
if err = attrs.ValidateSchema(
|
||||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "enable", Type: fieldcollection.SchemaFieldTypeBool}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
}
|
||||
|
|
|
@ -80,6 +80,7 @@ func (actor) Name() string { return actorName }
|
|||
func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
|
||||
if err = attrs.ValidateSchema(
|
||||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "user", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
helpers.SchemaValidateTemplateField(tplValidator, "user"),
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
|
|
|
@ -65,6 +65,7 @@ func (actor) Name() string { return actorName }
|
|||
func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
|
||||
if err = attrs.ValidateSchema(
|
||||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "when", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
helpers.SchemaValidateTemplateField(tplValidator, "when"),
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
|
|
|
@ -93,6 +93,7 @@ func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldco
|
|||
if err = attrs.ValidateSchema(
|
||||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "duration", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeDuration}),
|
||||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "reason", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
helpers.SchemaValidateTemplateField(tplValidator, "reason"),
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
|
|
|
@ -178,6 +178,7 @@ func (actorSetVariable) Validate(tplValidator plugins.TemplateValidatorFunc, att
|
|||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "variable", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "clear", Type: fieldcollection.SchemaFieldTypeBool}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "set", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
helpers.SchemaValidateTemplateField(tplValidator, "set", "variable"),
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
|
|
|
@ -103,6 +103,7 @@ func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldco
|
|||
if err = attrs.ValidateSchema(
|
||||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "channel", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "user", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
helpers.SchemaValidateTemplateField(tplValidator, "channel", "user"),
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
|
|
|
@ -84,6 +84,7 @@ func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldco
|
|||
if err = attrs.ValidateSchema(
|
||||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "message", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "to", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
helpers.SchemaValidateTemplateField(tplValidator, "message", "to"),
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
|
|
|
@ -42,6 +42,7 @@ func (actor) Validate(tplValidator plugins.TemplateValidatorFunc, attrs *fieldco
|
|||
if err = attrs.ValidateSchema(
|
||||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "fields", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.CanHaveField(fieldcollection.SchemaField{Name: "schedule_in", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
helpers.SchemaValidateTemplateField(tplValidator, "fields", "schedule_in"),
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
|
|
|
@ -75,6 +75,7 @@ func (enterRaffleActor) Name() string { return "enter-raffle" }
|
|||
func (enterRaffleActor) Validate(_ plugins.TemplateValidatorFunc, attrs *fieldcollection.FieldCollection) (err error) {
|
||||
if err = attrs.ValidateSchema(
|
||||
fieldcollection.MustHaveField(fieldcollection.SchemaField{Name: "keyword", NonEmpty: true, Type: fieldcollection.SchemaFieldTypeString}),
|
||||
fieldcollection.MustHaveNoUnknowFields,
|
||||
); err != nil {
|
||||
return fmt.Errorf("validating attributes: %w", err)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue