metavalidator

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2022 License: MPL-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package metavalidator provides attribute validators that combine (algebraically) other attribute validators.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(valueValidators ...tfsdk.AttributeValidator) tfsdk.AttributeValidator

All returns an AttributeValidator which ensures that any configured attribute value:

  • Validates against all the value validators.

Use of All is only necessary when used in conjunction with Any or AnyWithAllWarnings as the []tfsdk.AttributeValidator field automatically applies a logical AND.

Example
package main

import (
	"regexp"

	"github.com/hashicorp/terraform-plugin-framework-validators/metavalidator"
	"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
	"github.com/hashicorp/terraform-plugin-framework/tfsdk"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func main() {
	// Used within a GetSchema method of a DataSource, Provider, or Resource
	_ = tfsdk.Schema{
		Attributes: map[string]tfsdk.Attribute{
			"example_attr": {
				Required: true,
				Type:     types.StringType,
				Validators: []tfsdk.AttributeValidator{
					// Validate this string value must either be:
					//  - "!!!"
					//  - At least 3 alphanumeric characters.
					metavalidator.Any(
						stringvalidator.OneOf("!!!"),
						metavalidator.All(
							stringvalidator.LengthAtLeast(3),
							stringvalidator.RegexMatches(
								regexp.MustCompile(`^[a-zA-Z0-9]*$`),
								"must contain only alphanumeric characters",
							),
						),
					),
				},
			},
		},
	}
}

func Any

func Any(valueValidators ...tfsdk.AttributeValidator) tfsdk.AttributeValidator

Any returns an AttributeValidator which ensures that any configured attribute value:

  • Validates against at least one of the value 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.

Example
package main

import (
	"regexp"

	"github.com/hashicorp/terraform-plugin-framework-validators/metavalidator"
	"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
	"github.com/hashicorp/terraform-plugin-framework/tfsdk"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func main() {
	// Used within a GetSchema method of a DataSource, Provider, or Resource
	_ = tfsdk.Schema{
		Attributes: map[string]tfsdk.Attribute{
			"example_attr": {
				Required: true,
				Type:     types.StringType,
				Validators: []tfsdk.AttributeValidator{
					// Validate this string value must either be:
					//  - "!!!"
					//  - Any length of alphanumeric characters.
					metavalidator.Any(
						stringvalidator.OneOf("!!!"),
						stringvalidator.RegexMatches(
							regexp.MustCompile(`^[a-zA-Z0-9]*$`),
							"must contain only alphanumeric characters",
						),
					),
				},
			},
		},
	}
}

func AnyWithAllWarnings

func AnyWithAllWarnings(valueValidators ...tfsdk.AttributeValidator) tfsdk.AttributeValidator

AnyWithAllWarnings returns an AttributeValidator which ensures that any configured attribute value:

  • Validates against at least one of the value validators.
  • Returns all warnings for all passing and failing validators when at least one of the validators passes.

Use Any() to only return warnings from the passing validator.

Example
package main

import (
	"regexp"

	"github.com/hashicorp/terraform-plugin-framework-validators/metavalidator"
	"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
	"github.com/hashicorp/terraform-plugin-framework/tfsdk"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func main() {
	// Used within a GetSchema method of a DataSource, Provider, or Resource
	_ = tfsdk.Schema{
		Attributes: map[string]tfsdk.Attribute{
			"example_attr": {
				Required: true,
				Type:     types.StringType,
				Validators: []tfsdk.AttributeValidator{
					// Validate this string value must either be:
					//  - "!!!"
					//  - Any length of alphanumeric characters.
					metavalidator.AnyWithAllWarnings(
						stringvalidator.OneOf("!!!"),
						stringvalidator.RegexMatches(
							regexp.MustCompile(`^[a-zA-Z0-9]*$`),
							"must contain only alphanumeric characters",
						),
					),
				},
			},
		},
	}
}

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL