mapvalidator

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2022 License: MPL-2.0 Imports: 7 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 KeysAre

func KeysAre(keyValidators ...tfsdk.AttributeValidator) tfsdk.AttributeValidator
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/tfsdk"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func main() {
	// Used within a GetSchema method of a DataSource, Provider, or Resource
	_ = tfsdk.Schema{
		Attributes: map[string]tfsdk.Attribute{
			"example_attr": {
				Required: true,
				Type: types.MapType{
					ElemType: types.StringType,
				},
				Validators: []tfsdk.AttributeValidator{
					// 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) tfsdk.AttributeValidator

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/tfsdk"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

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

func SizeAtMost

func SizeAtMost(max int) tfsdk.AttributeValidator

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/tfsdk"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

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

func SizeBetween

func SizeBetween(min, max int) tfsdk.AttributeValidator

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/tfsdk"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

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

func ValuesAre

func ValuesAre(valueValidators ...tfsdk.AttributeValidator) tfsdk.AttributeValidator

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

  • Is a Map.
  • Contains Map elements, each of which validate against each value 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/tfsdk"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func main() {
	// Used within a GetSchema method of a DataSource, Provider, or Resource
	_ = tfsdk.Schema{
		Attributes: map[string]tfsdk.Attribute{
			"example_attr": {
				Required: true,
				Type: types.MapType{
					ElemType: types.StringType,
				},
				Validators: []tfsdk.AttributeValidator{
					// Validate this map must contain string values which are at least 3 characters.
					mapvalidator.ValuesAre(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