Skip to content

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

Closed
ulfox wants to merge 5 commits into
crossplane-contrib:mainfrom
ulfox:feature/validation-block
Closed

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

Conversation

@ulfox

@ulfox ulfox commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator

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.

@gotwarlost

Copy link
Copy Markdown
Collaborator

can you also add support for this in the language server?

ulfox added 2 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>
@ulfox
ulfox force-pushed the feature/validation-block branch from 2c82f4e to 5d9fa2e Compare October 29, 2026 13:09
@ulfox

ulfox commented Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

can you also add support for this in the language server?

I added language server support + DCO sign-off

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

Validation bypasses documented type checks for non-string error messages when conditions are true and for known non-boolean conditions containing unknown values.

2 open issues
What changed in this PR

Adds validation blocks so compositions can reject invalid input with an author-defined error message, both at the top level and inside groups.

Changes:

  • Adds evaluator support for validation failures and deferred conditions.
  • Adds language-server schemas, completion, and hover support.
  • Documents validation behavior and adds evaluator and editor tests.
File Description
language-server/​internal/​funchcl/​schema/​schemas.go Defines validation blocks and required attributes.
language-server/​internal/​funchcl/​schema/​schema_test.go Checks schema requirements 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 failure conditions.
function/​internal/​evaluator/​validation.go Evaluates conditions and produces validation diagnostics.
function/​internal/​evaluator/​validation_test.go Covers successful, failed, incomplete, and skipped validations.
function/​internal/​evaluator/​schemas.go Registers validation syntax with the evaluator.
function/​internal/​evaluator/​resources.go Dispatches validation blocks during group processing.
function/​internal/​evaluator/​api.go Adds validation block, attribute, and discard constants.
docs-site/​content/​en/​docs/​language-guide/​validation.md Documents syntax, behavior, and editor support.

Review effort: Balanced

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>

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

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.

3 open issues
2 resolved since last review

Review effort: Balanced

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>

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.

🔵 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

Medium severity 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.

3 resolved since last review

Review effort: Balanced

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>

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.

🔵 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.

No open issues 🎉

Review effort: Balanced

@gotwarlost

Copy link
Copy Markdown
Collaborator

make it message instead of error_message since we do not otherwise use snake case anywhere

@gotwarlost

Copy link
Copy Markdown
Collaborator

superseded by #77

@gotwarlost gotwarlost closed this Nov 10, 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.

3 participants