|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package dynamicvalidator |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 12 | +) |
| 13 | + |
| 14 | +// AnyWithAllWarnings returns a validator which ensures that any configured |
| 15 | +// attribute value passes at least one of the given validators. This validator |
| 16 | +// returns all warnings, including failed validators. |
| 17 | +// |
| 18 | +// Use Any() to return warnings only from the passing validator. |
| 19 | +func AnyWithAllWarnings(validators ...validator.Dynamic) validator.Dynamic { |
| 20 | + return anyWithAllWarningsValidator{ |
| 21 | + validators: validators, |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +var _ validator.Dynamic = anyWithAllWarningsValidator{} |
| 26 | + |
| 27 | +// anyWithAllWarningsValidator implements the validator. |
| 28 | +type anyWithAllWarningsValidator struct { |
| 29 | + validators []validator.Dynamic |
| 30 | +} |
| 31 | + |
| 32 | +// Description describes the validation in plain text formatting. |
| 33 | +func (v anyWithAllWarningsValidator) Description(ctx context.Context) string { |
| 34 | + var descriptions []string |
| 35 | + |
| 36 | + for _, subValidator := range v.validators { |
| 37 | + descriptions = append(descriptions, subValidator.Description(ctx)) |
| 38 | + } |
| 39 | + |
| 40 | + return fmt.Sprintf("Value must satisfy at least one of the validations: %s", strings.Join(descriptions, " + ")) |
| 41 | +} |
| 42 | + |
| 43 | +// MarkdownDescription describes the validation in Markdown formatting. |
| 44 | +func (v anyWithAllWarningsValidator) MarkdownDescription(ctx context.Context) string { |
| 45 | + return v.Description(ctx) |
| 46 | +} |
| 47 | + |
| 48 | +// ValidateDynamic performs the validation. |
| 49 | +func (v anyWithAllWarningsValidator) ValidateDynamic(ctx context.Context, req validator.DynamicRequest, resp *validator.DynamicResponse) { |
| 50 | + anyValid := false |
| 51 | + |
| 52 | + for _, subValidator := range v.validators { |
| 53 | + validateResp := &validator.DynamicResponse{} |
| 54 | + |
| 55 | + subValidator.ValidateDynamic(ctx, req, validateResp) |
| 56 | + |
| 57 | + if !validateResp.Diagnostics.HasError() { |
| 58 | + anyValid = true |
| 59 | + } |
| 60 | + |
| 61 | + resp.Diagnostics.Append(validateResp.Diagnostics...) |
| 62 | + } |
| 63 | + |
| 64 | + if anyValid { |
| 65 | + resp.Diagnostics = resp.Diagnostics.Warnings() |
| 66 | + } |
| 67 | +} |
0 commit comments