Skip to content

feat: Add limited type checking for validate command#5076

Merged
kalleep merged 25 commits intomainfrom
kalleep/validation-simple-typecheck
Feb 18, 2026
Merged

feat: Add limited type checking for validate command#5076
kalleep merged 25 commits intomainfrom
kalleep/validation-simple-typecheck

Conversation

@kalleep
Copy link
Contributor

@kalleep kalleep commented Dec 12, 2025

PR Description

Doing type checking without evaluation is not easy. But I figured we could start with the simplest case *ast.LiteralExpr.

Because we always know what the values are it's pretty straight forward to add. I also added support for other expressions but it all boils down to performing type checking on Literal expressions.

I also try to handle capsules correctly, where e.g. discovery.Target can be converted into a map.

Which issue(s) this PR fixes

Part of: #3712

Notes to the Reviewer

Next up we can try to type check *ast.AccessExrp but for that we need a bit more information passed and I figured I can do that in a follow up pr.

PR Checklist

  • CHANGELOG.md updated
  • Documentation added
  • Tests updated
  • Config converters updated

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements limited type checking for the validation command, starting with the simplest case of *ast.LiteralExpr expressions. The implementation includes type checking for literal values, arrays, objects, unary expressions, and binary expressions. The PR also handles capsule types correctly, allowing conversions such as discovery.Target to map types.

Key Changes

  • Added comprehensive type checking functions in syntax/typecheck/typecheck.go for various expression types (literal, array, object, unary, binary)
  • Refactored binary and unary operation functions from syntax/vm package to a new syntax/internal/transform package for reuse
  • Enhanced the value package to expose TryCapsuleConvert and add EncapsulateWithRv for better capsule handling

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
syntax/typecheck/typecheck.go Added type checking functions for literal, array, object, unary, and binary expressions with proper diagnostic reporting
syntax/typecheck/typecheck_test.go Extended test suite with new test cases covering wrong literal types, arrays, objects, and capsule type checking
syntax/vm/vm.go Refactored to use transform.BinaryOp and transform.UnaryOp instead of internal functions
syntax/internal/transform/unary.go Moved unary operation logic from vm package, renamed function to UnaryOp for public access
syntax/internal/transform/binary.go Moved binary operation logic from vm package, renamed function to BinaryOp for public access
syntax/internal/value/value.go Added EncapsulateWithRv function to create capsule values from reflect.Value
syntax/internal/value/decode.go Made TryCapsuleConvert public and simplified redundant error checking conditions
internal/validator/testdata/default/*.txtar Updated test data files to use appropriate types for proper validation testing
internal/validator/testdata/default/invalid_types.diags Added expected diagnostics for new invalid type test cases

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 5 comments.

Comments suppressed due to low confidence (1)

syntax/internal/value/value.go:105

  • The function documentation incorrectly states "Encapsulate panics" when it should say "EncapsulateWithRv panics" to match the actual function name.
// Encode creates a new Value from v. If v is a pointer, v must be considered
// immutable and not change while the Value is used.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.

@kalleep kalleep force-pushed the kalleep/validation-simple-typecheck branch from 4df3f02 to 70583f5 Compare December 15, 2025 07:52
@kalleep kalleep changed the title feat: implement limited type checking for validation command. feat: implement limited type checking for validation command Dec 15, 2025
@kalleep kalleep changed the title feat: implement limited type checking for validation command feat: add limited type checking for validation command Dec 15, 2025
@kalleep kalleep force-pushed the kalleep/validation-simple-typecheck branch from 1c12de4 to dde0acd Compare December 15, 2025 14:40
@kalleep kalleep changed the title feat: add limited type checking for validation command feat: add limited type checking for validate command Dec 15, 2025
@kalleep kalleep force-pushed the kalleep/validation-simple-typecheck branch from dde0acd to 2194d37 Compare January 19, 2026 08:11
@kalleep kalleep changed the title feat: add limited type checking for validate command feat: Add limited type checking for validate command Jan 19, 2026
kalleep and others added 12 commits February 18, 2026 15:39
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@kalleep kalleep force-pushed the kalleep/validation-simple-typecheck branch from 2194d37 to c67c499 Compare February 18, 2026 14:39
@github-actions
Copy link
Contributor

TruffleHog Scan Results

No secrets detected in this PR.

@kalleep kalleep merged commit 21702d6 into main Feb 18, 2026
48 of 51 checks passed
@kalleep kalleep deleted the kalleep/validation-simple-typecheck branch February 18, 2026 15:14
@grafana-alloybot grafana-alloybot bot mentioned this pull request Feb 18, 2026
jharvey10 pushed a commit that referenced this pull request Feb 26, 2026
#### PR Description
Doing type checking without evaluation is not easy. But I figured we
could start with the simplest case `*ast.LiteralExpr`.

Because we always know what the values are it's pretty straight forward
to add. I also added support for other expressions but it all boils down
to performing type checking on Literal expressions.

I also try to handle capsules correctly, where e.g. discovery.Target can
be converted into a map.

#### Which issue(s) this PR fixes

Part of: #3712

#### Notes to the Reviewer

Next up we can try to type check `*ast.AccessExrp` but for that we need
a bit more information passed and I figured I can do that in a follow up
pr.

#### PR Checklist

- [x] CHANGELOG.md updated
- [ ] Documentation added
- [x] Tests updated
- [ ] Config converters updated

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants