Skip to content

Commit

Permalink
Merge pull request #262 from carolynvs/skip-validate-optional-empty-p…
Browse files Browse the repository at this point in the history
…arameters

Do not validate unspecified optional paramters
  • Loading branch information
carolynvs committed Jun 14, 2021
2 parents c9aabe2 + d618ea7 commit 9f2498b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
26 changes: 16 additions & 10 deletions bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,23 @@ func ValuesOrDefaults(vals map[string]interface{}, b *Bundle, action string) (ma
uncoerced = s.Default
}

// Validate the selection
valErrs, err := s.Validate(uncoerced)
if err != nil {
return res, pkgErrors.Wrapf(err, "encountered an error validating parameter %s", name)
}
// This interface returns a single error. Validation can have multiple errors. For now return the first
// We should update this later.
if len(valErrs) > 0 {
valErr := valErrs[0]
return res, fmt.Errorf("cannot use value: %v as parameter %s: %s", uncoerced, name, valErr.Error)
// Only collect defaults and specified parameters. Unspecified optional parameters without defaults should not be validated.
if param.Required || uncoerced != nil {
// Validate the selection
valErrs, err := s.Validate(uncoerced)
if err != nil {
return res, pkgErrors.Wrapf(err, "encountered an error validating parameter %s", name)
}
// This interface returns a single error. Validation can have multiple errors. For now return the first
// We should update this later.
if len(valErrs) > 0 {
valErr := valErrs[0]
return res, fmt.Errorf("cannot use value: %v as parameter %s: %s", uncoerced, name, valErr.Error)
}
}

// Per the spec: "If no value is provided and default is unset, the runtime MUST set the value to an empty string (""), regardless of type",
// that is why this line is called for every parameter.
res[name] = s.CoerceValue(uncoerced)
}
return res, nil
Expand Down
9 changes: 9 additions & 0 deletions bundle/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ func TestValuesOrDefaults(t *testing.T) {
Type: "boolean",
Default: false,
},
"msg": {
Type: "string",
// msg has no default, but it is optional
// Make sure that we are not validating optional unspecified parameters
},
},
Parameters: map[string]Parameter{
"port": {
Expand All @@ -142,6 +147,9 @@ func TestValuesOrDefaults(t *testing.T) {
"replicaCount": {
Definition: "replicaCountType",
},
"msg": {
Definition: "msg",
},
},
}

Expand All @@ -152,6 +160,7 @@ func TestValuesOrDefaults(t *testing.T) {
is.Equal(vod["host"].(string), "localhost")
is.Equal(vod["port"].(int), 8080)
is.Equal(vod["replicaCount"].(int), 3)
is.Equal(nil, vod["msg"], "msg", "msg should be passed even though it had no default and wasn't set because the spec requires it")

// This should err out because of type problem
vals["replicaCount"] = "banana"
Expand Down

0 comments on commit 9f2498b

Please sign in to comment.