stringvalidator

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2023 License: MPL-2.0 Imports: 10 Imported by: 176

Documentation

Overview

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

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/stringvalidator"
	"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.StringAttribute{
				Required: true,
				Validators: []validator.String{
					// Validate this String value must either be:
					//  - "one"
					//  - Length at least 4 characters, but not "three"
					stringvalidator.Any(
						stringvalidator.OneOf("one"),
						stringvalidator.All(
							stringvalidator.LengthAtLeast(4),
							stringvalidator.NoneOf("three"),
						),
					),
				},
			},
		},
	}
}
Output:

func AlsoRequires added in v0.7.0

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

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/stringvalidator"
	"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.StringAttribute{
				Optional: true,
				Validators: []validator.String{
					// Validate this attribute must be configured with other_attr.
					stringvalidator.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.String) validator.String

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/stringvalidator"
	"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.StringAttribute{
				Required: true,
				Validators: []validator.String{
					// Validate this String value must either be:
					//  - "one"
					//  - Length at least 4 characters
					stringvalidator.Any(
						stringvalidator.OneOf("one"),
						stringvalidator.LengthAtLeast(4),
					),
				},
			},
		},
	}
}
Output:

func AnyWithAllWarnings added in v0.7.0

func AnyWithAllWarnings(validators ...validator.String) validator.String

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/stringvalidator"
	"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.StringAttribute{
				Required: true,
				Validators: []validator.String{
					// Validate this String value must either be:
					//  - "one"
					//  - Length at least 4 characters
					stringvalidator.AnyWithAllWarnings(
						stringvalidator.OneOf("one"),
						stringvalidator.LengthAtLeast(4),
					),
				},
			},
		},
	}
}
Output:

func AtLeastOneOf added in v0.7.0

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

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/stringvalidator"
	"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.StringAttribute{
				Optional: true,
				Validators: []validator.String{
					// Validate at least this attribute or other_attr should be configured.
					stringvalidator.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.String

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/stringvalidator"
	"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.StringAttribute{
				Optional: true,
				Validators: []validator.String{
					// Validate this attribute must not be configured with other_attr.
					stringvalidator.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.String

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

func LengthAtLeast

func LengthAtLeast(minLength int) validator.String

LengthAtLeast returns an validator which ensures that any configured attribute value is of single-byte character length greater than or equal to the given minimum. Null (unconfigured) and unknown (known after apply) values are skipped.

Use UTF8LengthAtLeast for checking multiple-byte characters.

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
	"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.StringAttribute{
				Required: true,
				Validators: []validator.String{
					// Validate string value length must be at least 3 characters.
					stringvalidator.LengthAtLeast(3),
				},
			},
		},
	}
}
Output:

func LengthAtMost

func LengthAtMost(maxLength int) validator.String

LengthAtMost returns an validator which ensures that any configured attribute value is of single-byte character length less than or equal to the given maximum. Null (unconfigured) and unknown (known after apply) values are skipped.

Use UTF8LengthAtMost for checking multiple-byte characters.

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
	"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.StringAttribute{
				Required: true,
				Validators: []validator.String{
					// Validate string value length must be at most 256 characters.
					stringvalidator.LengthAtMost(256),
				},
			},
		},
	}
}
Output:

func LengthBetween

func LengthBetween(minLength, maxLength int) validator.String

LengthBetween returns a validator which ensures that any configured attribute value is of single-byte character length 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.

Use UTF8LengthBetween for checking multiple-byte characters.

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
	"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.StringAttribute{
				Required: true,
				Validators: []validator.String{
					// Validate string value length must be at least 3 and at most 256 characters.
					stringvalidator.LengthBetween(3, 256),
				},
			},
		},
	}
}
Output:

func NoneOf added in v0.3.0

func NoneOf(values ...string) validator.String

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

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
	"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.StringAttribute{
				Required: true,
				Validators: []validator.String{
					// Validate string value must not be "one", "two", or "three"
					stringvalidator.NoneOf([]string{"one", "two", "three"}...),
				},
			},
		},
	}
}
Output:

func NoneOfCaseInsensitive added in v0.3.0

func NoneOfCaseInsensitive(values ...string) validator.String

NoneOfCaseInsensitive checks that the String held in the attribute is none of the given `values`.

func OneOf added in v0.3.0

func OneOf(values ...string) validator.String

OneOf checks that the String held in the attribute is one of the given `values`.

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
	"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.StringAttribute{
				Required: true,
				Validators: []validator.String{
					// Validate string value must be "one", "two", or "three"
					stringvalidator.OneOf([]string{"one", "two", "three"}...),
				},
			},
		},
	}
}
Output:

func OneOfCaseInsensitive added in v0.3.0

func OneOfCaseInsensitive(values ...string) validator.String

OneOfCaseInsensitive checks that the String held in the attribute is one of the given `values`.

func RegexMatches

func RegexMatches(regexp *regexp.Regexp, message string) validator.String

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

Null (unconfigured) and unknown (known after apply) values are skipped. Optionally an error message can be provided to return something friendlier than "value must match regular expression 'regexp'".

Example
package main

import (
	"regexp"

	"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
	"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.StringAttribute{
				Required: true,
				Validators: []validator.String{
					// Validate string value satisfies the regular expression for alphanumeric characters
					stringvalidator.RegexMatches(
						regexp.MustCompile(`^[a-zA-Z0-9]*$`),
						"must only contain only alphanumeric characters",
					),
				},
			},
		},
	}
}
Output:

func UTF8LengthAtLeast added in v0.9.0

func UTF8LengthAtLeast(minLength int) validator.String

UTF8LengthAtLeast returns an validator which ensures that any configured attribute value is of UTF-8 character count greater than or equal to the given minimum. Null (unconfigured) and unknown (known after apply) values are skipped.

Use LengthAtLeast for checking single-byte character counts.

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
	"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.StringAttribute{
				Required: true,
				Validators: []validator.String{
					// Validate UTF-8 character count must be at least 3 characters.
					stringvalidator.UTF8LengthAtLeast(3),
				},
			},
		},
	}
}
Output:

func UTF8LengthAtMost added in v0.9.0

func UTF8LengthAtMost(maxLength int) validator.String

UTF8LengthAtMost returns an validator which ensures that any configured attribute value is of UTF-8 character count less than or equal to the given maximum. Null (unconfigured) and unknown (known after apply) values are skipped.

Use LengthAtMost for checking single-byte character counts.

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
	"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.StringAttribute{
				Required: true,
				Validators: []validator.String{
					// Validate UTF-8 character count must be at most 255 characters.
					stringvalidator.UTF8LengthAtMost(255),
				},
			},
		},
	}
}
Output:

func UTF8LengthBetween added in v0.9.0

func UTF8LengthBetween(minLength int, maxLength int) validator.String

UTF8LengthBetween returns an validator which ensures that any configured attribute value is of UTF-8 character count 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.

Use LengthBetween for checking single-byte character counts.

Example
package main

import (
	"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
	"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.StringAttribute{
				Required: true,
				Validators: []validator.String{
					// Validate UTF-8 character count must be at least 3 characters
					// and at most 255 characters.
					stringvalidator.UTF8LengthBetween(3, 255),
				},
			},
		},
	}
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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