-
Notifications
You must be signed in to change notification settings - Fork 10
/
any_with_all_warnings.go
67 lines (51 loc) · 1.89 KB
/
any_with_all_warnings.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
58
59
60
61
62
63
64
65
66
67
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package boolvalidator
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)
// AnyWithAllWarnings returns a validator which ensures that any configured
// attribute value passes at least one of the given validators. This validator
// returns all warnings, including failed validators.
//
// Use Any() to return warnings only from the passing validator.
func AnyWithAllWarnings(validators ...validator.Bool) validator.Bool {
return anyWithAllWarningsValidator{
validators: validators,
}
}
var _ validator.Bool = anyWithAllWarningsValidator{}
// anyWithAllWarningsValidator implements the validator.
type anyWithAllWarningsValidator struct {
validators []validator.Bool
}
// Description describes the validation in plain text formatting.
func (v anyWithAllWarningsValidator) 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 at least one of the validations: %s", strings.Join(descriptions, " + "))
}
// MarkdownDescription describes the validation in Markdown formatting.
func (v anyWithAllWarningsValidator) MarkdownDescription(ctx context.Context) string {
return v.Description(ctx)
}
// ValidateBool performs the validation.
func (v anyWithAllWarningsValidator) ValidateBool(ctx context.Context, req validator.BoolRequest, resp *validator.BoolResponse) {
anyValid := false
for _, subValidator := range v.validators {
validateResp := &validator.BoolResponse{}
subValidator.ValidateBool(ctx, req, validateResp)
if !validateResp.Diagnostics.HasError() {
anyValid = true
}
resp.Diagnostics.Append(validateResp.Diagnostics...)
}
if anyValid {
resp.Diagnostics = resp.Diagnostics.Warnings()
}
}