mapvalidator

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

Documentation

Overview

Package mapvalidator provides validators for types.Map 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.Map) validator.Map

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

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.MapAttribute{
				ElementType: types.StringType,
				Required:    true,
				Validators: []validator.Map{
					// Validate this Map value must either be:
					//  - More than 5 elements
					//  - At least 2 elements, but not more than 3 elements
					mapvalidator.Any(
						mapvalidator.SizeAtLeast(5),
						mapvalidator.All(
							mapvalidator.SizeAtLeast(2),
							mapvalidator.SizeAtMost(3),
						),
					),
				},
			},
		},
	}
}
Output:

func AlsoRequires added in v0.7.0

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

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/mapvalidator"
	"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/types"
)

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.MapAttribute{
				ElementType: types.StringType,
				Optional:    true,
				Validators: []validator.Map{
					// Validate this attribute must be configured with other_attr.
					mapvalidator.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.Map) validator.Map

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/mapvalidator"
	"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.MapAttribute{
				Required: true,
				Validators: []validator.Map{
					// Validate this Map value must either be:
					//  - Between 1 and 2 elements
					//  - At least 4 elements
					mapvalidator.Any(
						mapvalidator.SizeBetween(1, 2),
						mapvalidator.SizeAtLeast(4),
					),
				},
			},
		},
	}
}
Output:

func AnyWithAllWarnings added in v0.7.0

func AnyWithAllWarnings(validators ...validator.Map) validator.Map

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/mapvalidator"
	"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.MapAttribute{
				Required: true,
				Validators: []validator.Map{
					// Validate this Map value must either be:
					//  - Between 1 and 2 elements
					//  - At least 4 elements
					mapvalidator.AnyWithAllWarnings(
						mapvalidator.SizeBetween(1, 2),
						mapvalidator.SizeAtLeast(4),
					),
				},
			},
		},
	}
}
Output:

func AtLeastOneOf added in v0.7.0

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

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/mapvalidator"
	"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/types"
)

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

func ConflictsWith added in v0.7.0

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

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/mapvalidator"
	"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/types"
)

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

func ExactlyOneOf added in v0.7.0

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

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/mapvalidator"
	"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/types"
)

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

func KeysAre

func KeysAre(keyValidators ...validator.String) validator.Map

KeysAre returns a map validator that validates all key strings with the given string validators.

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator"
	"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
	"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
	"github.com/hashicorp/terraform-plugin-framework/schema/validator"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.MapAttribute{
				ElementType: types.StringType,
				Required:    true,
				Validators: []validator.Map{
					// Validate this map must contain string keys which are at least 3 characters.
					mapvalidator.KeysAre(stringvalidator.LengthAtLeast(3)),
				},
			},
		},
	}
}
Output:

func SizeAtLeast

func SizeAtLeast(min int) validator.Map

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

  • Is a Map.
  • Contains at least min elements.

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

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator"
	"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
	"github.com/hashicorp/terraform-plugin-framework/schema/validator"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.MapAttribute{
				ElementType: types.StringType,
				Required:    true,
				Validators: []validator.Map{
					// Validate this map must contain at least 2 elements.
					mapvalidator.SizeAtLeast(2),
				},
			},
		},
	}
}
Output:

func SizeAtMost

func SizeAtMost(max int) validator.Map

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

  • Is a Map.
  • Contains at most max elements.

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

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator"
	"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
	"github.com/hashicorp/terraform-plugin-framework/schema/validator"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.MapAttribute{
				ElementType: types.StringType,
				Required:    true,
				Validators: []validator.Map{
					// Validate this map must contain at most 2 elements.
					mapvalidator.SizeAtMost(2),
				},
			},
		},
	}
}
Output:

func SizeBetween

func SizeBetween(min, max int) validator.Map

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

  • Is a Map.
  • Contains at least min elements and at most max elements.

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

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator"
	"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
	"github.com/hashicorp/terraform-plugin-framework/schema/validator"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.MapAttribute{
				ElementType: types.StringType,
				Required:    true,
				Validators: []validator.Map{
					// Validate this map must contain at least 2 and at most 4 elements.
					mapvalidator.SizeBetween(2, 4),
				},
			},
		},
	}
}
Output:

func ValueFloat64sAre added in v0.7.0

func ValueFloat64sAre(elementValidators ...validator.Float64) validator.Map

ValueFloat64sAre returns an validator which ensures that any configured Float64 values passes each Float64 validator.

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

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
	"github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator"
	"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
	"github.com/hashicorp/terraform-plugin-framework/schema/validator"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.MapAttribute{
				ElementType: types.Float64Type,
				Required:    true,
				Validators: []validator.Map{
					// Validate this Map must contain Float64 values which are at least 1.2.
					mapvalidator.ValueFloat64sAre(float64validator.AtLeast(1.2)),
				},
			},
		},
	}
}
Output:

