Skip to content

Commit f5dc37c

Browse files
authored
feat: relax error message for number-typed coder parameters (#195)
1 parent 9c2e569 commit f5dc37c

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

docs/data-sources/parameter.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Optional:
5757

5858
Optional:
5959

60-
- `error` (String) An error message to display if the value doesn't match the provided regex.
60+
- `error` (String) An error message to display if the value breaks the validation rules. The following placeholders are supported: {max}, {min}, and {value}.
6161
- `max` (Number) The maximum of a number parameter.
6262
- `min` (Number) The minimum of a number parameter.
6363
- `monotonic` (String) Number monotonicity, either increasing or decreasing.

provider/parameter.go

+27-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"regexp"
1212
"strconv"
13+
"strings"
1314

1415
"github.com/google/uuid"
1516
"github.com/hashicorp/go-cty/cty"
@@ -314,10 +315,9 @@ func parameterDataSource() *schema.Resource {
314315
Optional: true,
315316
},
316317
"error": {
317-
Type: schema.TypeString,
318-
Optional: true,
319-
RequiredWith: []string{"validation.0.regex"},
320-
Description: "An error message to display if the value doesn't match the provided regex.",
318+
Type: schema.TypeString,
319+
Optional: true,
320+
Description: "An error message to display if the value breaks the validation rules. The following placeholders are supported: {max}, {min}, and {value}.",
321321
},
322322
},
323323
},
@@ -438,13 +438,13 @@ func (v *Validation) Valid(typ, value string) error {
438438
case "number":
439439
num, err := strconv.Atoi(value)
440440
if err != nil {
441-
return fmt.Errorf("value %q is not a number", value)
441+
return takeFirstError(v.errorRendered(value), fmt.Errorf("value %q is not a number", value))
442442
}
443443
if !v.MinDisabled && num < v.Min {
444-
return fmt.Errorf("value %d is less than the minimum %d", num, v.Min)
444+
return takeFirstError(v.errorRendered(value), fmt.Errorf("value %d is less than the minimum %d", num, v.Min))
445445
}
446446
if !v.MaxDisabled && num > v.Max {
447-
return fmt.Errorf("value %d is more than the maximum %d", num, v.Max)
447+
return takeFirstError(v.errorRendered(value), fmt.Errorf("value %d is more than the maximum %d", num, v.Max))
448448
}
449449
if v.Monotonic != "" && v.Monotonic != ValidationMonotonicIncreasing && v.Monotonic != ValidationMonotonicDecreasing {
450450
return fmt.Errorf("number monotonicity can be either %q or %q", ValidationMonotonicIncreasing, ValidationMonotonicDecreasing)
@@ -466,3 +466,23 @@ func ParameterEnvironmentVariable(name string) string {
466466
sum := sha256.Sum256([]byte(name))
467467
return "CODER_PARAMETER_" + hex.EncodeToString(sum[:])
468468
}
469+
470+
func takeFirstError(errs ...error) error {
471+
for _, err := range errs {
472+
if err != nil {
473+
return err
474+
}
475+
}
476+
return xerrors.Errorf("developer error: error message is not provided")
477+
}
478+
479+
func (v *Validation) errorRendered(value string) error {
480+
if v.Error == "" {
481+
return nil
482+
}
483+
r := strings.NewReplacer(
484+
"{min}", fmt.Sprintf("%d", v.Min),
485+
"{max}", fmt.Sprintf("%d", v.Max),
486+
"{value}", value)
487+
return xerrors.Errorf(r.Replace(v.Error))
488+
}

provider/parameter_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,20 @@ data "coder_parameter" "region" {
527527
}
528528
`,
529529
ExpectError: regexp.MustCompile("is more than the maximum"),
530+
}, {
531+
Name: "NumberValidation_CustomError",
532+
Config: `
533+
data "coder_parameter" "region" {
534+
name = "Region"
535+
type = "number"
536+
default = 5
537+
validation {
538+
max = 3
539+
error = "foobar"
540+
}
541+
}
542+
`,
543+
ExpectError: regexp.MustCompile("foobar"),
530544
}, {
531545
Name: "NumberValidation_NotInRange",
532546
Config: `

0 commit comments

Comments
 (0)