-
Notifications
You must be signed in to change notification settings - Fork 13
New package dynamicvalidator
#249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package dynamicvalidator | ||
|
||
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.Dynamic) validator.Dynamic { | ||
return allValidator{ | ||
validators: validators, | ||
} | ||
} | ||
|
||
var _ validator.Dynamic = allValidator{} | ||
|
||
// allValidator implements the validator. | ||
type allValidator struct { | ||
validators []validator.Dynamic | ||
} | ||
|
||
// 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) | ||
} | ||
|
||
// ValidateDynamic performs the validation. | ||
func (v allValidator) ValidateDynamic(ctx context.Context, req validator.DynamicRequest, resp *validator.DynamicResponse) { | ||
for _, subValidator := range v.validators { | ||
validateResp := &validator.DynamicResponse{} | ||
|
||
subValidator.ValidateDynamic(ctx, req, validateResp) | ||
|
||
resp.Diagnostics.Append(validateResp.Diagnostics...) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package dynamicvalidator_test | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework-validators/dynamicvalidator" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
) | ||
|
||
func ExampleAll() { | ||
// Used within a Schema method of a DataSource, Provider, or Resource | ||
_ = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"example_attr": schema.DynamicAttribute{ | ||
Required: true, | ||
Validators: []validator.Dynamic{ | ||
// Validate this Dynamic value must either be: | ||
// - "one" | ||
dynamicvalidator.Any( | ||
dynamicvalidator.OneOf("one"), | ||
Check failure on line 22 in dynamicvalidator/all_example_test.go
|
||
dynamicvalidator.All( | ||
dynamicvalidator.NoneOf("three"), | ||
Check failure on line 24 in dynamicvalidator/all_example_test.go
|
||
), | ||
), | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package dynamicvalidator | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework-validators/internal/schemavalidator" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
) | ||
|
||
// AlsoRequires checks that a set of path.Expression has a non-null value, | ||
// if the current attribute or block also has a non-null value. | ||
// | ||
// This implements the validation logic declaratively within the schema. | ||
// Refer to [datasourcevalidator.RequiredTogether], | ||
// [providervalidator.RequiredTogether], or [resourcevalidator.RequiredTogether] | ||
// for declaring this type of validation outside the schema definition. | ||
// | ||
// Relative path.Expression will be resolved using the attribute or block | ||
// being validated. | ||
func AlsoRequires(expressions ...path.Expression) validator.Dynamic { | ||
return schemavalidator.AlsoRequiresValidator{ | ||
PathExpressions: expressions, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package dynamicvalidator_test | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework-validators/dynamicvalidator" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
) | ||
|
||
func ExampleAlsoRequires() { | ||
// Used within a Schema method of a DataSource, Provider, or Resource | ||
_ = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"example_attr": schema.DynamicAttribute{ | ||
Optional: true, | ||
Validators: []validator.Dynamic{ | ||
// Validate this attribute must be configured with other_attr. | ||
dynamicvalidator.AlsoRequires(path.Expressions{ | ||
path.MatchRoot("other_attr"), | ||
}...), | ||
}, | ||
}, | ||
"other_attr": schema.DynamicAttribute{ | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package dynamicvalidator | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
) | ||
|
||
// Any returns a validator which ensures that any configured attribute value | ||
// passes at least one of the given validators. | ||
// | ||
// To prevent practitioner confusion should non-passing validators have | ||
// conflicting logic, only warnings from the passing validator are returned. | ||
// Use AnyWithAllWarnings() to return warnings from non-passing validators | ||
// as well. | ||
func Any(validators ...validator.Dynamic) validator.Dynamic { | ||
return anyValidator{ | ||
validators: validators, | ||
} | ||
} | ||
|
||
var _ validator.Dynamic = anyValidator{} | ||
|
||
// anyValidator implements the validator. | ||
type anyValidator struct { | ||
validators []validator.Dynamic | ||
} | ||
|
||
// Description describes the validation in plain text formatting. | ||
func (v anyValidator) 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 anyValidator) MarkdownDescription(ctx context.Context) string { | ||
return v.Description(ctx) | ||
} | ||
|
||
// ValidateDynamic performs the validation. | ||
func (v anyValidator) ValidateDynamic(ctx context.Context, req validator.DynamicRequest, resp *validator.DynamicResponse) { | ||
for _, subValidator := range v.validators { | ||
validateResp := &validator.DynamicResponse{} | ||
|
||
subValidator.ValidateDynamic(ctx, req, validateResp) | ||
|
||
if !validateResp.Diagnostics.HasError() { | ||
resp.Diagnostics = validateResp.Diagnostics | ||
|
||
return | ||
} | ||
|
||
resp.Diagnostics.Append(validateResp.Diagnostics...) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package dynamicvalidator_test | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework-validators/dynamicvalidator" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
) | ||
|
||
func ExampleAny() { | ||
// Used within a Schema method of a DataSource, Provider, or Resource | ||
_ = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"example_attr": schema.DynamicAttribute{ | ||
Required: true, | ||
Validators: []validator.Dynamic{ | ||
// Validate this Dynamic value must either be: | ||
// - "one" | ||
// - Length at least 4 characters | ||
dynamicvalidator.Any( | ||
dynamicvalidator.OneOf("one"), | ||
Check failure on line 23 in dynamicvalidator/any_example_test.go
|
||
), | ||
austinvalle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}, | ||
}, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package dynamicvalidator | ||
|
||
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.Dynamic) validator.Dynamic { | ||
return anyWithAllWarningsValidator{ | ||
validators: validators, | ||
} | ||
} | ||
|
||
var _ validator.Dynamic = anyWithAllWarningsValidator{} | ||
|
||
// anyWithAllWarningsValidator implements the validator. | ||
type anyWithAllWarningsValidator struct { | ||
validators []validator.Dynamic | ||
} | ||
|
||
// 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) | ||
} | ||
|
||
// ValidateDynamic performs the validation. | ||
func (v anyWithAllWarningsValidator) ValidateDynamic(ctx context.Context, req validator.DynamicRequest, resp *validator.DynamicResponse) { | ||
anyValid := false | ||
|
||
for _, subValidator := range v.validators { | ||
validateResp := &validator.DynamicResponse{} | ||
|
||
subValidator.ValidateDynamic(ctx, req, validateResp) | ||
|
||
if !validateResp.Diagnostics.HasError() { | ||
anyValid = true | ||
} | ||
|
||
resp.Diagnostics.Append(validateResp.Diagnostics...) | ||
} | ||
|
||
if anyValid { | ||
resp.Diagnostics = resp.Diagnostics.Warnings() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package dynamicvalidator_test | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework-validators/dynamicvalidator" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
) | ||
|
||
func ExampleAnyWithAllWarnings() { | ||
// Used within a Schema method of a DataSource, Provider, or Resource | ||
_ = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"example_attr": schema.DynamicAttribute{ | ||
Required: true, | ||
Validators: []validator.Dynamic{ | ||
// Validate this Dynamic value must either be: | ||
// - "one" | ||
// - Length at least 4 characters | ||
dynamicvalidator.AnyWithAllWarnings( | ||
dynamicvalidator.OneOf("one"), | ||
Check failure on line 23 in dynamicvalidator/any_with_all_warnings_example_test.go
|
||
), | ||
austinvalle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}, | ||
}, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package dynamicvalidator | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework-validators/internal/schemavalidator" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
) | ||
|
||
// AtLeastOneOf checks that of a set of path.Expression, | ||
// including the attribute this validator is applied to, | ||
// at least one has a non-null value. | ||
// | ||
// This implements the validation logic declaratively within the tfsdk.Schema. | ||
// Refer to [datasourcevalidator.AtLeastOneOf], | ||
// [providervalidator.AtLeastOneOf], or [resourcevalidator.AtLeastOneOf] | ||
// for declaring this type of validation outside the schema definition. | ||
// | ||
// Any relative path.Expression will be resolved using the attribute being | ||
// validated. | ||
func AtLeastOneOf(expressions ...path.Expression) validator.Dynamic { | ||
return schemavalidator.AtLeastOneOfValidator{ | ||
PathExpressions: expressions, | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.