Skip to content

Commit 7f41e4f

Browse files
committed
Relax option/validation conflicts, add tests
Signed-off-by: Danny Kopping <[email protected]>
1 parent ed30ba3 commit 7f41e4f

File tree

2 files changed

+78
-13
lines changed

2 files changed

+78
-13
lines changed

provider/parameter.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,11 @@ func parameterDataSource() *schema.Resource {
231231
},
232232
},
233233
"option": {
234-
Type: schema.TypeList,
235-
Description: "Each \"option\" block defines a value for a user to select from.",
236-
ForceNew: true,
237-
Optional: true,
238-
MaxItems: 64,
239-
ConflictsWith: []string{"validation"},
234+
Type: schema.TypeList,
235+
Description: "Each \"option\" block defines a value for a user to select from.",
236+
ForceNew: true,
237+
Optional: true,
238+
MaxItems: 64,
240239
Elem: &schema.Resource{
241240
Schema: map[string]*schema.Schema{
242241
"name": {
@@ -276,11 +275,10 @@ func parameterDataSource() *schema.Resource {
276275
},
277276
},
278277
"validation": {
279-
Type: schema.TypeList,
280-
MaxItems: 1,
281-
Optional: true,
282-
Description: "Validate the input of a parameter.",
283-
ConflictsWith: []string{"option"},
278+
Type: schema.TypeList,
279+
MaxItems: 1,
280+
Optional: true,
281+
Description: "Validate the input of a parameter.",
284282
Elem: &schema.Resource{
285283
Schema: map[string]*schema.Schema{
286284
"min": {
@@ -410,6 +408,9 @@ func (v *Validation) Valid(typ, value string) error {
410408
if !v.MaxDisabled {
411409
return fmt.Errorf("a max cannot be specified for a %s type", typ)
412410
}
411+
if v.Monotonic != "" {
412+
return fmt.Errorf("monotonic validation can only be specified for number types, not %s types", typ)
413+
}
413414
}
414415
if typ != "string" && v.Regex != "" {
415416
return fmt.Errorf("a regex cannot be specified for a %s type", typ)

provider/parameter_test.go

+66-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestParameter(t *testing.T) {
7373
}
7474
},
7575
}, {
76-
Name: "ValidationWithOptions",
76+
Name: "RegexValidationWithOptions",
7777
Config: `
7878
data "coder_parameter" "region" {
7979
name = "Region"
@@ -88,7 +88,23 @@ func TestParameter(t *testing.T) {
8888
}
8989
}
9090
`,
91-
ExpectError: regexp.MustCompile("conflicts with option"),
91+
ExpectError: regexp.MustCompile("a regex cannot be specified for a number type"),
92+
}, {
93+
Name: "MonotonicValidationWithNonNumberType",
94+
Config: `
95+
data "coder_parameter" "region" {
96+
name = "Region"
97+
type = "string"
98+
option {
99+
name = "1"
100+
value = "1"
101+
}
102+
validation {
103+
monotonic = "increasing"
104+
}
105+
}
106+
`,
107+
ExpectError: regexp.MustCompile("monotonic validation can only be specified for number types, not string types"),
92108
}, {
93109
Name: "ValidationRegexMissingError",
94110
Config: `
@@ -424,6 +440,54 @@ data "coder_parameter" "region" {
424440
require.Equal(t, expected, state.Primary.Attributes[key])
425441
}
426442
},
443+
}, {
444+
Name: "NumberValidation_MonotonicWithOptions",
445+
Config: `
446+
data "coder_parameter" "region" {
447+
name = "Region"
448+
type = "number"
449+
description = <<-EOF
450+
Always pick a larger region.
451+
EOF
452+
default = 1
453+
454+
option {
455+
name = "Small"
456+
value = 1
457+
}
458+
459+
option {
460+
name = "Medium"
461+
value = 2
462+
}
463+
464+
option {
465+
name = "Large"
466+
value = 3
467+
}
468+
469+
validation {
470+
monotonic = "increasing"
471+
}
472+
}
473+
`,
474+
Check: func(state *terraform.ResourceState) {
475+
for key, expected := range map[string]any{
476+
"name": "Region",
477+
"type": "number",
478+
"validation.#": "1",
479+
"option.0.name": "Small",
480+
"option.0.value": "1",
481+
"option.1.name": "Medium",
482+
"option.1.value": "2",
483+
"option.2.name": "Large",
484+
"option.2.value": "3",
485+
"default": "1",
486+
"validation.0.monotonic": "increasing",
487+
} {
488+
require.Equal(t, expected, state.Primary.Attributes[key])
489+
}
490+
},
427491
}, {
428492
Name: "NumberValidation_Min",
429493
Config: `

0 commit comments

Comments
 (0)