Skip to content

Commit 3a36fbb

Browse files
committed
PR fixes
1 parent 5ef834e commit 3a36fbb

File tree

1 file changed

+25
-35
lines changed

1 file changed

+25
-35
lines changed

provider/parameter.go

+25-35
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ const (
3030
// as unused options do not affect the final parameter value. The default value
3131
// is also ignored, assuming a value is provided.
3232
ValidationModeDefault ValidationMode = ""
33-
// ValidationModeTemplateImport tolerates empty values, as the value might not be
34-
// available at import. It does not tolerate an invalid default or invalid option
35-
// values.
33+
// ValidationModeTemplateImport tolerates empty values as long as no 'validation'
34+
// block is specified. This allows for importing a template without having to
35+
// specify a value for every parameter. It does not tolerate an invalid default
36+
// or invalid option values.
3637
ValidationModeTemplateImport ValidationMode = "template-import"
3738
)
3839

@@ -428,14 +429,6 @@ func (v *Parameter) Valid(input *string, mode ValidationMode) (string, diag.Diag
428429
value = v.Default
429430
}
430431

431-
// TODO: When empty values want to be rejected, uncomment this.
432-
// coder/coder should update to use template import mode first,
433-
// before this is uncommented.
434-
//if value == nil && mode == ValidationModeDefault {
435-
// var empty string
436-
// value = &empty
437-
//}
438-
439432
// optionType might differ from parameter.Type. This is ok, and parameter.Type
440433
// should be used for the value type, and optionType for options.
441434
optionType, v.FormType, err = ValidateFormType(v.Type, len(v.Option), v.FormType)
@@ -477,39 +470,36 @@ func (v *Parameter) Valid(input *string, mode ValidationMode) (string, diag.Diag
477470

478471
// TODO: This is a bit of a hack. The current behavior states if validation
479472
// is given, then apply validation to unset values.
480-
// This should be removed, and all values should be validated. Meaning
481473
// value == nil should not be accepted in the first place.
482-
if len(v.Validation) > 0 && value == nil {
483-
empty := ""
484-
value = &empty
474+
// To fix this, value should be coerced to an empty string
475+
// if it is nil. Then let the validation logic always apply.
476+
if len(v.Validation) == 0 && value == nil {
477+
return "", nil
485478
}
486479

487-
// Value is only validated if it is set. If it is unset, validation
488-
// is skipped.
480+
// forcedValue ensures the value is not-nil.
481+
var forcedValue string
489482
if value != nil {
490-
d := v.validValue(*value, optionType, optionValues, cty.Path{})
491-
if d.HasError() {
492-
return "", d
493-
}
483+
forcedValue = *value
484+
}
494485

495-
err = valueIsType(v.Type, *value)
496-
if err != nil {
497-
return "", diag.Diagnostics{
498-
{
499-
Severity: diag.Error,
500-
Summary: fmt.Sprintf("Parameter value is not of type %q", v.Type),
501-
Detail: err.Error(),
502-
},
503-
}
504-
}
486+
d := v.validValue(forcedValue, optionType, optionValues, cty.Path{})
487+
if d.HasError() {
488+
return "", d
505489
}
506490

507-
if value == nil {
508-
// The previous behavior is to always write an empty string
509-
return "", nil
491+
err = valueIsType(v.Type, forcedValue)
492+
if err != nil {
493+
return "", diag.Diagnostics{
494+
{
495+
Severity: diag.Error,
496+
Summary: fmt.Sprintf("Parameter value is not of type %q", v.Type),
497+
Detail: err.Error(),
498+
},
499+
}
510500
}
511501

512-
return *value, nil
502+
return forcedValue, nil
513503
}
514504

515505
func (v *Parameter) ValidOptions(optionType OptionType, mode ValidationMode) (map[string]struct{}, diag.Diagnostics) {

0 commit comments

Comments
 (0)