@@ -10,6 +10,7 @@ import (
10
10
"os"
11
11
"regexp"
12
12
"strconv"
13
+ "strings"
13
14
14
15
"github.com/google/uuid"
15
16
"github.com/hashicorp/go-cty/cty"
@@ -437,13 +438,13 @@ func (v *Validation) Valid(typ, value string) error {
437
438
case "number" :
438
439
num , err := strconv .Atoi (value )
439
440
if err != nil {
440
- return validationError (v .Error , "value %q is not a number" , value )
441
+ return takeFirstError (v .errorRendered ( value ), fmt . Errorf ( "value %q is not a number" , value ) )
441
442
}
442
443
if ! v .MinDisabled && num < v .Min {
443
- return validationError (v .Error , "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 ) )
444
445
}
445
446
if ! v .MaxDisabled && num > v .Max {
446
- return validationError (v .Error , "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 ) )
447
448
}
448
449
if v .Monotonic != "" && v .Monotonic != ValidationMonotonicIncreasing && v .Monotonic != ValidationMonotonicDecreasing {
449
450
return fmt .Errorf ("number monotonicity can be either %q or %q" , ValidationMonotonicIncreasing , ValidationMonotonicDecreasing )
@@ -466,9 +467,22 @@ func ParameterEnvironmentVariable(name string) string {
466
467
return "CODER_PARAMETER_" + hex .EncodeToString (sum [:])
467
468
}
468
469
469
- func validationError (customMessage , defaultMessage string , a ... any ) error {
470
- if customMessage != "" {
471
- return fmt .Errorf (customMessage )
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
472
482
}
473
- return fmt .Errorf (defaultMessage , a ... )
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 ))
474
488
}
0 commit comments