int64validator

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2022 License: MPL-2.0 Imports: 10 Imported by: 58

Documentation

Overview

Package int64validator provides validators for types.Int64 attributes.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All added in v0.7.0

func All(validators ...validator.Int64) validator.Int64

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/int64validator"
	"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.Int64Attribute{
				Required: true,
				Validators: []validator.Int64{
					// Validate this Int64 value must either be:
					//  - 1.0
					//  - At least 2.0, but not 3.0
					int64validator.Any(
						int64validator.OneOf(1.0),
						int64validator.All(
							int64validator.AtLeast(2.0),
							int64validator.NoneOf(3.0),
						),
					),
				},
			},
		},
	}
}
Output:

func AlsoRequires added in v0.7.0

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

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

func Any added in v0.7.0

func Any(validators ...validator.Int64) validator.Int64

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/int64validator"
	"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.Int64Attribute{
				Required: true,
				Validators: []validator.Int64{
					// Validate this Int64 value must either be:
					//  - 1.0
					//  - At least 2.0
					int64validator.Any(
						int64validator.OneOf(1.0),
						int64validator.AtLeast(2.0),
					),
				},
			},
		},
	}
}
Output:

func AnyWithAllWarnings added in v0.7.0

func AnyWithAllWarnings(validators ...validator.Int64) validator.Int64

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/int64validator"
	"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.Int64Attribute{
				Required: true,
				Validators: []validator.Int64{
					// Validate this Int64 value must either be:
					//  - 1.0
					//  - At least 2.0
					int64validator.AnyWithAllWarnings(
						int64validator.OneOf(1.0),
						int64validator.AtLeast(2.0),
					),
				},
			},
		},
	}
}
Output:

func AtLeast

func AtLeast(min int64) validator.Int64

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

  • Is a number, which can be represented by a 64-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-validators/int64validator"
	"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.Int64Attribute{
				Required: true,
				Validators: []validator.Int64{
					// Validate integer value must be at least 42
					int64validator.AtLeast(42),
				},
			},
		},
	}
}
Output:

func AtLeastOneOf added in v0.7.0

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

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

func AtLeastSumOf added in v0.4.0

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

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

  • Is a number, which can be represented by a 64-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-validators/int64validator"
	"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.Int64Attribute{
				Required: true,
				Validators: []validator.Int64{
					// Validate this integer value must be at least the
					// summed integer values of other_attr1 and other_attr2.
					int64validator.AtLeastSumOf(path.Expressions{
						path.MatchRoot("other_attr1"),
						path.MatchRoot("other_attr2"),
					}...),
				},
			},
			"other_attr1": schema.Int64Attribute{
				Required: true,
			},
			"other_attr2": schema.Int64Attribute{
				Required: true,
			},
		},
	}
}
Output:

func AtMost

func AtMost(max int64) validator.Int64

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

  • Is a number, which can be represented by a 64-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-validators/int64validator"
	"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.Int64Attribute{
				Required: true,
				Validators: []validator.Int64{
					// Validate integer value must be at most 42
					int64validator.AtMost(42),
				},
			},
		},
	}
}
Output:

func AtMostSumOf added in v0.4.0

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

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

  • Is a number, which can be represented by a 64-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-validators/int64validator"
	"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.Int64Attribute{
				Required: true,
				Validators: []validator.Int64{
					// Validate this integer value must be at most the
					// summed integer values of other_attr1 and other_attr2.
					int64validator.AtMostSumOf(path.Expressions{
						path.MatchRoot("other_attr1"),
						path.MatchRoot("other_attr2"),
					}...),
				},
			},
			"other_attr1": schema.Int64Attribute{
				Required: true,
			},
			"other_attr2": schema.Int64Attribute{
				Required: true,
			},
		},
	}
}
Output:

func Between

func Between(min, max int64) validator.Int64

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

  • Is a number, which can be represented by a 64-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.

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
	"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.Int64Attribute{
				Required: true,
				Validators: []validator.Int64{
					// Validate integer value must be at least 10 and at most 100
					int64validator.Between(10, 100),
				},
			},
		},
	}
}
Output:

func ConflictsWith added in v0.7.0

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

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

func EqualToSumOf added in v0.4.0

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

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

  • Is a number, which can be represented by a 64-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-validators/int64validator"
	"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.Int64Attribute{
				Required: true,
				Validators: []validator.Int64{
					// Validate this integer value must be equal to the
					// summed integer values of other_attr1 and other_attr2.
					int64validator.EqualToSumOf(path.Expressions{
						path.MatchRoot("other_attr1"),
						path.MatchRoot("other_attr2"),
					}...),
				},
			},
			"other_attr1": schema.Int64Attribute{
				Required: true,
			},
			"other_attr2": schema.Int64Attribute{
				Required: true,
			},
		},
	}
}
Output:

func ExactlyOneOf added in v0.7.0

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

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

func NoneOf added in v0.3.0

func NoneOf(values ...int64) validator.Int64

NoneOf checks that the Int64 held in the attribute is none of the given `values`.

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
	"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.Int64Attribute{
				Required: true,
				Validators: []validator.Int64{
					// Validate integer value must not be 12, 24, or 48
					int64validator.NoneOf([]int64{12, 24, 48}...),
				},
			},
		},
	}
}
Output:

func OneOf added in v0.3.0

func OneOf(values ...int64) validator.Int64

OneOf checks that the Int64 held in the attribute is none of the given `values`.

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
	"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.Int64Attribute{
				Required: true,
				Validators: []validator.Int64{
					// Validate integer value must be 12, 24, or 48
					int64validator.OneOf([]int64{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