Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSONSchema parser #1017

Merged
merged 1 commit into from
Jan 18, 2025
Merged

Add JSONSchema parser #1017

merged 1 commit into from
Jan 18, 2025

Conversation

jlowin
Copy link
Member

@jlowin jlowin commented Jan 18, 2025

This PR adds a new utility module jsonschema.py that converts JSON Schema definitions into Python types. This enables dynamic type creation and validation from JSON Schema specifications.

Key Features

  • Converts JSON Schema into Python types
  • Supports complex schema features including:
    • Basic types (string, number, integer, boolean, null)
    • Complex types (arrays, objects)
    • Format constraints (date-time, email, uri)
    • Numeric and string constraints (min/max, patterns, etc.)
    • Object properties with intelligent default handling
    • References and recursive schemas
    • Enums and constants
    • Union types

Example Usage

from marvin.utilities.jsonschema import jsonschema_to_type

schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string", "minLength": 1},
        "age": {"type": "integer", "minimum": 0},
        "email": {"type": "string", "format": "email"}
    },
    "required": ["name", "age"]
}

Person = jsonschema_to_type(schema)
person = Person(name="John", age=30)

Implementation Details

  • Uses modern Python type hints
  • Implements intelligent caching to handle recursive schemas
  • Provides robust default value handling with proper inheritance
  • Sanitizes field names for Python compatibility
  • Maintains original field names through aliases

Testing

The module includes extensive test coverage across multiple test classes:

  • Basic Types: Validates string, number, integer, boolean, and null types
  • Constraints: Tests string patterns, numeric bounds, array limits
  • Complex Types:
    • Arrays with type validation and uniqueness
    • Objects with required fields and nested structures
    • Format types (datetime, email, URI)
  • Advanced Features:
    • Circular references and recursive schemas
    • Property name normalization
    • Constant values and enums
    • Schema caching and hashing
    • Default value inheritance and merging

@jlowin jlowin changed the base branch from main to marvin-3 January 18, 2025 00:08
@jlowin jlowin changed the title Json schema Add JSONSchema parser Jan 18, 2025
@github-actions github-actions bot added enhancement New feature or request feature Pull requests that add features labels Jan 18, 2025
@jlowin jlowin merged commit 6d7966e into marvin-3 Jan 18, 2025
2 of 3 checks passed
@jlowin jlowin deleted the json-schema branch January 18, 2025 00:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature Pull requests that add features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant