@@ -314,10 +314,9 @@ func parameterDataSource() *schema.Resource {
314
314
Optional : true ,
315
315
},
316
316
"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." ,
321
320
},
322
321
},
323
322
},
@@ -438,16 +437,16 @@ func (v *Validation) Valid(typ, value string) error {
438
437
case "number" :
439
438
num , err := strconv .Atoi (value )
440
439
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 )
442
441
}
443
442
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 )
445
444
}
446
445
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 )
448
447
}
449
448
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 )
451
450
}
452
451
case "list(string)" :
453
452
var listOfStrings []string
@@ -466,3 +465,10 @@ func ParameterEnvironmentVariable(name string) string {
466
465
sum := sha256 .Sum256 ([]byte (name ))
467
466
return "CODER_PARAMETER_" + hex .EncodeToString (sum [:])
468
467
}
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