Documentation
¶
Overview ¶
Package stringvalidator provides validators for types.String attributes.
Index ¶
- func LengthAtLeast(minLength int) tfsdk.AttributeValidator
- func LengthAtMost(maxLength int) tfsdk.AttributeValidator
- func LengthBetween(minLength, maxLength int) tfsdk.AttributeValidator
- func NoneOf(unacceptableStrings ...string) tfsdk.AttributeValidator
- func NoneOfCaseInsensitive(unacceptableStrings ...string) tfsdk.AttributeValidator
- func OneOf(acceptableStrings ...string) tfsdk.AttributeValidator
- func OneOfCaseInsensitive(acceptableStrings ...string) tfsdk.AttributeValidator
- func RegexMatches(regexp *regexp.Regexp, message string) tfsdk.AttributeValidator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LengthAtLeast ¶
func LengthAtLeast(minLength int) tfsdk.AttributeValidator
LengthAtLeast returns an AttributeValidator which ensures that any configured attribute value:
- Is a string.
- Is of length 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/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.StringType,
Validators: []tfsdk.AttributeValidator{
// Validate string value length must be at least 3 characters.
stringvalidator.LengthAtLeast(3),
},
},
},
}
}
func LengthAtMost ¶
func LengthAtMost(maxLength int) tfsdk.AttributeValidator
LengthAtMost returns an AttributeValidator which ensures that any configured attribute value:
- Is a string.
- Is of length 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/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.StringType,
Validators: []tfsdk.AttributeValidator{
// Validate string value length must be at most 256 characters.
stringvalidator.LengthAtMost(256),
},
},
},
}
}
func LengthBetween ¶
func LengthBetween(minLength, maxLength int) tfsdk.AttributeValidator
LengthBetween returns an AttributeValidator which ensures that any configured attribute value:
- Is a string.
- Is of length greater than the given minimum and less than the given maximum.
Null (unconfigured) and unknown (known after apply) values are skipped.
Example ¶
package main
import (
"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.StringType,
Validators: []tfsdk.AttributeValidator{
// Validate string value length must be at least 3 and at most 256 characters.
stringvalidator.LengthBetween(3, 256),
},
},
},
}
}
func NoneOf ¶ added in v0.3.0
func NoneOf(unacceptableStrings ...string) tfsdk.AttributeValidator
NoneOf checks that the string held in the attribute is none of the given `unacceptableStrings`.
Example ¶
package main
import (
"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.StringType,
Validators: []tfsdk.AttributeValidator{
// Validate string value must not be "one", "two", or "three"
stringvalidator.NoneOf([]string{"one", "two", "three"}...),
},
},
},
}
}
func NoneOfCaseInsensitive ¶ added in v0.3.0
func NoneOfCaseInsensitive(unacceptableStrings ...string) tfsdk.AttributeValidator
NoneOfCaseInsensitive checks that the string held in the attribute is none of the given `unacceptableStrings`, irrespective of case sensitivity.
func OneOf ¶ added in v0.3.0
func OneOf(acceptableStrings ...string) tfsdk.AttributeValidator
OneOf checks that the string held in the attribute is one of the given `acceptableStrings`.
Example ¶
package main
import (
"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.StringType,
Validators: []tfsdk.AttributeValidator{
// Validate string value must be "one", "two", or "three"
stringvalidator.OneOf([]string{"one", "two", "three"}...),
},
},
},
}
}
func OneOfCaseInsensitive ¶ added in v0.3.0
func OneOfCaseInsensitive(acceptableStrings ...string) tfsdk.AttributeValidator
OneOfCaseInsensitive checks that the string held in the attribute is one of the given `acceptableStrings`, irrespective of case sensitivity.
func RegexMatches ¶
func RegexMatches(regexp *regexp.Regexp, message string) tfsdk.AttributeValidator
RegexMatches returns an AttributeValidator which ensures that any configured attribute value:
- Is a string.
- Matches the given regular expression https://github.com/google/re2/wiki/Syntax.
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/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.StringType,
Validators: []tfsdk.AttributeValidator{
// Validate string value satisfies the regular expression for alphanumeric characters
stringvalidator.RegexMatches(
regexp.MustCompile(`^[a-zA-Z0-9]*$`),
"must only contain only alphanumeric characters",
),
},
},
},
}
}
Types ¶
This section is empty.