Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

regex - How to validate a character set in terraform variable?

i need to validate a variable in terraform. The content of the variable should be only 0-9, a-z and A-Z. Im tried it with following code:

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."
  }
}

It doesen't work. When i set abcd- in the variable the validation returns true.

How can i fix the regex ?

Thanks for help ;)


@vgersh99 This doesn't work for me:

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."
  }
}

Here is the error:

$ 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
question from:https://stackoverflow.com/questions/65905029/how-to-validate-a-character-set-in-terraform-variable

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The regex function attempts to match a substring of the given string against the specified pattern, so the pattern in your first example will succeed as long as there is at least one ASCII digit or letter in the input.

To implement the rule you described you'll need to expand the pattern to cover the entire string. There are three parts of the regular expression syntax you can use together to achieve that:

  • The ^ symbol matches only at the start of the given string.
  • The $ symbol matches only at the end of the given string.
  • The + operator allows the preceding pattern to appear one or more times.

Putting those together, we get the pattern ^[0-9A-Za-z]+$: the beginning of the string, followed by one or more ASCII letters or digits, followed by the end of the string. This pattern will therefore only succeed if the entire string matches it.

Putting that into your full example gives us the following:

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."
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...