jsonschema
Convert JSON Schema to Python types with validation.
marvin.utilities.jsonschema
Convert JSON Schema to Python types with validation.
The jsonschema_to_type function converts a JSON Schema into a Python type that can be used for validation with Pydantic. It supports:
- Basic types (string, number, integer, boolean, null)
- Complex types (arrays, objects)
- Format constraints (date-time, email, uri)
- Numeric constraints (minimum, maximum, multipleOf)
- String constraints (minLength, maxLength, pattern)
- Array constraints (minItems, maxItems, uniqueItems)
- Object properties with defaults
- References and recursive schemas
- Enums and constants
- Union types
Example:
Constants
FORMAT_TYPES
Classes
JSONSchema
Functions
create_array_type
Create list/set type with optional constraints.
create_dataclass
Create dataclass from object schema.
create_enum
Create enum type from list of values.
create_field_with_default
Create a field with simplified default handling.
create_numeric_type
Create numeric type with optional constraints.
create_string_type
Create string type with optional constraints.
get_default_value
Get default value with proper priority ordering.
- Value from parent’s default if it exists
- Property’s own default if it exists
- None
hash_schema
Generate a deterministic hash for schema caching.
jsonschema_to_type
Convert JSON schema to appropriate Python type with validation.
Args: schema: A JSON Schema dictionary defining the type structure and validation rules name: Optional name for object schemas. Only allowed when schema type is “object”. If not provided for objects, name will be inferred from schema’s “title” property or default to “Root”.
Returns: A Python type (typically a dataclass for objects) with Pydantic validation
Raises: ValueError: If a name is provided for a non-object schema
Examples: Create a dataclass from an object schema:
Person(name=“John”, age=30)
Create a scalar type with constraints:
merge_defaults
Merge defaults with provided data at all levels.
resolve_ref
Resolve JSON Schema reference to target schema.
sanitize_name
Convert string to valid Python identifier.
schema_to_type
Convert schema to appropriate Python type.
Parent Module: utilities