schemavalidator

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2022 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package schemavalidator provides validators to express relationships between multiple attributes within the schema of a resource, data source, or provider. For example, checking that an attribute is present when another is present, or vice-versa.

These validators are implemented on a starting attribute, where relationships can be expressed as absolute paths to others or relative to the starting attribute. For multiple attribute validators that are defined outside the schema, which may be easier to implement in provider code generation situations or suit provider code preferences differently, refer to the datasourcevalidator, providervalidator, or resourcevalidator package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AlsoRequires

func AlsoRequires(attributePaths ...path.Expression) tfsdk.AttributeValidator

AlsoRequires checks that a set of path.Expression has a non-null value, if the current attribute also has a non-null value.

This implements the validation logic declaratively within the tfsdk.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 against the validated attribute.

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/schemavalidator"
	"github.com/hashicorp/terraform-plugin-framework/path"
	"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 attribute must be configured with other_attr.
					schemavalidator.AlsoRequires(path.Expressions{
						path.MatchRoot("other_attr"),
					}...),
				},
			},
			"other_attr": {
				Required: true,
				Type:     types.StringType,
			},
		},
	}
}
Output:

func AtLeastOneOf

func AtLeastOneOf(attributePaths ...path.Expression) tfsdk.AttributeValidator

AtLeastOneOf checks that of a set of path.Expression, including the attribute it's applied to, at least one attribute out of all specified is 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 against the attribute with this validator.

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/schemavalidator"
	"github.com/hashicorp/terraform-plugin-framework/path"
	"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 at least this attribute or other_attr should be configured.
					schemavalidator.AtLeastOneOf(path.Expressions{
						path.MatchRoot("other_attr"),
					}...),
				},
			},
			"other_attr": {
				Required: true,
				Type:     types.StringType,
			},
		},
	}
}
Output:

func ConflictsWith

func ConflictsWith(attributePaths ...path.Expression) tfsdk.AttributeValidator

ConflictsWith checks that a set of path.Expression, including the attribute it's applied to, do not have a value simultaneously.

This implements the validation logic declaratively within the tfsdk.Schema. Refer to [datasourcevalidator.Conflicting], [providervalidator.Conflicting], or [resourcevalidator.Conflicting] for declaring this type of validation outside the schema definition.

Relative path.Expression will be resolved against the validated attribute.

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/schemavalidator"
	"github.com/hashicorp/terraform-plugin-framework/path"
	"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 attribute must not be configured with other_attr.
					schemavalidator.ConflictsWith(path.Expressions{
						path.MatchRoot("other_attr"),
					}...),
				},
			},
			"other_attr": {
				Required: true,
				Type:     types.StringType,
			},
		},
	}
}
Output:

func ExactlyOneOf

func ExactlyOneOf(attributePaths ...path.Expression) tfsdk.AttributeValidator

ExactlyOneOf checks that of a set of path.Expression, including the attribute it's applied to, one and only one attribute out of all specified has a value. It will also cause a validation error if none are specified.

This implements the validation logic declaratively within the tfsdk.Schema. Refer to [datasourcevalidator.ExactlyOneOf], [providervalidator.ExactlyOneOf], or [resourcevalidator.ExactlyOneOf] for declaring this type of validation outside the schema definition.

Relative path.Expression will be resolved against the validated attribute.

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/schemavalidator"
	"github.com/hashicorp/terraform-plugin-framework/path"
	"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 only this attribute or other_attr is configured.
					schemavalidator.ExactlyOneOf(path.Expressions{
						path.MatchRoot("other_attr"),
					}...),
				},
			},
			"other_attr": {
				Required: true,
				Type:     types.StringType,
			},
		},
	}
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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