# Create validation for map(string)

**URL:** <https://discuss.hashicorp.com/t/create-validation-for-map-string/34330>\
**Category:** Terraform\
**Created:** [January 17, 2022, 3:20am UTC](https://discuss.hashicorp.com/t/create-validation-for-map-string/34330 "2022-01-17T03:20:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![captainfalcon23](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/captainfalcon23/32/25167_2.png) [@captainfalcon23](https://discuss.hashicorp.com/u/captainfalcon23)\
**Post date:** [January 17, 2022, 3:20am UTC](https://discuss.hashicorp.com/t/create-validation-for-map-string/34330/1 "2022-01-17T03:20:12Z")

</div>

Hello!

I am new to terraform and trying to figure out how we can create a validation on a variable where type is map(string) and we do not know the key names in advance.

Suppose we have a variable definition like:  
`variable "myvar" { type = map(string) default = {}`  
And suppose we define it like this in main.tf:  
`myvar = {"key1" : "val1", "key2":"val2"}`  
Now, I want to create a validation to check that the key always contains a string “key”.

How can I do it? Is it possible? I can’t find any relevant documentation.

NOTE: I am using this var in a “for\_each” which only supports maps and sets of strings e.g.  
`resource "myresource" "map" { for_each = var.myvar a = each.key b = each.value`

---

<div class="post-metadata">

**Author:** ![tbugfinder](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/tbugfinder/32/12689_2.png) [@tbugfinder](https://discuss.hashicorp.com/u/tbugfinder)\
**Post date:** [January 17, 2022, 10:14pm UTC](https://discuss.hashicorp.com/t/create-validation-for-map-string/34330/2 "2022-01-17T22:14:20Z")

</div>

This might be a starting point:

```auto
variable "mymap" {
  type = map(string)

  validation {
    condition = alltrue(
       [for obj in keys(var.mymap) : can(regex("^key.*",obj))]
    )
    error_message = "Each map key should include key string."
  }

  #default = {"key1" = "val1"}
  default = {"key1" = "val1", "key2" = "val2"}
  #default = {"okey1" = "val1", "key2" = "val2"}
  #default = {"ok1" = "val1", "key2" = "val2"}
}

```

---

<div class="post-metadata">

**Author:** ![captainfalcon23](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/captainfalcon23/32/25167_2.png) [@captainfalcon23](https://discuss.hashicorp.com/u/captainfalcon23)\
**Post date:** [January 18, 2022, 10:54am UTC](https://discuss.hashicorp.com/t/create-validation-for-map-string/34330/3 "2022-01-18T10:54:56Z")

</div>

Thanks heaps! Do you have any links to relevant documentation which explains this behaviour? Eg. Why are you using “alltrue”? And what structure can contains have? I’m finding it a little hard to find the documentation.

---

<div class="post-metadata">

**Author:** ![tbugfinder](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/tbugfinder/32/12689_2.png) [@tbugfinder](https://discuss.hashicorp.com/u/tbugfinder)\
**Post date:** [January 18, 2022, 7:34pm UTC](https://discuss.hashicorp.com/t/create-validation-for-map-string/34330/4 "2022-01-18T19:34:03Z")

</div>

I’m using the [terraform functions](https://www.terraform.io/language/functions) documentation. However the key is combining different functions properly. Therefore I’m following this discussion group or watch sometimes in reddit for interesting approaches and certainly ask when help is needed.

---

<div class="post-metadata">

**Author:** ![captainfalcon23](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/captainfalcon23/32/25167_2.png) [@captainfalcon23](https://discuss.hashicorp.com/u/captainfalcon23)\
**Post date:** [January 19, 2022, 1:55am UTC](https://discuss.hashicorp.com/t/create-validation-for-map-string/34330/5 "2022-01-19T01:55:40Z")

</div>

Awesome, Thankyou heaps 🙂

Cheers,  
Steve

Please excuse any typos!

> ![](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/tbugfinder/45/12689_2.png "tbugfinder") | [tbugfinder](https://discuss.hashicorp.com/u/tbugfinder)  
> January 18 |
> 
> - | - |

I’m using the [terraform functions](https://www.terraform.io/language/functions) documentation. However the key is combining different functions properly. Therefore I’m following this discussion group or watch sometimes in reddit for interesting approaches and certainly ask when help is needed.
