Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf5dc37c

Browse files
authored
feat: relax error message for number-typed coder parameters (#195)
1 parent9c2e569 commitf5dc37c

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

‎docs/data-sources/parameter.md

Lines changed: 1 addition & 1 deletion
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 valuedoesn't matchtheprovided regex.
60+
-`error` (String) An error message to display if the valuebreaksthevalidation 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

Lines changed: 27 additions & 7 deletions
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
iferr!=nil {
441-
returnfmt.Errorf("value %q is not a number",value)
441+
returntakeFirstError(v.errorRendered(value),fmt.Errorf("value %q is not a number",value))
442442
}
443443
if!v.MinDisabled&&num<v.Min {
444-
returnfmt.Errorf("value %d is less than the minimum %d",num,v.Min)
444+
returntakeFirstError(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-
returnfmt.Errorf("value %d is more than the maximum %d",num,v.Max)
447+
returntakeFirstError(v.errorRendered(value),fmt.Errorf("value %d is more than the maximum %d",num,v.Max))
448448
}
449449
ifv.Monotonic!=""&&v.Monotonic!=ValidationMonotonicIncreasing&&v.Monotonic!=ValidationMonotonicDecreasing {
450450
returnfmt.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+
functakeFirstError(errs...error)error {
471+
for_,err:=rangeerrs {
472+
iferr!=nil {
473+
returnerr
474+
}
475+
}
476+
returnxerrors.Errorf("developer error: error message is not provided")
477+
}
478+
479+
func (v*Validation)errorRendered(valuestring)error {
480+
ifv.Error=="" {
481+
returnnil
482+
}
483+
r:=strings.NewReplacer(
484+
"{min}",fmt.Sprintf("%d",v.Min),
485+
"{max}",fmt.Sprintf("%d",v.Max),
486+
"{value}",value)
487+
returnxerrors.Errorf(r.Replace(v.Error))
488+
}

‎provider/parameter_test.go

Lines changed: 14 additions & 0 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp