diff --git a/test/fixtures/hclvalidate/valid/validation-block/main.tf b/test/fixtures/hclvalidate/valid/validation-block/main.tf new file mode 100644 index 0000000000..79dedd90ce --- /dev/null +++ b/test/fixtures/hclvalidate/valid/validation-block/main.tf @@ -0,0 +1,10 @@ +variable "example_var" { + type = string + description = "A variable that requires validation." + default = "test" + + validation { + condition = length(var.example_var) > 0 + error_message = "The example_var must not be empty." + } +} diff --git a/test/fixtures/hclvalidate/valid/validation-block/terragrunt.hcl b/test/fixtures/hclvalidate/valid/validation-block/terragrunt.hcl new file mode 100644 index 0000000000..65e2cc3404 --- /dev/null +++ b/test/fixtures/hclvalidate/valid/validation-block/terragrunt.hcl @@ -0,0 +1 @@ +// intentionally blank diff --git a/tf/tf.go b/tf/tf.go index ee59db8c89..bf0b080766 100644 --- a/tf/tf.go +++ b/tf/tf.go @@ -179,16 +179,25 @@ func ModuleVariables(modulePath string) ([]string, []string, error) { }, } + varsAttributesSchema := &hcl.BodySchema{ + Attributes: []hcl.AttributeSchema{ + { + Name: "default", + Required: false, + }, + }, + } + varsContent, _, contentDiags := body.PartialContent(varsSchema) allDiags = append(allDiags, contentDiags...) optional, required := []string{}, []string{} for _, b := range varsContent.Blocks { name := b.Labels[0] - attributes, attrDiags := b.Body.JustAttributes() + varBodyContent, _, attrDiags := b.Body.PartialContent(varsAttributesSchema) allDiags = append(allDiags, attrDiags...) - if _, ok := attributes["default"]; ok { + if _, ok := varBodyContent.Attributes["default"]; ok { optional = append(optional, name) } else { required = append(required, name)