Skip to content

Commit

Permalink
feat: Improve validation messages for coder_parameter (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtojek authored Jan 23, 2023
1 parent 0385749 commit e10b5f4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
32 changes: 16 additions & 16 deletions examples/resources/coder_parameter/resource.tf
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
data "coder_parameter" "example" {
display_name = "Region"
description = "Specify a region to place your workspace."
immutable = true
type = "string"
name = "Region"
description = "Specify a region to place your workspace."
mutable = false
type = "string"
option {
value = "us-central1-a"
label = "US Central"
name = "US Central"
icon = "/icon/usa.svg"
}
option {
value = "asia-central1-a"
label = "Asia"
name = "Asia"
icon = "/icon/asia.svg"
}
}

data "coder_parameter" "ami" {
display_name = "Machine Image"
name = "Machine Image"
option {
value = "ami-xxxxxxxx"
label = "Ubuntu"
name = "Ubuntu"
icon = "/icon/ubuntu.svg"
}
}

data "coder_parameter" "image" {
display_name = "Docker Image"
icon = "/icon/docker.svg"
type = "bool"
data "coder_parameter" "is_public_instance" {
name = "Is public instance?"
icon = "/icon/docker.svg"
type = "bool"
}

data "coder_parameter" "cores" {
display_name = "CPU Cores"
icon = "/icon/"
name = "CPU Cores"
icon = "/icon/"
}

data "coder_parameter" "disk_size" {
display_name = "Disk Size"
type = "number"
name = "Disk Size"
type = "number"
validation {
# This can apply to number and string types.
min = 0
Expand Down
9 changes: 6 additions & 3 deletions provider/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ func (v *Validation) Valid(typ, value string) error {
}
switch typ {
case "bool":
if value != "true" && value != "false" {
return fmt.Errorf(`boolean value can be either "true" or "false"`)
}
return nil
case "string":
if v.Regex == "" {
Expand All @@ -307,13 +310,13 @@ func (v *Validation) Valid(typ, value string) error {
case "number":
num, err := strconv.Atoi(value)
if err != nil {
return fmt.Errorf("parse value %s as int: %s", value, err)
return fmt.Errorf("value %q is not a number", value)
}
if num < v.Min {
return fmt.Errorf("provided value %d is less than the minimum %d", num, v.Min)
return fmt.Errorf("value %d is less than the minimum %d", num, v.Min)
}
if num > v.Max {
return fmt.Errorf("provided value %d is more than the maximum %d", num, v.Max)
return fmt.Errorf("value %d is more than the maximum %d", num, v.Max)
}
}
return nil
Expand Down
9 changes: 7 additions & 2 deletions provider/parameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func TestValueValidatesType(t *testing.T) {
Name: "InvalidNumber",
Type: "number",
Value: "hi",
Error: regexp.MustCompile("parse value hi as int"),
Error: regexp.MustCompile("is not a number"),
}, {
Name: "NumberBelowMin",
Type: "number",
Expand All @@ -307,6 +307,11 @@ func TestValueValidatesType(t *testing.T) {
Value: "1",
Max: 0,
Error: regexp.MustCompile("is more than the maximum"),
}, {
Name: "InvalidBool",
Type: "bool",
Value: "cat",
Error: regexp.MustCompile("boolean value can be either"),
}} {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
Expand All @@ -318,7 +323,7 @@ func TestValueValidatesType(t *testing.T) {
}
err := v.Valid(tc.Type, tc.Value)
if tc.Error != nil {
require.True(t, tc.Error.MatchString(err.Error()))
require.True(t, tc.Error.MatchString(err.Error()), "got: %s", err.Error())
}
})
}
Expand Down

0 comments on commit e10b5f4

Please sign in to comment.