Skip to content

Commit e10b5f4

Browse files
authored
feat: Improve validation messages for coder_parameter (#85)
1 parent 0385749 commit e10b5f4

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

examples/resources/coder_parameter/resource.tf

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
data "coder_parameter" "example" {
2-
display_name = "Region"
3-
description = "Specify a region to place your workspace."
4-
immutable = true
5-
type = "string"
2+
name = "Region"
3+
description = "Specify a region to place your workspace."
4+
mutable = false
5+
type = "string"
66
option {
77
value = "us-central1-a"
8-
label = "US Central"
8+
name = "US Central"
99
icon = "/icon/usa.svg"
1010
}
1111
option {
1212
value = "asia-central1-a"
13-
label = "Asia"
13+
name = "Asia"
1414
icon = "/icon/asia.svg"
1515
}
1616
}
1717

1818
data "coder_parameter" "ami" {
19-
display_name = "Machine Image"
19+
name = "Machine Image"
2020
option {
2121
value = "ami-xxxxxxxx"
22-
label = "Ubuntu"
22+
name = "Ubuntu"
2323
icon = "/icon/ubuntu.svg"
2424
}
2525
}
2626

27-
data "coder_parameter" "image" {
28-
display_name = "Docker Image"
29-
icon = "/icon/docker.svg"
30-
type = "bool"
27+
data "coder_parameter" "is_public_instance" {
28+
name = "Is public instance?"
29+
icon = "/icon/docker.svg"
30+
type = "bool"
3131
}
3232

3333
data "coder_parameter" "cores" {
34-
display_name = "CPU Cores"
35-
icon = "/icon/"
34+
name = "CPU Cores"
35+
icon = "/icon/"
3636
}
3737

3838
data "coder_parameter" "disk_size" {
39-
display_name = "Disk Size"
40-
type = "number"
39+
name = "Disk Size"
40+
type = "number"
4141
validation {
4242
# This can apply to number and string types.
4343
min = 0

provider/parameter.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ func (v *Validation) Valid(typ, value string) error {
288288
}
289289
switch typ {
290290
case "bool":
291+
if value != "true" && value != "false" {
292+
return fmt.Errorf(`boolean value can be either "true" or "false"`)
293+
}
291294
return nil
292295
case "string":
293296
if v.Regex == "" {
@@ -307,13 +310,13 @@ func (v *Validation) Valid(typ, value string) error {
307310
case "number":
308311
num, err := strconv.Atoi(value)
309312
if err != nil {
310-
return fmt.Errorf("parse value %s as int: %s", value, err)
313+
return fmt.Errorf("value %q is not a number", value)
311314
}
312315
if num < v.Min {
313-
return fmt.Errorf("provided value %d is less than the minimum %d", num, v.Min)
316+
return fmt.Errorf("value %d is less than the minimum %d", num, v.Min)
314317
}
315318
if num > v.Max {
316-
return fmt.Errorf("provided value %d is more than the maximum %d", num, v.Max)
319+
return fmt.Errorf("value %d is more than the maximum %d", num, v.Max)
317320
}
318321
}
319322
return nil

provider/parameter_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ func TestValueValidatesType(t *testing.T) {
294294
Name: "InvalidNumber",
295295
Type: "number",
296296
Value: "hi",
297-
Error: regexp.MustCompile("parse value hi as int"),
297+
Error: regexp.MustCompile("is not a number"),
298298
}, {
299299
Name: "NumberBelowMin",
300300
Type: "number",
@@ -307,6 +307,11 @@ func TestValueValidatesType(t *testing.T) {
307307
Value: "1",
308308
Max: 0,
309309
Error: regexp.MustCompile("is more than the maximum"),
310+
}, {
311+
Name: "InvalidBool",
312+
Type: "bool",
313+
Value: "cat",
314+
Error: regexp.MustCompile("boolean value can be either"),
310315
}} {
311316
tc := tc
312317
t.Run(tc.Name, func(t *testing.T) {
@@ -318,7 +323,7 @@ func TestValueValidatesType(t *testing.T) {
318323
}
319324
err := v.Valid(tc.Type, tc.Value)
320325
if tc.Error != nil {
321-
require.True(t, tc.Error.MatchString(err.Error()))
326+
require.True(t, tc.Error.MatchString(err.Error()), "got: %s", err.Error())
322327
}
323328
})
324329
}

0 commit comments

Comments
 (0)