resourcevalidator

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: 7 Imported by: 24

Documentation

Overview

Package resourcevalidator provides validators to express relationships between multiple attributes of a resource. For example, checking that multiple attributes are not configured at the same time.

These validators are implemented outside the schema, which may be easier to implement in provider code generation situations or suit provider code preferences differently than those in the schemavalidator package. Those validators start on a starting attribute, where relationships can be expressed as absolute paths to others or relative to the starting attribute.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All added in v0.12.0

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.

Example
package main

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

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

func main() {
	// Used inside a resource.Resource type ConfigValidators method
	_ = []resource.ConfigValidator{
		// The configuration must satisfy either All validator.
		resourcevalidator.Any(
			resourcevalidator.All( /* ... */ ),
			resourcevalidator.All( /* ... */ ),
		),
	}
}

func Any added in v0.12.0

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/resource"

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

func main() {
	// Used inside a resource.Resource type ConfigValidators method
	_ = []resource.ConfigValidator{
		resourcevalidator.Any( /* ... */ ),
	}
}

func AnyWithAllWarnings added in v0.12.0

func AnyWithAllWarnings(validators ...resource.ConfigValidator) resource.ConfigValidator

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/resource"

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

func main() {
	// Used inside a resource.Resource type ConfigValidators method
	_ = []resource.ConfigValidator{
		resourcevalidator.AnyWithAllWarnings( /* ... */ ),
	}
}

func AtLeastOneOf

func AtLeastOneOf(expressions ...path.Expression) resource.ConfigValidator

AtLeastOneOf checks that a set of path.Expression has at least one non-null or unknown value.

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator"
	"github.com/hashicorp/terraform-plugin-framework/path"
	"github.com/hashicorp/terraform-plugin-framework/resource"
)

func main() {
	// Used inside a resource.Resource type ConfigValidators method
	_ = []resource.ConfigValidator{
		// Validate at least one of the schema defined attributes named attr1
		// and attr2 has a known, non-null value.
		resourcevalidator.AtLeastOneOf(
			path.MatchRoot("attr1"),
			path.MatchRoot("attr2"),
		),
	}
}

func Conflicting

func Conflicting(expressions ...path.Expression) resource.ConfigValidator

Conflicting checks that a set of path.Expression, are not configured simultaneously.

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator"
	"github.com/hashicorp/terraform-plugin-framework/path"
	"github.com/hashicorp/terraform-plugin-framework/resource"
)

func main() {
	// Used inside a resource.Resource type ConfigValidators method
	_ = []resource.ConfigValidator{
		// Validate that schema defined attributes named attr1 and attr2 are not
		// both configured with known, non-null values.
		resourcevalidator.Conflicting(
			path.MatchRoot("attr1"),
			path.MatchRoot("attr2"),
		),
	}
}

func ExactlyOneOf

func ExactlyOneOf(expressions ...path.Expression) resource.ConfigValidator

ExactlyOneOf checks that a set of path.Expression does not have more than one known value.

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator"
	"github.com/hashicorp/terraform-plugin-framework/path"
	"github.com/hashicorp/terraform-plugin-framework/resource"
)

func main() {
	// Used inside a resource.Resource type ConfigValidators method
	_ = []resource.ConfigValidator{
		// Validate only one of the schema defined attributes named attr1
		// and attr2 has a known, non-null value.
		resourcevalidator.ExactlyOneOf(
			path.MatchRoot("attr1"),
			path.MatchRoot("attr2"),
		),
	}
}

func PreferWriteOnlyAttribute added in v0.17.0

func PreferWriteOnlyAttribute(oldAttribute path.Expression, writeOnlyAttribute path.Expression) resource.ConfigValidator

PreferWriteOnlyAttribute returns a warning if the Terraform client supports write-only attributes, and the old attribute value is not null.

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"

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

func main() {
	// Used inside a resource.Resource type ConfigValidators method
	_ = []resource.ConfigValidator{
		// Throws a warning diagnostic if the resource supports write-only
		// attributes and the oldAttribute has a known value.
		resourcevalidator.PreferWriteOnlyAttribute(
			path.MatchRoot("oldAttribute"),
			path.MatchRoot("writeOnlyAttribute"),
		),
	}
}

func RequiredTogether

func RequiredTogether(expressions ...path.Expression) resource.ConfigValidator

RequiredTogether checks that a set of path.Expression either has all known or all null values.

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator"
	"github.com/hashicorp/terraform-plugin-framework/path"
	"github.com/hashicorp/terraform-plugin-framework/resource"
)

func main() {
	// Used inside a resource.Resource type ConfigValidators method
	_ = []resource.ConfigValidator{
		// Validate the schema defined attributes named attr1 and attr2 are either
		// both null or both known values.
		resourcevalidator.RequiredTogether(
			path.MatchRoot("attr1"),
			path.MatchRoot("attr2"),
		),
	}
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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