Skip to content
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

Add All, Any, and AnyWithAllWarnings validators to boolvalidator, providervalidator, resourcevalidator and datasourcevalidator packages #158

Merged
merged 9 commits into from
Aug 21, 2023
5 changes: 5 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20230818-185606.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: ENHANCEMENTS
body: 'boolvalidator: Added `All`, `Any`, and `AnyWithAllWarnings` validators'
time: 2023-08-18T18:56:06.051472-04:00
custom:
Issue: "158"
5 changes: 5 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20230818-185653.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: ENHANCEMENTS
body: 'datasourcevalidator: Added `All`, `Any`, and `AnyWithAllWarnings` validators'
time: 2023-08-18T18:56:53.809569-04:00
custom:
Issue: "158"
5 changes: 5 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20230818-185720.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: ENHANCEMENTS
body: 'providervalidator: Added `All`, `Any`, and `AnyWithAllWarnings` validators'
time: 2023-08-18T18:57:20.9318-04:00
custom:
Issue: "158"
5 changes: 5 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20230818-185806.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: ENHANCEMENTS
body: 'resourcevalidator: Added `All`, `Any`, and `AnyWithAllWarnings` validators'
time: 2023-08-18T18:58:06.709077-04:00
custom:
Issue: "158"
57 changes: 57 additions & 0 deletions boolvalidator/all.go
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 boolvalidator

import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

// All returns a validator which ensures that any configured 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.Bool) validator.Bool {
return allValidator{
validators: validators,
}
}

var _ validator.Bool = allValidator{}

// allValidator implements the validator.
type allValidator struct {
validators []validator.Bool
}

// 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)
}

// ValidateBool performs the validation.
func (v allValidator) ValidateBool(ctx context.Context, req validator.BoolRequest, resp *validator.BoolResponse) {
for _, subValidator := range v.validators {
validateResp := &validator.BoolResponse{}

subValidator.ValidateBool(ctx, req, validateResp)

resp.Diagnostics.Append(validateResp.Diagnostics...)
}
}
29 changes: 29 additions & 0 deletions boolvalidator/all_example_test.go
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 boolvalidator_test

import (
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"

"github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator"
)

func ExampleAll() {
// Used within a Schema method of a DataSource, Provider, or Resource
_ = schema.Schema{
Attributes: map[string]schema.Attribute{
"example_attr": schema.BoolAttribute{
Required: true,
Validators: []validator.Bool{
// This attribute must satisfy either All validator.
boolvalidator.Any(
boolvalidator.All( /* ... */ ),
boolvalidator.All( /* ... */ ),
),
},
},
},
}
}
65 changes: 65 additions & 0 deletions boolvalidator/any.go
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 boolvalidator

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.Bool) validator.Bool {
return anyValidator{
validators: validators,
}
}

var _ validator.Bool = anyValidator{}

// anyValidator implements the validator.
type anyValidator struct {
validators []validator.Bool
}

// 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)
}

// ValidateBool performs the validation.
func (v anyValidator) ValidateBool(ctx context.Context, req validator.BoolRequest, resp *validator.BoolResponse) {
for _, subValidator := range v.validators {
validateResp := &validator.BoolResponse{}

subValidator.ValidateBool(ctx, req, validateResp)

if !validateResp.Diagnostics.HasError() {
resp.Diagnostics = validateResp.Diagnostics

return
}

resp.Diagnostics.Append(validateResp.Diagnostics...)
}
}
25 changes: 25 additions & 0 deletions boolvalidator/any_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package boolvalidator_test

import (
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"

"github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator"
)

func ExampleAny() {
// Used within a Schema method of a DataSource, Provider, or Resource
_ = schema.Schema{
Attributes: map[string]schema.Attribute{
"example_attr": schema.BoolAttribute{
Required: true,
Validators: []validator.Bool{
boolvalidator.Any( /* ... */ ),
},
},
},
}
}
67 changes: 67 additions & 0 deletions boolvalidator/any_with_all_warnings.go
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 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()
}
}
25 changes: 25 additions & 0 deletions boolvalidator/any_with_all_warnings_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package boolvalidator_test

import (
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"

"github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator"
)

func ExampleAnyWithAllWarnings() {
// Used within a Schema method of a DataSource, Provider, or Resource
_ = schema.Schema{
Attributes: map[string]schema.Attribute{
"example_attr": schema.BoolAttribute{
Required: true,
Validators: []validator.Bool{
boolvalidator.AnyWithAllWarnings( /* ... */ ),
},
},
},
}
}
57 changes: 57 additions & 0 deletions datasourcevalidator/all.go
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 datasourcevalidator

import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/datasource"
)

// All returns a validator which ensures that any configured 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 ...datasource.ConfigValidator) datasource.ConfigValidator {
return allValidator{
validators: validators,
}
}

var _ datasource.ConfigValidator = allValidator{}

// allValidator implements the validator.
type allValidator struct {
validators []datasource.ConfigValidator
}

// 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)
}

// ValidateDataSource performs the validation.
func (v allValidator) ValidateDataSource(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) {
for _, subValidator := range v.validators {
validateResp := &datasource.ValidateConfigResponse{}

subValidator.ValidateDataSource(ctx, req, validateResp)

resp.Diagnostics.Append(validateResp.Diagnostics...)
}
}
21 changes: 21 additions & 0 deletions datasourcevalidator/all_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package datasourcevalidator_test

import (
"github.com/hashicorp/terraform-plugin-framework/datasource"

"github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator"
)

func ExampleAll() {
// Used inside a datasource.DataSource type ConfigValidators method
_ = []datasource.ConfigValidator{
// The configuration must satisfy either All validator.
datasourcevalidator.Any(
datasourcevalidator.All( /* ... */ ),
datasourcevalidator.All( /* ... */ ),
),
}
}
Loading