func ValueInt64sAre added in v0.7.0

func ValueInt64sAre(elementValidators ...validator.Int64) validator.Map

ValueInt64sAre returns an validator which ensures that any configured Int64 values passes each Int64 validator.

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

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.MapAttribute{
				ElementType: types.Int64Type,
				Required:    true,
				Validators: []validator.Map{
					// Validate this Map must contain Int64 values which are at least 1.
					mapvalidator.ValueInt64sAre(int64validator.AtLeast(1)),
				},
			},
		},
	}
}
Output:

func ValueListsAre added in v0.7.0

func ValueListsAre(elementValidators ...validator.List) validator.Map

ValueListsAre returns an validator which ensures that any configured List values passes each List validator.

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

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
	"github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator"
	"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
	"github.com/hashicorp/terraform-plugin-framework/schema/validator"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.MapAttribute{
				// This Map has values of Lists of Strings.
				// Roughly equivalent to map[string][]string.
				ElementType: types.ListType{
					ElemType: types.StringType,
				},
				Required: true,
				Validators: []validator.Map{
					// Validate this Map must contain List elements
					// which have at least 1 String element.
					mapvalidator.ValueListsAre(listvalidator.SizeAtLeast(1)),
				},
			},
		},
	}
}
Output:

func ValueMapsAre added in v0.7.0

func ValueMapsAre(elementValidators ...validator.Map) validator.Map

ValueMapsAre returns an validator which ensures that any configured Map values passes each Map validator.

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

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator"
	"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
	"github.com/hashicorp/terraform-plugin-framework/schema/validator"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.MapAttribute{
				// This Map has values of Maps of Strings.
				// Roughly equivalent to map[string]map[string]string.
				ElementType: types.MapType{
					ElemType: types.StringType,
				},
				Required: true,
				Validators: []validator.Map{
					// Validate this Map must contain Map elements
					// which have at least 1 element.
					mapvalidator.ValueMapsAre(mapvalidator.SizeAtLeast(1)),
				},
			},
		},
	}
}
Output:

func ValueNumbersAre added in v0.7.0

func ValueNumbersAre(elementValidators ...validator.Number) validator.Map

ValueNumbersAre returns an validator which ensures that any configured Number values passes each Number validator.

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

Example
package main

import (
	"math/big"

	"github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator"
	"github.com/hashicorp/terraform-plugin-framework-validators/numbervalidator"
	"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
	"github.com/hashicorp/terraform-plugin-framework/schema/validator"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.MapAttribute{
				ElementType: types.NumberType,
				Required:    true,
				Validators: []validator.Map{
					// Validate this Map must contain Number values which are 1.2 or 2.4.
					mapvalidator.ValueNumbersAre(
						numbervalidator.OneOf(
							big.NewFloat(1.2),
							big.NewFloat(2.4),
						),
					),
				},
			},
		},
	}
}
Output:

func ValueSetsAre added in v0.7.0

func ValueSetsAre(elementValidators ...validator.Set) validator.Map

ValueSetsAre returns an validator which ensures that any configured Set values passes each Set validator.

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

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator"
	"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
	"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
	"github.com/hashicorp/terraform-plugin-framework/schema/validator"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.MapAttribute{
				// This Map has values of Sets of Strings.
				// Roughly equivalent to map[string][]string.
				ElementType: types.SetType{
					ElemType: types.StringType,
				},
				Required: true,
				Validators: []validator.Map{
					// Validate this Map must contain Set elements
					// which have at least 1 String element.
					mapvalidator.ValueSetsAre(setvalidator.SizeAtLeast(1)),
				},
			},
		},
	}
}
Output:

func ValueStringsAre added in v0.7.0

func ValueStringsAre(elementValidators ...validator.String) validator.Map

ValueStringsAre returns an validator which ensures that any configured String values passes each String validator.

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

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator"
	"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
	"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
	"github.com/hashicorp/terraform-plugin-framework/schema/validator"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func main() {
	// Used within a Schema method of a DataSource, Provider, or Resource
	_ = schema.Schema{
		Attributes: map[string]schema.Attribute{
			"example_attr": schema.MapAttribute{
				ElementType: types.StringType,
				Required:    true,
				Validators: []validator.Map{
					// Validate this Map must contain string values which are at least 3 characters.
					mapvalidator.ValueStringsAre(stringvalidator.LengthAtLeast(3)),
				},
			},
		},
	}
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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