Skip to content

Commit

Permalink
Merge pull request getporter#1598 from vdice/fix/param-output-validation
Browse files Browse the repository at this point in the history
fix(manifest.go): fix faulty param/output validation
  • Loading branch information
carolynvs authored May 19, 2021
2 parents 6b68798 + 7effef8 commit 865b030
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
28 changes: 16 additions & 12 deletions pkg/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,14 @@ func (pd *ParameterDefinition) Validate() error {
pdCopy.ContentEncoding = "base64"
}

schemaValidationErrs, err := pdCopy.Schema.Validate(pdCopy)
if err != nil {
result = multierror.Append(result, errors.Wrapf(err, "encountered error while validating parameter %s", pdCopy.Name))
}
for _, schemaValidationErr := range schemaValidationErrs {
result = multierror.Append(result, errors.Wrapf(err, "encountered validation error(s) for parameter %s: %v", pdCopy.Name, schemaValidationErr))
if pdCopy.Default != nil {
schemaValidationErrs, err := pdCopy.Schema.Validate(pdCopy.Default)
if err != nil {
result = multierror.Append(result, errors.Wrapf(err, "encountered error while validating parameter %s", pdCopy.Name))
}
for _, schemaValidationErr := range schemaValidationErrs {
result = multierror.Append(result, fmt.Errorf("encountered an error validating the default value %v for parameter %q: %s", pdCopy.Default, pdCopy.Name, schemaValidationErr.Error))
}
}

return result.ErrorOrNil()
Expand Down Expand Up @@ -696,12 +698,14 @@ func (od *OutputDefinition) Validate() error {
odCopy.ContentEncoding = "base64"
}

schemaValidationErrs, err := odCopy.Schema.Validate(od)
if err != nil {
result = multierror.Append(result, errors.Wrapf(err, "encountered error while validating output %s", odCopy.Name))
}
for _, schemaValidationErr := range schemaValidationErrs {
result = multierror.Append(result, errors.Wrapf(err, "encountered validation error(s) for output %s: %v", odCopy.Name, schemaValidationErr))
if odCopy.Default != nil {
schemaValidationErrs, err := odCopy.Schema.Validate(odCopy.Default)
if err != nil {
result = multierror.Append(result, errors.Wrapf(err, "encountered error while validating output %s", odCopy.Name))
}
for _, schemaValidationErr := range schemaValidationErrs {
result = multierror.Append(result, fmt.Errorf("encountered an error validating the default value %v for output %q: %s", odCopy.Default, odCopy.Name, schemaValidationErr.Error))
}
}

return result.ErrorOrNil()
Expand Down
36 changes: 34 additions & 2 deletions pkg/manifest/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func TestMixinDeclaration_MarshalYAML(t *testing.T) {
assert.Equal(t, string(wantYaml), string(gotYaml))
}

func TestValidateParameterDefinition(t *testing.T) {
func TestValidateParameterDefinition_missingPath(t *testing.T) {
pd := ParameterDefinition{
Name: "myparam",
Schema: definition.Schema{
Expand All @@ -456,7 +456,23 @@ func TestValidateParameterDefinition(t *testing.T) {
assert.NoError(t, err)
}

func TestValidateOutputDefinition(t *testing.T) {
func TestValidateParameterDefinition_defaultFailsValidation(t *testing.T) {
pd := ParameterDefinition{
Name: "myparam",
Schema: definition.Schema{
Type: "string",
Default: 1,
},
}

err := pd.Validate()
assert.EqualError(t, err, `1 error occurred:
* encountered an error validating the default value 1 for parameter "myparam": type should be string, got integer
`)
}

func TestValidateOutputDefinition_missingPath(t *testing.T) {
od := OutputDefinition{
Name: "myoutput",
Schema: definition.Schema{
Expand All @@ -476,6 +492,22 @@ func TestValidateOutputDefinition(t *testing.T) {
assert.NoError(t, err)
}

func TestValidateOutputDefinition_defaultFailsValidation(t *testing.T) {
od := OutputDefinition{
Name: "myoutput",
Schema: definition.Schema{
Type: "string",
Default: 1,
},
}

err := od.Validate()
assert.EqualError(t, err, `1 error occurred:
* encountered an error validating the default value 1 for output "myoutput": type should be string, got integer
`)
}

func TestValidateImageMap(t *testing.T) {
t.Run("with both valid image digest and valid repository format", func(t *testing.T) {
mi := MappedImage{
Expand Down

0 comments on commit 865b030

Please sign in to comment.