Skip to content

Commit 8ad4fad

Browse files
akinrosslhercot
authored andcommitted
[ignore] add path check to avoid index errors for make string required validation
1 parent 3e11e79 commit 8ad4fad

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

internal/provider/utils.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,23 @@ func (v MakeStringRequiredValidator) ValidateString(ctx context.Context, req val
219219
return
220220
}
221221

222-
setName := req.Path.String()[0:strings.Index(req.Path.String(), "[")]
223-
attributeName := req.Path.String()[strings.Index(req.Path.String(), "]")+2:]
224-
225-
resp.Diagnostics.AddAttributeError(
226-
req.Path,
227-
"Incorrect attribute value type",
228-
fmt.Sprintf("Inappropriate value for attribute \"%s\": attribute \"%s\" is required.", setName, attributeName),
229-
)
222+
path := req.Path.String()
223+
if strings.Contains(path, "[") {
224+
setName := path[0:strings.Index(path, "[")]
225+
attributeName := path[strings.Index(path, "]")+2:]
226+
resp.Diagnostics.AddAttributeError(
227+
req.Path,
228+
"Incorrect attribute value type",
229+
fmt.Sprintf("Inappropriate value for attribute \"%s\": attribute \"%s\" is required.", setName, attributeName),
230+
)
231+
} else {
232+
resp.Diagnostics.AddAttributeError(
233+
req.Path,
234+
"Incorrect attribute value type",
235+
fmt.Sprintf("Attribute is required for path: %s", path),
236+
)
237+
}
238+
230239
}
231240

232241
// StringNotNull returns an validator which ensures that the string attribute is

0 commit comments

Comments
 (0)