marvin_impl
Class info¶
Classes¶
| Name | Children | Inherits |
|---|---|---|
| JSONSchema schemez.schema_to_type.marvin_impl |
🛈 DocStrings¶
Convert JSON schema to appropriate Python type with validation.
Credits to Marvin / prefect for original code.
create_array_type
¶
create_array_type(
schema: Mapping[str, Any], schemas: Mapping[str, Any]
) -> type | Annotated[Any, ...]
Create list/set type with optional constraints.
Source code in src/schemez/schema_to_type/marvin_impl.py
create_dataclass
¶
create_dataclass(
schema: Mapping[str, Any], name: str | None = None, schemas: Mapping[str, Any] | None = None
) -> type
Create dataclass from object schema.
Source code in src/schemez/schema_to_type/marvin_impl.py
create_enum
¶
Create enum type from list of values.
Source code in src/schemez/schema_to_type/marvin_impl.py
create_field_with_default
¶
Create a field with simplified default handling.
Source code in src/schemez/schema_to_type/marvin_impl.py
create_numeric_type
¶
create_numeric_type(
base: type[int | float], schema: Mapping[str, Any]
) -> type | Annotated[Any, ...]
Create numeric type with optional constraints.
Source code in src/schemez/schema_to_type/marvin_impl.py
create_string_type
¶
Create string type with optional constraints.
Source code in src/schemez/schema_to_type/marvin_impl.py
get_default_value
¶
get_default_value(
schema: dict[str, Any], prop_name: str, parent_default: dict[str, Any] | None = None
) -> Any
Get default value with proper priority ordering.
- Value from parent's default if it exists
- Property's own default if it exists
- None.
Source code in src/schemez/schema_to_type/marvin_impl.py
hash_schema
¶
Generate a deterministic hash for schema caching.
jsonschema_to_type
¶
Convert JSON schema to appropriate Python type with validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Mapping[str, Any]
|
A JSON Schema dictionary defining the type structure and validation rules |
required |
name
|
str | None
|
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". |
None
|
Returns:
| Type | Description |
|---|---|
type
|
A Python type (typically a dataclass for objects) with Pydantic validation |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a name is provided for a non-object schema |
Examples:
Create a dataclass from an object schema:
schema = {
"type": "object",
"title": "Person",
"properties": {
"name": {"type": "string", "minLength": 1},
"age": {"type": "integer", "minimum": 0},
"email": {"type": "string", "format": "email"}
},
"required": ["name", "age"]
}
Person = jsonschema_to_type(schema)
# Creates a dataclass with name, age, and optional email fields:
# @dataclass
# class Person:
# name: str
# age: int
# email: str | None = None
Create a scalar type with constraints:
schema = {
"type": "string",
"minLength": 3,
"pattern": "^[A-Z][a-z]+$"
}
NameType = jsonschema_to_type(schema)
# Creates Annotated[str, StringConstraints(min_length=3, pattern="^[A-Z][a-z]+$")]
@dataclass
class Name:
name: NameType
Source code in src/schemez/schema_to_type/marvin_impl.py
merge_defaults
¶
merge_defaults(
data: Mapping[str, Any],
schema: Mapping[str, Any],
parent_default: Mapping[str, Any] | None = None,
) -> dict[str, Any]
Merge defaults with provided data at all levels.
Source code in src/schemez/schema_to_type/marvin_impl.py
resolve_ref
¶
Resolve JSON Schema reference to target schema.
Source code in src/schemez/schema_to_type/marvin_impl.py
sanitize_name
¶
Convert string to valid Python identifier.
Source code in src/schemez/schema_to_type/marvin_impl.py
schema_to_type
¶
Convert schema to appropriate Python type.