chore: simplify template validation (#24825)

This commit is contained in:
andig 2025-10-28 16:08:48 +01:00 • committed by GitHub
parent 93ad721845
commit 8319ac1ec6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 17 deletions

View file

@ -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) {

View file

@ -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)
}
}
}