objectvalidator

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2024 License: MPL-2.0 Imports: 7 Imported by: 18

Documentation

Overview

Package objectvalidator provides validators for types.Object attributes.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(validators ...validator.Object) validator.Object

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/objectvalidator"
	"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.ObjectAttribute{
				Required: true,
				Validators: []validator.Object{
					// This Object must satify either All validator.
					objectvalidator.Any(
						objectvalidator.All( /* ... */ ),
						objectvalidator.All( /* ... */ ),
					),
				},
			},
		},
	}
}
Output:

func AlsoRequires

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

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/objectvalidator"
	"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.ObjectAttribute{
				Optional: true,
				Validators: []validator.Object{
					// Validate this attribute must be configured with other_attr.
					objectvalidator.AlsoRequires(path.Expressions{
						path.MatchRoot("other_attr"),
					}...),
				},
			},
			"other_attr": schema.StringAttribute{
				Optional: true,
			},
		},
	}
}
Output:

func Any

func Any(validators ...validator.Object) validator.Object

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/objectvalidator"
	"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.ObjectAttribute{
				Required: true,
				Validators: []validator.Object{
					objectvalidator.Any( /* ... */ ),
				},
			},
		},
	}
}
Output:

func AnyWithAllWarnings

func AnyWithAllWarnings(validators ...validator.Object) validator.Object

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/objectvalidator"
	"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.ObjectAttribute{
				Required: true,
				Validators: []validator.Object{
					objectvalidator.AnyWithAllWarnings( /* ... */ ),
				},
			},
		},
	}
}
Output:

func AtLeastOneOf

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

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/objectvalidator"
	"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.ObjectAttribute{
				Optional: true,
				Validators: []validator.Object{
					// Validate at least this attribute or other_attr should be configured.
					objectvalidator.AtLeastOneOf(path.Expressions{
						path.MatchRoot("other_attr"),
					}...),
				},
			},
			"other_attr": schema.StringAttribute{
				Optional: true,
			},
		},
	}
}
Output:

func ConflictsWith

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

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/objectvalidator"
	"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.ObjectAttribute{
				Optional: true,
				Validators: []validator.Object{
					// Validate this attribute must not be configured with other_attr.
					objectvalidator.ConflictsWith(path.Expressions{
						path.MatchRoot("other_attr"),
					}...),
				},
			},
			"other_attr": schema.StringAttribute{
				Optional: true,
			},
		},
	}
}
Output:

func ExactlyOneOf

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

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/objectvalidator"
	"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.ObjectAttribute{
				Optional: true,
				Validators: []validator.Object{
					// Validate only this attribute or other_attr is configured.
					objectvalidator.ExactlyOneOf(path.Expressions{
						path.MatchRoot("other_attr"),
					}...),
				},
			},
			"other_attr": schema.StringAttribute{
				Optional: true,
			},
		},
	}
}
Output:

func IsRequired added in v0.10.0

func IsRequired() validator.Object

IsRequired returns a validator which ensures that any configured object has a value (not null).

This validator is equivalent to the `Required` field on attributes and is only practical for use with `schema.SingleNestedBlock`

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/objectvalidator"
	"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{
		Blocks: map[string]schema.Block{
			"example_block": schema.SingleNestedBlock{
				Validators: []validator.Object{
					// Validate this block has a value (not null).
					objectvalidator.IsRequired(),
				},
				Attributes: map[string]schema.Attribute{
					"example_string_attribute": schema.StringAttribute{
						Required: true,
					},
				},
			},
		},
	}
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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