chore: simplify template validation (#24825)
This commit is contained in:
parent
93ad721845
commit
8319ac1ec6
2 changed files with 12 additions and 17 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue