fix: added validation block in variable schema#841
Open
ritesh-harihar wants to merge 2 commits intomainfrom
Open
fix: added validation block in variable schema#841ritesh-harihar wants to merge 2 commits intomainfrom
ritesh-harihar wants to merge 2 commits intomainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Schema — Added validation as an allowed block type inside VariableBlockSchema, and defined a new ValidationBlockSchema requiring condition (expression) and error_message (string).
Variable struct — Added a Validations []Validation field and a Validation type holding the condition expression, error message, and source range.
Validation functions — Created validation_funcs.go exposing go-cty stdlib functions (contains, length, regex, regexall, comparison operators, etc.) so condition expressions can use them.
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.
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.
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
CHANGELOG.mdentryChanges to Security Controls
Are there any changes to security controls (access controls, encryption, logging) in this pull request? If so, explain.