Skip to main content

Type Checker

The type checker validates the parsed AST before runtime, catching errors like invalid relations, missing primary keys, and annotation misuse. It runs after parsing and before schema registration in the vault.

Overview

Validation is organized into three specialized modules:
  1. Relations (relations.rs) - Entity references and foreign key consistency
  2. Constraints (constraints.rs) - Primary keys and annotation rules
  3. Orchestration (mod.rs) - Pipeline coordination and error reporting

Architecture

Entry Point

Location: chameleon-core/src/typechecker/mod.rs:36
Key characteristics:
  • Non-short-circuiting: Collects all errors, not just the first
  • Zero allocation (where possible): Borrows schema, doesn’t modify
  • Deterministic: Same schema always produces same errors in same order

TypeCheckResult

Location: chameleon-core/src/typechecker/mod.rs:9
Example report:

Relations Validation

Location: chameleon-core/src/typechecker/relations.rs

Check Relations

Checks performed:
  • UnknownRelationTarget - Target entity doesn’t exist
  • InvalidForeignKey - Foreign key field doesn’t exist in target
  • MissingForeignKey - HasMany without via clause

Circular Dependency Detection

Algorithm: Depth-First Search (DFS) Location: chameleon-core/src/typechecker/relations.rs:64
Example cycle:
Why skip BelongsTo?
  • BelongsTo is the inverse of HasOne/HasMany
  • It doesn’t create a new dependency, just references the parent
  • Example: Post.user (BelongsTo User) doesn’t add Post → User edge

Constraints Validation

Location: chameleon-core/src/typechecker/constraints.rs

Primary Key Validation

Rules:
  • Every entity must have exactly one primary key
  • Composite primary keys are not supported in v1.0
  • Primary key must be a field (not a relation)

Annotation Validation

Annotation rules: Rationale:
  • Primary/unique fields must reside in the main OLTP database
  • They’re used for joins, indexes, and integrity constraints
  • Splitting them across backends breaks referential integrity

Error Types

Location: chameleon-core/src/typechecker/errors.rs
Error design:
  • Uses thiserror for automatic Display implementation
  • Structured fields for programmatic access (e.g., CI tooling)
  • Human-readable messages with context

Performance Characteristics

Benchmarks (typical schema: 20 entities, 10 fields each):
  • Type check: ~5ms
  • Error report generation: ~0.5ms
  • Memory overhead: ~2KB (visited sets)

Example Usage

Valid Schema

Invalid Schema

Annotation Errors

Testing

Location: chameleon-core/src/typechecker/mod.rs:98 Test coverage:
  • ✅ Valid simple schemas
  • ✅ Valid schemas with relations
  • ✅ Valid backend annotations
  • ✅ Unknown relation targets
  • ✅ Invalid foreign keys
  • ✅ Missing foreign keys (HasMany)
  • ✅ Missing primary keys
  • ✅ Multiple primary keys
  • ✅ Invalid vector annotations
  • ✅ Annotations on primary keys
  • ✅ Annotations on unique fields
  • ✅ Circular dependencies
  • ✅ Error report formatting
Example test:

Integration with FFI

The type checker is exposed to Go via FFI (see FFI Interface): Location: chameleon-core/src/ffi/mod.rs:106
JSON error format:

See Also