@@ -68,64 +68,75 @@ func MapValueLenBetween(min, max int) schema.SchemaValidateDiagFunc {
6868 }
6969}
7070
71- // MapKeyMatch returns a SchemaValidateFunc which tests if the provided value
71+ // MapKeyMatch returns a SchemaValidateDiagFunc which tests if the provided value
7272// is of type map and all keys match a given regexp. Optionally an error message
7373// can be provided to return something friendlier than "expected to match some globby regexp".
74- func MapKeyMatch (r * regexp.Regexp , message string ) schema.SchemaValidateFunc {
75- return func (i interface {}, k string ) (warnings []string , errors []error ) {
76- v , ok := i .(map [string ]interface {})
77- if ! ok {
78- errors = append (errors , fmt .Errorf ("expected type of %[1]q to be Map, got %[1]T" , k ))
79- return warnings , errors
80- }
74+ func MapKeyMatch (r * regexp.Regexp , message string ) schema.SchemaValidateDiagFunc {
75+ return func (v interface {}, path cty.Path ) diag.Diagnostics {
76+ var diags diag.Diagnostics
8177
82- for key := range v {
78+ for _ , key := range sortedKeys ( v .( map [ string ] interface {})) {
8379 if ok := r .MatchString (key ); ! ok {
84- if message != "" {
85- errors = append (errors , fmt .Errorf ("invalid key %q for %q (%s)" , key , k , message ))
86- return warnings , errors
80+ var detail string
81+ if message == "" {
82+ detail = fmt .Sprintf ("Map key expected to match regular expression %q: %s" , r , key )
83+ } else {
84+ detail = fmt .Sprintf ("%s: %s" , message , key )
8785 }
8886
89- errors = append (errors , fmt .Errorf ("invalid key %q for %q (expected to match regular expression %q)" , key , k , r ))
90- return warnings , errors
87+ diags = append (diags , diag.Diagnostic {
88+ Severity : diag .Error ,
89+ Summary : "Invalid map key" ,
90+ Detail : detail ,
91+ AttributePath : append (path , cty.IndexStep {Key : cty .StringVal (key )}),
92+ })
9193 }
9294 }
9395
94- return warnings , errors
96+ return diags
9597 }
9698}
9799
98- // MapValueMatch returns a SchemaValidateFunc which tests if the provided value
100+ // MapValueMatch returns a SchemaValidateDiagFunc which tests if the provided value
99101// is of type map and all values match a given regexp. Optionally an error message
100102// can be provided to return something friendlier than "expected to match some globby regexp".
101- func MapValueMatch (r * regexp.Regexp , message string ) schema.SchemaValidateFunc {
102- return func (i interface {}, k string ) (warnings []string , errors []error ) {
103- v , ok := i .(map [string ]interface {})
104- if ! ok {
105- errors = append (errors , fmt .Errorf ("expected type of %[1]q to be Map, got %[1]T" , k ))
106- return warnings , errors
107- }
103+ func MapValueMatch (r * regexp.Regexp , message string ) schema.SchemaValidateDiagFunc {
104+ return func (v interface {}, path cty.Path ) diag.Diagnostics {
105+ var diags diag.Diagnostics
106+
107+ m := v .(map [string ]interface {})
108+
109+ for _ , key := range sortedKeys (m ) {
110+ val := m [key ]
108111
109- for _ , val := range v {
110112 if _ , ok := val .(string ); ! ok {
111- errors = append (errors , fmt .Errorf ("expected all values of %[1]q to be strings, found %[2]v (type = %[2]T)" , k , val ))
112- return warnings , errors
113+ diags = append (diags , diag.Diagnostic {
114+ Severity : diag .Error ,
115+ Summary : "Bad map value type" ,
116+ Detail : fmt .Sprintf ("Map values should be strings: %s => %v (type = %T)" , key , val , val ),
117+ AttributePath : append (path , cty.IndexStep {Key : cty .StringVal (key )}),
118+ })
119+ continue
113120 }
114- }
115121
116- for _ , val := range v {
117122 if ok := r .MatchString (val .(string )); ! ok {
118- if message != "" {
119- errors = append (errors , fmt .Errorf ("invalid value %q for %q (%s)" , val , k , message ))
120- return warnings , errors
123+ var detail string
124+ if message == "" {
125+ detail = fmt .Sprintf ("Map value expected to match regular expression %q: %s => %v" , r , key , val )
126+ } else {
127+ detail = fmt .Sprintf ("%s: %s => %v" , message , key , val )
121128 }
122129
123- errors = append (errors , fmt .Errorf ("invalid value %q for %q (expected to match regular expression %q)" , val , k , r ))
124- return warnings , errors
130+ diags = append (diags , diag.Diagnostic {
131+ Severity : diag .Error ,
132+ Summary : "Invalid map value" ,
133+ Detail : detail ,
134+ AttributePath : append (path , cty.IndexStep {Key : cty .StringVal (key )}),
135+ })
125136 }
126137 }
127138
128- return warnings , errors
139+ return diags
129140 }
130141}
131142
0 commit comments