int32validator

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: 12 Imported by: 2

Documentation

Overview

Package int32validator provides validators for types.Int32 attributes or function parameters.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(validators ...validator.Int32) validator.Int32

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/datasource/schema"
	"github.com/hashicorp/terraform-plugin-framework/schema/validator"

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

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.Int32Attribute{
				Required: true,
				Validators: []validator.Int32{
					// Validate this Int32 value must either be:
					//  - 1
					//  - At least 2, but not 3
					int32validator.Any(
						int32validator.OneOf(1),
						int32validator.All(
							int32validator.AtLeast(2),
							int32validator.NoneOf(3),
						),
					),
				},
			},
		},
	}
}
Output:

func AlsoRequires

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

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 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 being validated.

Example
package main

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

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

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.Int32Attribute{
				Optional: true,
				Validators: []validator.Int32{
					// Validate this attribute must be configured with other_attr.
					int32validator.AlsoRequires(path.Expressions{
						path.MatchRoot("other_attr"),
					}...),
				},
			},
			"other_attr": schema.StringAttribute{
				Optional: true,
			},
		},
	}
}
Output:

func Any

func Any(validators ...validator.Int32) validator.Int32

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/int32validator"
)

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.Int32Attribute{
				Required: true,
				Validators: []validator.Int32{
					// Validate this Int32 value must either be:
					//  - 1
					//  - At least 2
					int32validator.Any(
						int32validator.OneOf(1),
						int32validator.AtLeast(2),
					),
				},
			},
		},
	}
}
Output:

func AnyWithAllWarnings

func AnyWithAllWarnings(validators ...validator.Int32) validator.Int32

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/int32validator"
)

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.Int32Attribute{
				Required: true,
				Validators: []validator.Int32{
					// Validate this Int32 value must either be:
					//  - 1
					//  - At least 2
					int32validator.AnyWithAllWarnings(
						int32validator.OneOf(1),
						int32validator.AtLeast(2),
					),
				},
			},
		},
	}
}
Output:

func AtLeast

func AtLeast(minVal int32) atLeastValidator

AtLeast returns an AttributeValidator which ensures that any configured attribute or function parameter value:

  • Is a number, which can be represented by a 32-bit integer.
  • Is greater than or equal to the given minimum.

Null (unconfigured) and unknown (known after apply) values are skipped.

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/int32validator"
)

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.Int32Attribute{
				Required: true,
				Validators: []validator.Int32{
					// Validate integer value must be at least 42
					int32validator.AtLeast(42),
				},
			},
		},
	}
}
Output:

Example (Function)
package main

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

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

func main() {
	_ = function.Definition{
		Parameters: []function.Parameter{
			function.Int32Parameter{
				Name: "example_param",
				Validators: []function.Int32ParameterValidator{
					// Validate integer value must be at least 42
					int32validator.AtLeast(42),
				},
			},
		},
	}
}
Output:

func AtLeastOneOf

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

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/datasource/schema"
	"github.com/hashicorp/terraform-plugin-framework/path"
	"github.com/hashicorp/terraform-plugin-framework/schema/validator"

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

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.Int32Attribute{
				Optional: true,
				Validators: []validator.Int32{
					// Validate at least this attribute or other_attr should be configured.
					int32validator.AtLeastOneOf(path.Expressions{
						path.MatchRoot("other_attr"),
					}...),
				},
			},
			"other_attr": schema.StringAttribute{
				Optional: true,
			},
		},
	}
}
Output:

func AtLeastSumOf

func AtLeastSumOf(attributesToSumPathExpressions ...path.Expression) validator.Int32

AtLeastSumOf returns an AttributeValidator which ensures that any configured attribute value:

  • Is a number, which can be represented by a 32-bit integer.
  • Is at least the sum of the attributes retrieved via the given path expression(s).

Null (unconfigured) and unknown (known after apply) values are skipped.

