I have a number_of_servers field in schema.schema and I need to set a range to it. Is there any way to do it?
Schema: map[string]*schema.Schema{
"number_of_servers": {
Type: schema.TypeString,
Required: true,
Range: 1-5,
},
Generally speaking validation functions are meant to validate any configs (possibly combined with variables). These run as part of terraform plan
, terraform apply
and terraform destroy
. The user can also trigger explicit validation via terraform validate
which is commonly used in CI.
Assuming that "Range" is meant to limit minimum & maximum number of characters in a given string you can use StringLenBetween
from the validation package.
Schema: map[string]*schema.Schema{
"number_of_servers": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 5),
},
You can see it being used in practice here: https://github.com/terraform-providers/terraform-provider-aws/blob/46bff11/aws/resource_aws_cloudwatch_event_target.go#L71-L75