Add JSON Schema extraction via schema() builtin function#444
Open
nbeerbower wants to merge 1 commit into
Open
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds support for extracting JSON Schema-compatible objects from Hemlock's
definetypes via a newschema()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 withtype,name,properties, andrequiredfieldsCompiler support - Added codegen for the
schema()builtin in the compiler backend with proper type checking (returnsCHECKED_OBJECT)Interpreter support - Implemented
builtin_schema()in a newschema.cfile with type mapping functions for both JSON Schema types and Hemlock type namesRuntime support - Added
hml_schema()function in the runtime'sbuiltins_types.cfor the compiled code pathStandard library helpers - Added three convenience functions to
@stdlib/json:to_json_schema(type_name)- Wrapper around the builtinvalidate_schema(data, schema_obj)- Validates data against a schema objectvalidate_type(data, type_name)- Convenience combining both above functionsDocumentation - Updated
stdlib/docs/json.mdwith comprehensive schema validation guide including usage examples and validation rulesTests - Added comprehensive test coverage:
tests/parity/builtins/schema.hml- Tests basic schema extraction for multiple define typestests/stdlib_json/schema_validation_test.hml- Tests validation functions with valid/invalid data, missing fields, type mismatches, etc.Implementation Details
"string","integer","number", etc.) and Hemlock type names ("i32","f64", etc.) for better type informationrequired: falsein the schema$reffield pointing to the referenced type name"integer"schema type)https://claude.ai/code/session_012dzceQ2ppdmkSym3fxLAWA