Example
package main

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

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

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.Int32Attribute{
				Required: true,
				Validators: []validator.Int32{
					// Validate this integer value must be at least the
					// summed integer values of other_attr1 and other_attr2.
					int32validator.AtLeastSumOf(path.Expressions{
						path.MatchRoot("other_attr1"),
						path.MatchRoot("other_attr2"),
					}...),
				},
			},
			"other_attr1": schema.Int32Attribute{
				Required: true,
			},
			"other_attr2": schema.Int32Attribute{
				Required: true,
			},
		},
	}
}
Output:

func AtMost

func AtMost(maxVal int32) atMostValidator

AtMost returns an AttributeValidator which ensures that any configured attribute or function parameter value:

  • Is a number, which can be represented by a 32-bit integer.
  • Is less than or equal to the given maximum.

Null (unconfigured) and unknown (known after apply) values are skipped.

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/int32validator"
)

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.Int32Attribute{
				Required: true,
				Validators: []validator.Int32{
					// Validate integer value must be at most 42
					int32validator.AtMost(42),
				},
			},
		},
	}
}
Output:

Example (Function)
package main

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

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

func main() {
	_ = function.Definition{
		Parameters: []function.Parameter{
			function.Int32Parameter{
				Name: "example_param",
				Validators: []function.Int32ParameterValidator{
					// Validate integer value must be at most 42
					int32validator.AtMost(42),
				},
			},
		},
	}
}
Output:

func AtMostSumOf

func AtMostSumOf(attributesToSumPathExpressions ...path.Expression) validator.Int32

AtMostSumOf returns an AttributeValidator which ensures that any configured attribute value:

  • Is a number, which can be represented by a 32-bit integer.
  • Is at most the sum of the given attributes retrieved via the given path expression(s).

Null (unconfigured) and unknown (known after apply) values are skipped.

Example
package main

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

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

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.Int32Attribute{
				Required: true,
				Validators: []validator.Int32{
					// Validate this integer value must be at most the
					// summed integer values of other_attr1 and other_attr2.
					int32validator.AtMostSumOf(path.Expressions{
						path.MatchRoot("other_attr1"),
						path.MatchRoot("other_attr2"),
					}...),
				},
			},
			"other_attr1": schema.Int32Attribute{
				Required: true,
			},
			"other_attr2": schema.Int32Attribute{
				Required: true,
			},
		},
	}
}
Output:

func Between

func Between(minVal, maxVal int32) betweenValidator

Between returns an AttributeValidator which ensures that any configured attribute or function parameter value:

  • Is a number, which can be represented by a 32-bit integer.
  • Is greater than or equal to the given minimum and less than or equal to the given maximum.

Null (unconfigured) and unknown (known after apply) values are skipped.

minVal cannot be greater than maxVal. Invalid combinations of minVal and maxVal will result in an implementation error message during validation.

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/int32validator"
)

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.Int32Attribute{
				Required: true,
				Validators: []validator.Int32{
					// Validate integer value must be at least 10 and at most 100
					int32validator.Between(10, 100),
				},
			},
		},
	}
}
Output:

Example (Function)
package main

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

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

func main() {
	_ = function.Definition{
		Parameters: []function.Parameter{
			function.Int32Parameter{
				Name: "example_param",
				Validators: []function.Int32ParameterValidator{
					// Validate integer value must be at least 10 and at most 100
					int32validator.Between(10, 100),
				},
			},
		},
	}
}
Output:

func ConflictsWith

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

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/datasource/schema"
	"github.com/hashicorp/terraform-plugin-framework/path"
	"github.com/hashicorp/terraform-plugin-framework/schema/validator"

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

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.Int32Attribute{
				Optional: true,
				Validators: []validator.Int32{
					// Validate this attribute must not be configured with other_attr.
					int32validator.ConflictsWith(path.Expressions{
						path.MatchRoot("other_attr"),
					}...),
				},
			},
			"other_attr": schema.StringAttribute{
				Optional: true,
			},
		},
	}
}
Output:

func EqualToProductOf

func EqualToProductOf(attributesToMultiplyPathExpressions ...path.Expression) validator.Int32

