dynamicvalidator

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2025 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package dynamicvalidator provides validators for types.Dynamic attributes and function parameters.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(validators ...validator.Dynamic) validator.Dynamic

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.

Example
package main

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 main() {
	// 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{
					dynamicvalidator.Any(
						dynamicvalidator.Any( /* ... */ ),
						dynamicvalidator.All( /* ... */ ),
					),
				},
			},
		},
	}
}

func AlsoRequires

func AlsoRequires(expressions ...path.Expression) validator.Dynamic

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.

Example
package main

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 main() {
	// 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,
			},
		},
	}
}

func Any

func Any(validators ...validator.Dynamic) validator.Dynamic

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.

Example
package main

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 main() {
	// 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{
					dynamicvalidator.Any(
						dynamicvalidator.Any( /* ... */ ),
					),
				},
			},
		},
	}
}

func AnyWithAllWarnings

func AnyWithAllWarnings(validators ...validator.Dynamic) validator.Dynamic

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.

Example
package main

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 main() {
	// 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{
					dynamicvalidator.AnyWithAllWarnings(
						dynamicvalidator.AnyWithAllWarnings( /* ... */ ),
					),
				},
			},
		},
	}
}

func AtLeastOneOf

func AtLeastOneOf(expressions ...path.Expression) validator.Dynamic

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.

Example
package main

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 main() {
	// 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 at least this attribute or other_attr should be configured.
					dynamicvalidator.AtLeastOneOf(path.Expressions{
						path.MatchRoot("other_attr"),
					}...),
				},
			},
			"other_attr": schema.DynamicAttribute{
				Optional: true,
			},
		},
	}
}

func ConflictsWith

func ConflictsWith(expressions ...path.Expression) validator.Dynamic

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

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

Example
package main

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 main() {
	// 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 not be configured with other_attr.
					dynamicvalidator.ConflictsWith(path.Expressions{
						path.MatchRoot("other_attr"),
					}...),
				},
			},
			"other_attr": schema.DynamicAttribute{
				Optional: true,
			},
		},
	}
}

func ExactlyOneOf

func ExactlyOneOf(expressions ...path.Expression) validator.Dynamic

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

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

Example
package main

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 main() {
	// 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 only this attribute or other_attr is configured.
					dynamicvalidator.ExactlyOneOf(path.Expressions{
						path.MatchRoot("other_attr"),
					}...),
				},
			},
			"other_attr": schema.DynamicAttribute{
				Optional: true,
			},
		},
	}
}

func PreferWriteOnlyAttribute added in v0.17.0

func PreferWriteOnlyAttribute(writeOnlyAttribute path.Expression) validator.Dynamic

PreferWriteOnlyAttribute returns a warning if the Terraform client supports write-only attributes, and the attribute that the validator is applied to has a value. It takes in a path.Expression that represents the write-only attribute schema location, and the warning message will indicate that the write-only attribute should be preferred.

This validator should only be used for resource attributes as other schema types do not support write-only attributes.

This implements the validation logic declaratively within the schema. Refer to [resourcevalidator.PreferWriteOnlyAttribute] for declaring this type of validation outside the schema definition.

NOTE: This validator will produce persistent warnings for practitioners on every Terraform run as long as the specified non-write-only attribute has a value in the configuration. The validator will also produce warnings for users of shared modules who cannot immediately take action on the warning.

Example
package main

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

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

func main() {
	// Used within a Schema method of a Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.DynamicAttribute{
				Optional: true,
				Validators: []validator.Dynamic{
					// Throws a warning diagnostic encouraging practitioners to use
					// write_only_attr if example_attr has a value.
					dynamicvalidator.PreferWriteOnlyAttribute(
						path.MatchRoot("write_only_attr"),
					),
				},
			},
			"write_only_attr": schema.DynamicAttribute{
				WriteOnly: true,
				Optional:  true,
			},
		},
	}
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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