@@ -30,9 +30,10 @@ const (
30
30
// as unused options do not affect the final parameter value. The default value
31
31
// is also ignored, assuming a value is provided.
32
32
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.
36
37
ValidationModeTemplateImport ValidationMode = "template-import"
37
38
)
38
39
@@ -428,14 +429,6 @@ func (v *Parameter) Valid(input *string, mode ValidationMode) (string, diag.Diag
428
429
value = v .Default
429
430
}
430
431
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
-
439
432
// optionType might differ from parameter.Type. This is ok, and parameter.Type
440
433
// should be used for the value type, and optionType for options.
441
434
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
477
470
478
471
// TODO: This is a bit of a hack. The current behavior states if validation
479
472
// is given, then apply validation to unset values.
480
- // This should be removed, and all values should be validated. Meaning
481
473
// 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
485
478
}
486
479
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
489
482
if value != nil {
490
- d := v .validValue (* value , optionType , optionValues , cty.Path {})
491
- if d .HasError () {
492
- return "" , d
493
- }
483
+ forcedValue = * value
484
+ }
494
485
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
505
489
}
506
490
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
+ }
510
500
}
511
501
512
- return * value , nil
502
+ return forcedValue , nil
513
503
}
514
504
515
505
func (v * Parameter ) ValidOptions (optionType OptionType , mode ValidationMode ) (map [string ]struct {}, diag.Diagnostics ) {
0 commit comments