Documentation ¶
Overview ¶
Package boolvalidator provides validators for types.Bool attributes or function parameters.
Index ¶
- func All(validators ...validator.Bool) validator.Bool
- func AlsoRequires(expressions ...path.Expression) validator.Bool
- func Any(validators ...validator.Bool) validator.Bool
- func AnyWithAllWarnings(validators ...validator.Bool) validator.Bool
- func AtLeastOneOf(expressions ...path.Expression) validator.Bool
- func ConflictsWith(expressions ...path.Expression) validator.Bool
- func Equals(value bool) equalsValidator
- func ExactlyOneOf(expressions ...path.Expression) validator.Bool
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/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator" ) func main() { // Used within a Schema method of a DataSource, Provider, or Resource _ = schema.Schema{ Attributes: map[string]schema.Attribute{ "example_attr": schema.BoolAttribute{ Required: true, Validators: []validator.Bool{ // This attribute must satisfy either All validator. boolvalidator.Any( boolvalidator.All( /* ... */ ), boolvalidator.All( /* ... */ ), ), }, }, }, } }
Output:
func AlsoRequires ¶
func AlsoRequires(expressions ...path.Expression) validator.Bool
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/boolvalidator" "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.BoolAttribute{ Optional: true, Validators: []validator.Bool{ // Validate this attribute must be configured with other_attr. boolvalidator.AlsoRequires(path.Expressions{ path.MatchRoot("other_attr"), }...), }, }, "other_attr": schema.StringAttribute{ Optional: true, }, }, } }
Output:
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/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator" ) func main() { // Used within a Schema method of a DataSource, Provider, or Resource _ = schema.Schema{ Attributes: map[string]schema.Attribute{ "example_attr": schema.BoolAttribute{ Required: true, Validators: []validator.Bool{ boolvalidator.Any( /* ... */ ), }, }, }, } }
Output:
func AnyWithAllWarnings ¶ added in v0.12.0
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/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator" ) func main() { // Used within a Schema method of a DataSource, Provider, or Resource _ = schema.Schema{ Attributes: map[string]schema.Attribute{ "example_attr": schema.BoolAttribute{ Required: true, Validators: []validator.Bool{ boolvalidator.AnyWithAllWarnings( /* ... */ ), }, }, }, } }
Output:
func AtLeastOneOf ¶
func AtLeastOneOf(expressions ...path.Expression) validator.Bool
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/boolvalidator" "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.BoolAttribute{ Optional: true, Validators: []validator.Bool{ // Validate at least this attribute or other_attr should be configured. boolvalidator.AtLeastOneOf(path.Expressions{ path.MatchRoot("other_attr"), }...), }, }, "other_attr": schema.StringAttribute{ Optional: true, }, }, } }
Output:
func ConflictsWith ¶
func ConflictsWith(expressions ...path.Expression) validator.Bool
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/boolvalidator" "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.BoolAttribute{ Optional: true, Validators: []validator.Bool{ // Validate this attribute must not be configured with other_attr. boolvalidator.ConflictsWith(path.Expressions{ path.MatchRoot("other_attr"), }...), }, }, "other_attr": schema.StringAttribute{ Optional: true, }, }, } }
Output:
func Equals ¶ added in v0.14.0
func Equals(value bool) equalsValidator
Equals returns an AttributeValidator which ensures that the configured boolean attribute or function parameter matches the given `value`. Null (unconfigured) and unknown (known after apply) values are skipped.
func ExactlyOneOf ¶
func ExactlyOneOf(expressions ...path.Expression) validator.Bool
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/boolvalidator" "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.BoolAttribute{ Optional: true, Validators: []validator.Bool{ // Validate only this attribute or other_attr is configured. boolvalidator.ExactlyOneOf(path.Expressions{ path.MatchRoot("other_attr"), }...), }, }, "other_attr": schema.StringAttribute{ Optional: true, }, }, } }
Output:
Types ¶
This section is empty.