EqualToProductOf returns an AttributeValidator which ensures that any configured attribute value:

  • Is a number, which can be represented by a 32-bit integer.
  • Is equal to the product of the given attributes retrieved via the given path expression(s).

Validation is skipped if any null (unconfigured) and/or unknown (known after apply) values are present.

Example
package main

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

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

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.Int32Attribute{
				Required: true,
				Validators: []validator.Int32{
					// Validate this integer value must be equal to the
					// product of integer values other_attr1 and other_attr2.
					int32validator.EqualToProductOf(path.Expressions{
						path.MatchRoot("other_attr1"),
						path.MatchRoot("other_attr2"),
					}...),
				},
			},
			"other_attr1": schema.Int32Attribute{
				Required: true,
			},
			"other_attr2": schema.Int32Attribute{
				Required: true,
			},
		},
	}
}
Output:

func EqualToSumOf

func EqualToSumOf(attributesToSumPathExpressions ...path.Expression) validator.Int32

EqualToSumOf returns an AttributeValidator which ensures that any configured attribute value:

  • Is a number, which can be represented by a 32-bit integer.
  • Is equal to the sum of the given attributes retrieved via the given path expression(s).

Null (unconfigured) and unknown (known after apply) values are skipped.

Example
package main

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

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

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.Int32Attribute{
				Required: true,
				Validators: []validator.Int32{
					// Validate this integer value must be equal to the
					// summed integer values of other_attr1 and other_attr2.
					int32validator.EqualToSumOf(path.Expressions{
						path.MatchRoot("other_attr1"),
						path.MatchRoot("other_attr2"),
					}...),
				},
			},
			"other_attr1": schema.Int32Attribute{
				Required: true,
			},
			"other_attr2": schema.Int32Attribute{
				Required: true,
			},
		},
	}
}
Output:

func ExactlyOneOf

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

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/datasource/schema"
	"github.com/hashicorp/terraform-plugin-framework/path"
	"github.com/hashicorp/terraform-plugin-framework/schema/validator"

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

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.Int32Attribute{
				Optional: true,
				Validators: []validator.Int32{
					// Validate only this attribute or other_attr is configured.
					int32validator.ExactlyOneOf(path.Expressions{
						path.MatchRoot("other_attr"),
					}...),
				},
			},
			"other_attr": schema.StringAttribute{
				Optional: true,
			},
		},
	}
}
Output:

func NoneOf

func NoneOf(values ...int32) noneOfValidator

NoneOf checks that the Int32 held in the attribute or function parameter is none of the given `values`.

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/int32validator"
)

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.Int32Attribute{
				Required: true,
				Validators: []validator.Int32{
					// Validate integer value must not be 12, 24, or 48
					int32validator.NoneOf([]int32{12, 24, 48}...),
				},
			},
		},
	}
}
Output:

Example (Function)
package main

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

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

func main() {
	_ = function.Definition{
		Parameters: []function.Parameter{
			function.Int32Parameter{
				Name: "example_param",
				Validators: []function.Int32ParameterValidator{
					// Validate integer value must not be 12, 24, or 48
					int32validator.NoneOf([]int32{12, 24, 48}...),
				},
			},
		},
	}
}
Output:

func OneOf

func OneOf(values ...int32) oneOfValidator

OneOf checks that the Int32 held in the attribute or function parameter is one of the given `values`.

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/int32validator"
)

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.Int32Attribute{
				Required: true,
				Validators: []validator.Int32{
					// Validate integer value must be 12, 24, or 48
					int32validator.OneOf([]int32{12, 24, 48}...),
				},
			},
		},
	}
}
Output:

Example (Function)
package main

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

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

func main() {
	_ = function.Definition{
		Parameters: []function.Parameter{
			function.Int32Parameter{
				Name: "example_param",
				Validators: []function.Int32ParameterValidator{
					// Validate integer value must be 12, 24, or 48
					int32validator.OneOf([]int32{12, 24, 48}...),
				},
			},
		},
	}
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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