@@ -10,7 +10,7 @@ import (
1010 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1111)
1212
13- // MapKeyLenBetween returns a SchemaValidateFunc which tests if the provided value
13+ // MapKeyLenBetween returns a SchemaValidateDiagFunc which tests if the provided value
1414// is of type map and the length of all keys are between min and max (inclusive)
1515func MapKeyLenBetween (min , max int ) schema.SchemaValidateDiagFunc {
1616 return func (v interface {}, path cty.Path ) diag.Diagnostics {
@@ -21,8 +21,8 @@ func MapKeyLenBetween(min, max int) schema.SchemaValidateDiagFunc {
2121 if len < min || len > max {
2222 diags = append (diags , diag.Diagnostic {
2323 Severity : diag .Error ,
24- Summary : "Bad key length" ,
25- Detail : fmt .Sprintf ("Key length should be in the range (%d - %d): %s (length = %d)" , min , max , key , len ),
24+ Summary : "Bad map key length" ,
25+ Detail : fmt .Sprintf ("Map key lengths should be in the range (%d - %d): %s (length = %d)" , min , max , key , len ),
2626 AttributePath : append (path , cty.IndexStep {Key : cty .StringVal (key )}),
2727 })
2828 }
@@ -32,32 +32,39 @@ func MapKeyLenBetween(min, max int) schema.SchemaValidateDiagFunc {
3232 }
3333}
3434
35- // MapValueLenBetween returns a SchemaValidateFunc which tests if the provided value
35+ // MapValueLenBetween returns a SchemaValidateDiagFunc which tests if the provided value
3636// is of type map and the length of all values are between min and max (inclusive)
37- func MapValueLenBetween (min , max int ) schema.SchemaValidateFunc {
38- return func (i interface {}, k string ) (warnings []string , errors []error ) {
39- v , ok := i .(map [string ]interface {})
40- if ! ok {
41- errors = append (errors , fmt .Errorf ("expected type of %[1]q to be Map, got %[1]T" , k ))
42- return warnings , errors
43- }
37+ func MapValueLenBetween (min , max int ) schema.SchemaValidateDiagFunc {
38+ return func (v interface {}, path cty.Path ) diag.Diagnostics {
39+ var diags diag.Diagnostics
40+
41+ m := v .(map [string ]interface {})
42+
43+ for _ , key := range sortedKeys (m ) {
44+ val := m [key ]
4445
45- for _ , val := range v {
4646 if _ , ok := val .(string ); ! ok {
47- errors = append (errors , fmt .Errorf ("expected all values of %[1]q to be strings, found %[2]v (type = %[2]T)" , k , val ))
48- return warnings , errors
47+ diags = append (diags , diag.Diagnostic {
48+ Severity : diag .Error ,
49+ Summary : "Bad map value type" ,
50+ Detail : fmt .Sprintf ("Map values should be strings: %s => %v (type = %T)" , key , val , val ),
51+ AttributePath : append (path , cty.IndexStep {Key : cty .StringVal (key )}),
52+ })
53+ continue
4954 }
50- }
5155
52- for _ , val := range v {
5356 len := len (val .(string ))
5457 if len < min || len > max {
55- errors = append (errors , fmt .Errorf ("expected the length of all values of %q to be in the range (%d - %d), got %q (length = %d)" , k , min , max , val , len ))
56- return warnings , errors
58+ diags = append (diags , diag.Diagnostic {
59+ Severity : diag .Error ,
60+ Summary : "Bad map value length" ,
61+ Detail : fmt .Sprintf ("Map value lengths should be in the range (%d - %d): %s => %v (length = %d)" , min , max , key , val , len ),
62+ AttributePath : append (path , cty.IndexStep {Key : cty .StringVal (key )}),
63+ })
5764 }
5865 }
5966
60- return warnings , errors
67+ return diags
6168 }
6269}
6370
0 commit comments