-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExactlyOneChild.go
53 lines (45 loc) · 1.57 KB
/
ExactlyOneChild.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
package validators
import (
"context"
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)
var _ validator.Object = exactlyOneChild{}
// exactlyOneChild validates if the provided value is of type string and can be parsed as JSON.
type exactlyOneChild struct {
}
func (validator exactlyOneChild) ValidateObject(ctx context.Context, req validator.ObjectRequest, resp *validator.ObjectResponse) {
// Only validate the attribute configuration value if it is known.
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
return
}
defined := make(map[string]bool)
count := 0
for key, attr := range req.ConfigValue.Attributes() {
if attr.IsUnknown() || attr.IsNull() {
continue
}
defined[key] = true
count++
}
if count != 1 {
resp.Diagnostics.Append(validatordiag.InvalidAttributeTypeDiagnostic(
req.Path,
validator.MarkdownDescription(ctx),
req.ConfigValue.String(),
))
}
}
func (validator exactlyOneChild) Description(ctx context.Context) string {
return "value must have exactly one child attribute defined"
}
func (validator exactlyOneChild) MarkdownDescription(ctx context.Context) string {
return validator.Description(ctx)
}
// ExactlyOneChild returns an AttributeValidator which ensures that any configured
// attribute object has only one child attribute.
// Null (unconfigured) and unknown values are skipped.
func ExactlyOneChild() validator.Object {
return exactlyOneChild{}
}