You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
add a validation block that fails the evaluation with a message - #75
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.
validation {
condition=length(try(req.composite.spec.parameters.subnets, [])) >0error_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
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.
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>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Validation bypasses documented type checks for non-string error messages when conditions are true and for known non-boolean conditions containing unknown values.
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>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Validation fails to reject some known non-boolean conditions and non-string messages, while the new pass also causes locals to be evaluated twice per reconciliation.
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 reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Reevaluating group conditions between validation and rendering can let time-dependent conditions activate a group whose failing validations were skipped, allowing invalid resources to render.
No open issues 🎉
1 previously missed issue
In code that hasn't changed since last review
Reuse validation scopes to prevent skipped checks during rendering
function/internal/evaluator/validation.go:28
The validation pass and processGroup evaluate locals and group conditions separately, so a condition using timestamp() can change from false to true between passes. In that case, validation is skipped here but rendering enters the group and ignores its validation blocks, allowing resources to render even when a validation has condition = false. Reuse the evaluated scope contexts and group-condition results across both passes so rendering cannot enter a group whose validations were skipped.
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>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Human assessment is needed because locals are recomputed after validation without revalidation, potentially allowing rendered values to violate constraints they previously passed.
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
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.
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.