Repository navigation
Conversation
A `validation` block fails the evaluation with its `error_message` when its `condition` is false. It can
appear at the top level and in groups.
```hcl
validation {
condition = length(try(req.composite.spec.parameters.subnets, [])) > 0
error_message = "subnets must list at least one subnet"
}
```
```
evaluate hcl: main.hcl:2,19-77: validation failed; subnets must list at least one subnet
```
Without it, a composition cant reject an invalid input. An expression error only skips the resources
that use it, so a new composite renders nothing and reports `Ready`, and the error reaches the composite
only as a location and a summary, never as a message the author wrote.
| Condition | Result |
|---|---|
| `true` | nothing happens |
| `false` | the evaluation fails with `validation failed; <error_message>`, for new and existing composites alike |
| cant be evaluated yet | the block is skipped with a warning, like other incomplete values |
| not a bool, or `error_message` not a string | the evaluation fails |
A condition that cant be evaluated is skipped rather than failed because a dependency that isnt ready
yet raises the same errors (for example `Unsupported attribute` on `req.resource.<name>`), so failing
would block dependencies from resolving. Optional inputs are read with `try` or `can`, so that a missing
value makes the condition false.
Compositions without a `validation` block evaluate as before: `validation` was not an accepted block type.
The language server doesnt know the block yet, so editors mark it as unknown; that can follow separately.
## Testing
- `validation_test.go`, through `Eval`: condition true; false for a new composite; false for an existing
composite; not known yet; not a bool; inside a group whose condition is false; `error_message` missing.
- `make test` (Go 1.25) and `make lint` pass; all examples render as before.
- Rendered against a real composition (`XGcpServiceAttachment` in elastic/mki-provisioning-gcp) with its
input rules as `validation` blocks: every invalid input fails with its rules message, and every valid
input renders the same resources as without the blocks.
Signed-off-by: Christos Kotsis <christos.kotsis@elastic.co>
Completion offers the validation block at the top level and in groups, and its condition and error_message attributes; hover shows their descriptions. Signed-off-by: Christos Kotsis <christos.kotsis@elastic.co>
A validation block must fail the evaluation with its own message
whenever its condition is false. Five cases broke that promise or
reported a broken block too late. Each case below shows the HCL, the
result before this commit and the result after it.
1. A resource that reads the invalid input hid the validation message
resource network {
body = { ... subnet = req.composite.spec.parameters.subnets[0] ... }
}
validation {
condition = length(try(req.composite.spec.parameters.subnets, [])) > 0
error_message = "subnets must list at least one subnet"
}
Input without subnets, existing composite:
Before: main.hcl:6,121-129: Unsupported attribute; This object
does not have an attribute named "subnets"., and 2 other
diagnostic(s)
After: main.hcl:10,19-77: validation failed; subnets must list at
least one subnet
2. A false condition with a message that could not be built lost the
validation failure
validation {
condition = can(req.composite.spec.parameters.zone)
error_message = "zone ${req.composite.spec.parameters.zone} is required"
}
Input without zone:
Before: main.hcl:3,56-61: Unsupported attribute; This object does
not have an attribute named "zone".
After: main.hcl:2,19-58: validation failed; error_message could not
be evaluated: req.composite.spec.parameters.zone,
main.hcl:3,56-61: Unsupported attribute; ...
3. A condition of the wrong type was skipped while part of it was not
known yet (Copilot review)
locals {
arn = req.resource.network.status.atProvider.arn
}
validation {
condition = [arn]
error_message = "never shown"
}
New composite, network not created yet:
Before: the block was skipped with a warning, and the evaluation
succeeded.
After: main.hcl:5,19-24: validation condition must be a bool; got
tuple
`[arn]` is a list, never a bool, so the block can never work. The
type is checked before the "not known yet" check now. A condition
whose type is not known yet is still skipped.
4. An error_message of the wrong type passed while the condition was
true (Copilot review)
validation {
condition = true
error_message = 123
}
Before: the evaluation succeeded. The broken message showed up only
when the input became invalid, as a type error instead of the
rules message.
After: main.hcl:3,19-22: validation error_message must be a string;
got number
The message is evaluated on every pass now. A known type other
than string fails at once. A message that cannot be evaluated while
the condition is true is ignored, because a message may read values
that exist only when the input is invalid:
error_message = "zone ${req.composite.spec.parameters.zone} is not allowed"
5. The warning for a skipped block did not say what was skipped
validation {
condition = req.resource.network.status.atProvider.arn != ""
error_message = "the network has no ARN"
}
New composite, network not created yet:
Before: main.hcl:2,19-67:discarded validation
After: main.hcl:2,19-67:discarded validation condition
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christos Kotsis <christos.kotsis@elastic.co>
Three cases from the Copilot review: the pass that runs validation
blocks first evaluated every local twice, and two type checks were
skipped when part of the value had an evaluation error.
1. Locals were evaluated twice in every composition
The validation blocks run in a first pass, before any other block.
That pass evaluated the locals of the top level and of every group
whose condition is true, and the normal pass evaluated them again.
Every composition paid for it, including the ones without a
validation block.
The first pass now skips any part of the composition that has no
validation block, before evaluating its locals. A composition
without validation blocks evaluates its locals once, as before this
feature. A group with a validation block still runs its rules before
any other block.
2. A condition of the wrong type was skipped when part of it failed to
evaluate
validation {
condition = [req.resource["primary-bucket"].status.atProvider.arn]
error_message = "never shown"
}
New composite, bucket not created yet:
Before: the block was skipped with a warning.
After: validation condition must be a bool; got tuple
The missing bucket made the evaluation fail, and the type was only
checked when it succeeded. The condition is a list whatever the
bucket's state, so the type is now checked first.
3. An error_message of the wrong type passed in two cases
validation {
condition = true
error_message = [req.composite.spec.parameters.missing]
}
Before: the evaluation succeeded.
After: validation error_message must be a string; got tuple
The same happened with a literal message, such as 123, while the
condition could not be evaluated yet: the block was skipped before
the message was checked. The message type is now checked first, and
also when its evaluation fails. A message that cannot be evaluated
is still only an error when the condition is false, because it may
read values that exist only when the input is invalid.
Compositions without validation blocks render the same as before: the
examples and the 26 test inputs of XGcpServiceAttachment in
elastic/mki-provisioning-gcp are unchanged, and so is that
composition's output with its rules written as validation blocks.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christos Kotsis <christos.kotsis@elastic.co>
The validation pass and the render pass evaluate a group's condition
separately. A condition that calls timestamp() or uuid() can be false
in the first pass and true in the second, so the group rendered its
resources without running its validation blocks.
group {
condition = timestamp() > "2026-11-01T00:00:00Z"
validation {
condition = false
error_message = "the group input is invalid"
}
...
}
The deadline passes between the two passes:
Before: the group's resources render.
After: validation failed; the group input is invalid
The render pass now runs any validation block that the validation pass
skipped, in source order with the group's other blocks. A group that
renders always has its validations evaluated. Inputs whose group
conditions give the same value in both passes evaluate as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christos Kotsis <christos.kotsis@elastic.co>
ulfox
requested review from
gotwarlost
and
a balanced review from Copilot
and removed request for
gotwarlost
November 10, 2026 16:36
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Locals are recomputed after validation while previously checked rules are skipped, allowing changing values to reach rendered resources without satisfying their validation rules.
1 open issue
What changed in this PR
Adds validation blocks to function-hcl so compositions can reject invalid input with author-defined error messages, at the top level or inside groups.
Changes:
- Adds validation evaluation before rendering, with type checks and warnings for incomplete conditions.
- Adds evaluator tests and language-server schema, completion, and hover support.
- Documents validation behavior, execution order, and guidance for Terraform users.
| File | Description |
|---|---|
| language-server/internal/funchcl/schema/schemas.go | Defines validation blocks and required attributes. |
| language-server/internal/funchcl/schema/schema_test.go | Checks validation schema and allowed nesting. |
| language-server/internal/funchcl/decoder/completion/hover_test.go | Tests validation hover descriptions. |
| language-server/internal/funchcl/decoder/completion/completion_test.go | Tests block and attribute completion. |
| function/spec.md | Specifies validation behavior and ordering. |
| function/internal/evaluator/validation.go | Implements validation traversal and diagnostics. |
| function/internal/evaluator/validation_test.go | Tests validation through the evaluator API. |
| function/internal/evaluator/validation_internal_test.go | Tests discovery and changing group conditions. |
| function/internal/evaluator/schemas.go | Registers the validation block schema. |
| function/internal/evaluator/resources.go | Handles validations skipped by the initial pass. |
| function/internal/evaluator/eval.go | Runs validation before rendering. |
| function/internal/evaluator/api.go | Adds validation constants and tracking state. |
| docs-site/content/en/docs/reference/error-conditions.md | Lists validation error conditions. |
| docs-site/content/en/docs/reference/dsl-specification.md | Documents validation syntax and semantics. |
| docs-site/content/en/docs/language-guide/validation.md | Adds validation usage and behavior guidance. |
| docs-site/content/en/docs/language-guide/_index.md | Links the validation guide. |
| docs-site/content/en/docs/for-terraform-users/_index.md | Explains validation equivalents and XRD guidance. |
Review effort: Balanced
| if !hasValidation(content) { | ||
| return nil | ||
| } | ||
| ctx, ds := e.processLocals(ctx, content) |
This branch has not been deployed
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.

Same as #75. We opened it from a branch in the same repo because build fails from forks
A
validationblock fails the evaluation with itserror_messagewhen itsconditionis false. It can appear at the top level and in groups.Without it, a composition cant reject an invalid input. An expression error only skips the resources that use it, so a new composite renders nothing and reports
Ready, and the error reaches the composite only as a location and a summary, never as a message the author wrote.truefalsevalidation failed; <error_message>, for new and existing composites alikeA condition that cant be evaluated is skipped rather than failed because a dependency that isnt ready yet raises the same errors (for example
Unsupported attributeonreq.resource.<name>), so failing would block dependencies from resolving. Optional inputs are read withtryorcan, so that a missing value makes the condition false.Compositions without a
validationblock evaluate as before:validationwas not an accepted block type. The language server doesnt know the block yet, so editors mark it as unknown; that can follow separately.Testing
validation_test.go, throughEval: condition true; false for a new composite; false for an existing composite; not known yet; not a bool; inside a group whose condition is false;error_messagemissing.make test(Go 1.25) andmake lintpass; all examples render as before.XGcpServiceAttachmentin elastic/mki-provisioning-gcp) with its input rules asvalidationblocks: every invalid input fails with its rules message, and every valid input renders the same resources as without the blocks.