From b4dc5d6841f7ded5995e5f308509b1a3a034cbed Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 24 Dec 2020 15:18:24 +0100 Subject: Began implementing config validation --- config/menu.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'config/menu.go') diff --git a/config/menu.go b/config/menu.go index 2913be2..160afa3 100644 --- a/config/menu.go +++ b/config/menu.go @@ -1,5 +1,14 @@ package config +import ( + "regexp" + + "github.com/pkg/errors" +) + +var reValidMenuName = regexp.MustCompile(`^[\w\._]+$`) +var reValidKey = regexp.MustCompile(`^\w$`) + // Menu struct describes a screen menu type Menu struct { // Banner is the banner to display before the menu @@ -21,3 +30,49 @@ type MenuEntry struct { // Action is the action executed when the menu entry is selected Action string `yaml:"Action"` } + +func (m *Menu) validate(name string) error { + // validate name + if ok := reValidMenuName.MatchString(name); !ok { + return errors.New("Invalid menu name, must be an alphanumeric word and match regex `^[\\w\\._]+$` : " + name) + } + // Banner is just any string, nothing to validate + // XOffset + if m.XOffset <= 0 { + return errors.New("XOffset must be a positive integer") + } + // YOffset + if m.YOffset <= 0 { + return errors.New("YOffset must be a positive integer") + } + // MenuEntries + keys := make(map[string]bool) + for i := 0; i < len(m.MenuEntries); i++ { + m.MenuEntries[i].validate() + if _, duplicate := keys[m.MenuEntries[i].Key]; duplicate { + return errors.New("A Menu has a duplicate key " + m.MenuEntries[i].Key) + } + keys[m.MenuEntries[i].Key] = true + if m.MenuEntries[i].Action == "menu "+name { + return errors.New("A menu shall not loop on itself") + } + } + // Loop test + return nil +} + +func (m *MenuEntry) validate() error { + // Key + if ok := reValidKey.MatchString(m.Key); !ok { + return errors.New("Invalid Key, must be exactly one alphanumeric character and match regex `^\\w$` : " + m.Key) + } + // Label + if len(m.Label) <= 0 { + return errors.New("Invalid Label, cannot be empty") + } + // Action + if err := validateAction(m.Action); err != nil { + return errors.Wrap(err, "Invalid Action in MenuEntry") + } + return nil +} -- cgit v1.2.3