From 8319ac1ec65a676cfd6f51623367b8ab51f996af Mon Sep 17 00:00:00 2001 From: andig Date: Tue, 28 Oct 2025 16:08:48 +0100 Subject: [PATCH] chore: simplify template validation (#24825) --- util/templates/init.go | 15 +++++---------- util/templates/template.go | 14 +++++++------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/util/templates/init.go b/util/templates/init.go index 5db5b5570..91b6e54b7 100644 --- a/util/templates/init.go +++ b/util/templates/init.go @@ -77,18 +77,13 @@ func fromBytes(b []byte) (Template, error) { TemplateDefinition: definition, } - err := tmpl.ResolvePresets() - if err == nil { - err = tmpl.ResolveGroup() - } - if err == nil { - err = tmpl.UpdateParamsWithDefaults() - } - if err == nil { - err = tmpl.Validate() + for _, f := range []func() error{tmpl.ResolvePresets, tmpl.ResolveGroup, tmpl.UpdateParamsWithDefaults, tmpl.Validate} { + if err := f(); err != nil { + return tmpl, fmt.Errorf("template '%s': %w", tmpl.Template, err) + } } - return tmpl, err + return tmpl, nil } func load(class Class) { diff --git a/util/templates/template.go b/util/templates/template.go index b2c688ebe..c5301a4dd 100644 --- a/util/templates/template.go +++ b/util/templates/template.go @@ -41,19 +41,19 @@ func (t *Template) UpdateParamsWithDefaults() error { func (t *Template) Validate() error { for _, c := range t.Capabilities { if !slices.Contains(ValidCapabilities, c) { - return fmt.Errorf("invalid capability '%s' in template %s", c, t.Template) + return fmt.Errorf("invalid capability: '%s'", c) } } for _, c := range t.Countries { if !c.IsValid() { - return fmt.Errorf("invalid country code '%s' in template %s", c, t.Template) + return fmt.Errorf("invalid country code: '%s'", c) } } for _, r := range t.Requirements.EVCC { if !slices.Contains(ValidRequirements, r) { - return fmt.Errorf("invalid requirement '%s' in template %s", r, t.Template) + return fmt.Errorf("invalid requirement: '%s'", r) } } @@ -63,26 +63,26 @@ func (t *Template) Validate() error { } if p.Description.String("en") == "" || p.Description.String("de") == "" { - return fmt.Errorf("description for param %s cant be empty in template %s", p.Name, t.Template) + return fmt.Errorf("param %s: description can't be empty", p.Name) } maxLength := 50 actualLength := max(len(p.Description.String("en")), len(p.Description.String("de"))) if actualLength > maxLength { - return fmt.Errorf("description for param %s is too long in template %s. allowed: %d. actual length: %d. use help field for details instead.", p.Name, t.Template, maxLength, actualLength) + return fmt.Errorf("param %s: description too long (%d/%d allowed)- use help instead", p.Name, actualLength, maxLength) } switch p.Name { case ParamUsage: for _, c := range p.Choice { if !slices.Contains(UsageStrings(), c) { - return fmt.Errorf("invalid usage choice '%s' in template %s", c, t.Template) + return fmt.Errorf("invalid usage: '%s'", c) } } case ParamModbus: for _, c := range p.Choice { if !slices.Contains(ValidModbusChoices, c) { - return fmt.Errorf("invalid modbus choice '%s' in template %s", c, t.Template) + return fmt.Errorf("invalid modbus type: '%s'", c) } } }