Skip to content

add a validation block that fails the evaluation with a message - #77

Open
ulfox wants to merge 5 commits into
mainfrom
feature/validation-block
Open

ulfox wants to merge 5 commits into
mainfrom
feature/validation-block

Conversation

@ulfox

@ulfox ulfox commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

Same as #75. We opened it from a branch in the same repo because build fails from forks

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, [])) > 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

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.

ulfox and others added 5 commits October 29, 2026 15:08
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
ulfox requested review from gotwarlost and a balanced review from Copilot and removed request for gotwarlost November 10, 2026 16:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

No deployments
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.

2 participants