Skip to content

fix: added validation block in variable schema#841

Open
ritesh-harihar wants to merge 2 commits intomainfrom
fix-variables-declaration-doesnt-support-validation
Open

fix: added validation block in variable schema#841
ritesh-harihar wants to merge 2 commits intomainfrom
fix-variables-declaration-doesnt-support-validation

Conversation

@ritesh-harihar
Copy link
Collaborator

@ritesh-harihar ritesh-harihar commented Mar 16, 2026

Description
This PR adds support for HCL2-style validation blocks inside variable declarations. Previously, variable decoding rejected nested validation blocks with: “Blocks of type validation are not expected here.”

Issue:
When HCL's body.Content(schema) is called in the decoder, it compares the actual file content against this schema. Since validation wasn't listed as an allowed block type, HCL raised the error.

What changed

  1. Schema — Added validation as an allowed block type inside VariableBlockSchema, and defined a new ValidationBlockSchema requiring condition (expression) and error_message (string).

  2. Variable struct — Added a Validations []Validation field and a Validation type holding the condition expression, error message, and source range.

  3. Validation functions — Created validation_funcs.go exposing go-cty stdlib functions (contains, length, regex, regexall, comparison operators, etc.) so condition expressions can use them.

  4. Decoder — After parsing description/type/default, the decoder now iterates any validation blocks in the content, parses each one's condition expression and error_message string, and appends them to v.Validations.

  5. Variable.Validate() method — Evaluates each validation rule by building an hcl.EvalContext where var. resolves to the variable's current value, then calls rule.Condition.Value(ctx). If the result is false, it emits a diagnostic with the custom error_message.

  6. Both V1 and V2 parsers — After all overrides (env → file → CLI flags) are merged into root variables, both parsers now call v.Validate() on every variable and collect any failing diagnostics.

JIRA Ticket

Reminders

  • Add CHANGELOG.md entry
  • If a change needs to be reverted, we will roll out an update to the code within 7 days.

Changes to Security Controls

Are there any changes to security controls (access controls, encryption, logging) in this pull request? If so, explain.

@ritesh-harihar ritesh-harihar requested review from a team as code owners March 16, 2026 05:11
@ritesh-harihar ritesh-harihar changed the title added validation block in variable schema, added tests fix: added validation block in variable schema Mar 16, 2026
@ritesh-harihar ritesh-harihar linked an issue Mar 16, 2026 that may be closed by this pull request
@ritesh-harihar ritesh-harihar self-assigned this Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Variables declaration doesn't support validation stanza

1 participant