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:- Relations (
relations.rs) - Entity references and foreign key consistency - Constraints (
constraints.rs) - Primary keys and annotation rules - Orchestration (
mod.rs) - Pipeline coordination and error reporting
Architecture
Entry Point
Location:chameleon-core/src/typechecker/mod.rs:36
- 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
Relations Validation
Location:chameleon-core/src/typechecker/relations.rs
Check Relations
UnknownRelationTarget- Target entity doesn’t existInvalidForeignKey- Foreign key field doesn’t exist in targetMissingForeignKey- HasMany withoutviaclause
Circular Dependency Detection
chameleon-core/src/typechecker/relations.rs:64
BelongsTois the inverse ofHasOne/HasMany- It doesn’t create a new dependency, just references the parent
- Example:
Post.user (BelongsTo User)doesn’t addPost → Useredge
Constraints Validation
Location:chameleon-core/src/typechecker/constraints.rs
Primary Key Validation
- 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 | Allowed Types | Constraints |
|---|---|---|
@cache | Any | ❌ Not on primary/unique |
@olap | Any | ❌ Not on primary/unique |
@vector | vector(N) only | ❌ Not on primary/unique |
@ml | Any | ❌ Not on primary/unique |
- 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
- Uses
thiserrorfor automaticDisplayimplementation - Structured fields for programmatic access (e.g., CI tooling)
- Human-readable messages with context
Performance Characteristics
| Operation | Time Complexity | Space Complexity | Notes |
|---|---|---|---|
| Relations check | O(E × R × F) | O(1) | E=entities, R=relations/entity, F=fields |
| Cycle detection | O(E + R) | O(E) | DFS graph traversal |
| Primary keys | O(E × F) | O(1) | Linear scan per entity |
| Annotations | O(E × F) | O(1) | Linear scan per entity |
| Total | O(E × F) | O(E) | Dominated by field scans |
- 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
Integration with FFI
The type checker is exposed to Go via FFI (see FFI Interface): Location:chameleon-core/src/ffi/mod.rs:106
See Also
- Parser and AST - AST construction before type checking
- FFI Interface - Exposing validation to Go