【发布时间】:2022-04-23 11:14:28
【问题描述】:
我需要验证 terraform 中的变量。变量的内容应该只有0-9、a-z和A-Z。我用下面的代码试了一下:
variable "application_name" {
type = string
default = "foo"
validation {
# regex(...) fails if it cannot find a match
condition = can(regex("([0-9A-Za-z])", var.application_name))
error_message = "For the application_name value only a-z, A-Z and 0-9 are allowed."
}
}
它不起作用。当我在变量中设置 abcd- 时,验证返回 true。
如何修复正则表达式?
感谢您的帮助;)
@vgersh99 这对我不起作用:
variable "application_name" {
type = string
default = "foo"
validation {
# regex(...) fails if it cannot find a match
condition = can(regex("[^[:alnum:]]", var.application_name))
error_message = "For the application_name value only a-z, A-Z and 0-9 are allowed."
}
}
这是错误:
$ terraform validate
Error: Invalid value for variable
on main.tf line 23, in module "ecs_cluster":
23: application_name = "frdlso"
For the application_name value only a-z, A-Z and 0-9 are allowed.
This was checked by the validation rule at
.terraform/modules/ecs_cluster/variables.tf:34,5-15
【问题讨论】:
-
你不是说
[^[:alnum:]]吗?