Return multiple errors in template validation

dev
Alexey Zhuchkov 2021-10-20 23:24:11 +03:00
parent 5d0f6b2622
commit ff7a5997a2
2 changed files with 22 additions and 7 deletions

View File

@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"regexp"
"strings"
"gopkg.in/yaml.v2"
@ -74,20 +75,24 @@ func isTemplateInfoMetadataMatch(tagFilter *filter.TagFilter, templateInfo *mode
func validateTemplateFields(template *templates.Template) error {
info := template.Info
var errors []string
if utils.IsBlank(info.Name) {
return fmt.Errorf(mandatoryFieldMissingTemplate, "name")
errors = append(errors, fmt.Sprintf(mandatoryFieldMissingTemplate, "name"))
}
if info.Authors.IsEmpty() {
return fmt.Errorf(mandatoryFieldMissingTemplate, "author")
errors = append(errors, fmt.Sprintf(mandatoryFieldMissingTemplate, "author"))
}
if template.ID == "" {
return fmt.Errorf(mandatoryFieldMissingTemplate, "id")
errors = append(errors, fmt.Sprintf(mandatoryFieldMissingTemplate, "id"))
} else if !templateIDRegexp.MatchString(template.ID) {
errors = append(errors, fmt.Sprintf(invalidFieldFormatTemplate, "id", templateIDRegexp.String()))
}
if !templateIDRegexp.MatchString(template.ID) {
return fmt.Errorf(invalidFieldFormatTemplate, "id", templateIDRegexp.String())
if len(errors) > 0 {
return fmt.Errorf(strings.Join(errors, ", "))
}
return nil

View File

@ -34,9 +34,19 @@ func TestLoadTemplate(t *testing.T) {
},
},
{
name: "missingName",
name: "emptyTemplate",
template: &templates.Template{},
expectedErr: errors.New("mandatory 'name' field is missing"),
expectedErr: errors.New("mandatory 'name' field is missing, mandatory 'author' field is missing, mandatory 'id' field is missing"),
},
{
name: "emptyNameWithInvalidID",
template: &templates.Template{
ID: "invalid id",
Info: model.Info{
Authors: stringslice.StringSlice{Value: "Author"},
},
},
expectedErr: errors.New("mandatory 'name' field is missing, invalid field format for 'id' (allowed format is ^([a-zA-Z0-9]+[-_])*[a-zA-Z0-9]+$)"),
},
}