Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit af7f27f

Browse files
committedFeb 16, 2024
First steps
1 parent 7815596 commit af7f27f

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed
 

‎provider/parameter.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,9 @@ func parameterDataSource() *schema.Resource {
314314
Optional: true,
315315
},
316316
"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.",
317+
Type: schema.TypeString,
318+
Optional: true,
319+
Description: "An error message to display if the value breaks the validation rules.",
321320
},
322321
},
323322
},
@@ -438,16 +437,16 @@ func (v *Validation) Valid(typ, value string) error {
438437
case "number":
439438
num, err := strconv.Atoi(value)
440439
if err != nil {
441-
return fmt.Errorf("value %q is not a number", value)
440+
return validationError(v.Error, "value %q is not a number", value)
442441
}
443442
if !v.MinDisabled && num < v.Min {
444-
return fmt.Errorf("value %d is less than the minimum %d", num, v.Min)
443+
return validationError(v.Error, "value %d is less than the minimum %d", num, v.Min)
445444
}
446445
if !v.MaxDisabled && num > v.Max {
447-
return fmt.Errorf("value %d is more than the maximum %d", num, v.Max)
446+
return validationError(v.Error, "value %d is more than the maximum %d", num, v.Max)
448447
}
449448
if v.Monotonic != "" && v.Monotonic != ValidationMonotonicIncreasing && v.Monotonic != ValidationMonotonicDecreasing {
450-
return fmt.Errorf("number monotonicity can be either %q or %q", ValidationMonotonicIncreasing, ValidationMonotonicDecreasing)
449+
return validationError(v.Error, "number monotonicity can be either %q or %q", ValidationMonotonicIncreasing, ValidationMonotonicDecreasing)
451450
}
452451
case "list(string)":
453452
var listOfStrings []string
@@ -466,3 +465,10 @@ func ParameterEnvironmentVariable(name string) string {
466465
sum := sha256.Sum256([]byte(name))
467466
return "CODER_PARAMETER_" + hex.EncodeToString(sum[:])
468467
}
468+
469+
func validationError(customMessage, defaultMessage string, a ...any) error {
470+
if customMessage != "" {
471+
return fmt.Errorf(customMessage)
472+
}
473+
return fmt.Errorf(defaultMessage, a...)
474+
}

0 commit comments

Comments
 (0)
Please sign in to comment.