Skip to content

Add JSON Schema extraction via schema() builtin function#444

Open
nbeerbower wants to merge 1 commit into
mainfrom
claude/hemlock-json-schema-validation-ETSuH
Open

Add JSON Schema extraction via schema() builtin function#444
nbeerbower wants to merge 1 commit into
mainfrom
claude/hemlock-json-schema-validation-ETSuH

Conversation

@nbeerbower

Copy link
Copy Markdown
Collaborator

Summary

This PR adds support for extracting JSON Schema-compatible objects from Hemlock's define types via a new schema() builtin function. This enables validation of JSON data against Hemlock's structural types.

Key Changes

  • New builtin function schema(type_name: string) - Extracts type metadata from the runtime type registry and returns a JSON Schema-compatible object with type, name, properties, and required fields

  • Compiler support - Added codegen for the schema() builtin in the compiler backend with proper type checking (returns CHECKED_OBJECT)

  • Interpreter support - Implemented builtin_schema() in a new schema.c file with type mapping functions for both JSON Schema types and Hemlock type names

  • Runtime support - Added hml_schema() function in the runtime's builtins_types.c for the compiled code path

  • Standard library helpers - Added three convenience functions to @stdlib/json:

    • to_json_schema(type_name) - Wrapper around the builtin
    • validate_schema(data, schema_obj) - Validates data against a schema object
    • validate_type(data, type_name) - Convenience combining both above functions
  • Documentation - Updated stdlib/docs/json.md with comprehensive schema validation guide including usage examples and validation rules

  • Tests - Added comprehensive test coverage:

    • tests/parity/builtins/schema.hml - Tests basic schema extraction for multiple define types
    • tests/stdlib_json/schema_validation_test.hml - Tests validation functions with valid/invalid data, missing fields, type mismatches, etc.

Implementation Details

  • Schema objects include both JSON Schema types ("string", "integer", "number", etc.) and Hemlock type names ("i32", "f64", etc.) for better type information
  • Optional fields are marked with required: false in the schema
  • Custom object types include a $ref field pointing to the referenced type name
  • Validation supports duck typing (extra fields accepted) and type category matching (all integer types match "integer" schema type)
  • Both compiler and interpreter backends are fully supported

https://claude.ai/code/session_012dzceQ2ppdmkSym3fxLAWA

Add a `schema()` built-in that extracts JSON Schema-compatible objects
from registered define types, enabling structured validation of JSON data
against Hemlock's type system.

Implementation:
- Interpreter: new builtin_schema() in builtins/schema.c
- Compiler: codegen for schema() call, runtime hml_schema() function
- @stdlib/json: to_json_schema(), validate_schema(), validate_type()
- Full interpreter/compiler parity

https://claude.ai/code/session_012dzceQ2ppdmkSym3fxLAWA
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