Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/util/helm/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Chart struct {
UpgradeCRDs *bool `mapstructure:"upgradeCRDs"`
// ValuesYaml is the values.yaml content.
// use string instead of map[string]interface{}
ValuesYaml string `mapstructure:"values_yaml"`
ValuesYaml string `validate:"omitempty,yaml" mapstructure:"values_yaml"`
}

func (repo *Repo) FillDefaultValue(defaultRepo *Repo) {
Expand Down
20 changes: 18 additions & 2 deletions pkg/util/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"log"

"gopkg.in/yaml.v3"

"github.com/go-playground/validator/v10"
"k8s.io/apimachinery/pkg/util/validation"
)
Expand All @@ -13,8 +15,18 @@ var v *validator.Validate
func init() {
v = validator.New()

if err := v.RegisterValidation("dns1123subdomain", dns1123SubDomain); err != nil {
log.Fatal(err)
validations := []struct {
tag string
fn validator.Func
}{
{"dns1123subdomain", dns1123SubDomain},
{"yaml", isYaml},
}

for _, vt := range validations {
if err := v.RegisterValidation(vt.tag, vt.fn); err != nil {
log.Fatal(err)
}
}
}

Expand All @@ -36,3 +48,7 @@ func StructAllError(s interface{}) error {
func dns1123SubDomain(fl validator.FieldLevel) bool {
return len(validation.IsDNS1123Subdomain(fl.Field().String())) == 0
}

func isYaml(fl validator.FieldLevel) bool {
return yaml.Unmarshal([]byte(fl.Field().String()), &struct{}{}) == nil
}
46 changes: 46 additions & 0 deletions pkg/util/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,49 @@ func Test_dns1123SubDomain(t *testing.T) {
})
}
}

func Test_isYaml(t *testing.T) {
goodValues := `
---
# An employee record
name: Martin D'vloper
foods:
- Apple
languages:
perl: Elite
education: |
4 GCSEs
3 A-Levels
BSc in the Internet of Things
`
badValues := `
---
# An employee record
job: Developer
skill:Elite
foods:
- Apple
- Orange
languages:
perl: Elite
python: Elite
education: ||
4 GCSEs
`
tests := []struct {
name string
fl validator.FieldLevel
want bool
}{
// TODO: Add test cases.
{"base", &FakerFieldLeveler{field: goodValues}, true},
{"base", &FakerFieldLeveler{field: badValues}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isYaml(tt.fl); got != tt.want {
t.Errorf("isYaml() = %v, want %v", got, tt.want)
}
})
}
}