/
all.go
57 lines (44 loc) · 1.61 KB
/
all.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
54
55
56
57
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package setvalidator
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)
// All returns a validator which ensures that any configured attribute value
// attribute value validates against all the given validators.
//
// Use of All is only necessary when used in conjunction with Any or AnyWithAllWarnings
// as the Validators field automatically applies a logical AND.
func All(validators ...validator.Set) validator.Set {
return allValidator{
validators: validators,
}
}
var _ validator.Set = allValidator{}
// allValidator implements the validator.
type allValidator struct {
validators []validator.Set
}
// Description describes the validation in plain text formatting.
func (v allValidator) Description(ctx context.Context) string {
var descriptions []string
for _, subValidator := range v.validators {
descriptions = append(descriptions, subValidator.Description(ctx))
}
return fmt.Sprintf("Value must satisfy all of the validations: %s", strings.Join(descriptions, " + "))
}
// MarkdownDescription describes the validation in Markdown formatting.
func (v allValidator) MarkdownDescription(ctx context.Context) string {
return v.Description(ctx)
}
// ValidateSet performs the validation.
func (v allValidator) ValidateSet(ctx context.Context, req validator.SetRequest, resp *validator.SetResponse) {
for _, subValidator := range v.validators {
validateResp := &validator.SetResponse{}
subValidator.ValidateSet(ctx, req, validateResp)
resp.Diagnostics.Append(validateResp.Diagnostics...)
}
}