hashicorp / hashicorp/hcl

cmd/hcldec: evaluating variables

Open
#499 1 comment 1 reaction 0 assignees View on GitHub
cmd/hcldec
Dominant language
Go
Stars
5.8k
Forks
657
Avg merge
20h 36m
Merged PRs (30d)
6

Description

I'd like to validate `variable` blocks in order to require all variables to have a `description` field. I'is part of the validation stage for my CI pipeline.

This is the input I'm going to receive from my user:

```hcl
# uservars.tf
variable "one" {
description = "One variable"
type = string
}

variable "two" {
description = "Another variable"
type = number
default = 2
}
```

And this is the spec I've got so far.

```hcl
# variables.hcldec
block_map {
block_type = "variable"
labels = ["name"]

object {
attr "description" {
type = string
required = true
}
attr "type" {
type = any
required = true
}
attr "default" {
type = any
required = false
}
}
}
```

However `type=any` inside a `block_map` is not allowed, which produces the error below:

```
$ hcldec -s variables.hcldec uservars.tf
Error: Invalid block_map spec

on vars.hcldec line 1, in block_map:
1: block_map {

A block_map spec may not contain attributes with type 'any'.
```

Any tips?

PS:

Changing variables to use the now deprecated quoted type constraints (`type = "string"` and `type = "number"`) does works as expected, but since this syntax is deprecated we are not allowed to use it anymore.

```hcl
$ hcldec -s variables.hcldec uservars.tf
{"one":{"description":"One variable","type":"string"},"two":{"default":"2","description":"Abother variable","type":"number"}}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the command shown with variables.hcldec and uservars.tf, then inspect the cmd/hcldec handling of block_map specs and type constraints. The issue is done when modern type constraints can be evaluated in this validation workflow while the required description check remains effective, with behavior confirmed against the provided input.

Written by the indexing model from the issue text.

Assessment

Domain
cli
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.