
Research
Security News
The Growing Risk of Malicious Browser Extensions
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
json-schema-to-pydantic
Advanced tools
A Python library for automatically generating Pydantic v2 models from JSON Schema definitions
A Python library for automatically generating Pydantic v2 models from JSON Schema definitions.
pip install json-schema-to-pydantic
# Using uv (recommended)
uv pip install -e ".[dev]"
# Or using pip
pip install -e ".[dev]"
pytest
from json_schema_to_pydantic import create_model
# Define your JSON Schema
schema = {
"title": "User",
"type": "object",
"properties": {
"name": {"type": "string"},
"email": {"type": "string", "format": "email"},
"age": {"type": "integer", "minimum": 0}
},
"required": ["name", "email"]
}
# Generate your Pydantic model
UserModel = create_model(schema)
# Use the model
user = UserModel(
name="John Doe",
email="john@example.com",
age=30
)
# Example with relaxed array item validation
RelaxedModel = create_model(
{"type": "object", "properties": {"tags": {"type": "array"}}},
allow_undefined_array_items=True
)
relaxed_instance = RelaxedModel(tags=[1, "two", True])
For more complex scenarios, you can use the PydanticModelBuilder
directly:
from json_schema_to_pydantic import PydanticModelBuilder
builder = PydanticModelBuilder()
model = builder.create_pydantic_model(schema, root_schema)
The library provides specific exceptions for different error cases:
from json_schema_to_pydantic import (
SchemaError, # Base class for all schema errors
TypeError, # Invalid or unsupported type
CombinerError, # Error in schema combiners
ReferenceError, # Error in schema references
)
try:
model = create_model(schema)
except TypeError as e:
print(f"Invalid type in schema: {e}")
except ReferenceError as e:
print(f"Invalid reference: {e}")
See docs/features.md for detailed documentation of supported JSON Schema features.
This project is licensed under the terms of the license included in the repository.
FAQs
A Python library for automatically generating Pydantic v2 models from JSON Schema definitions
We found that json-schema-to-pydantic demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Research
Security News
An in-depth analysis of credential stealers, crypto drainers, cryptojackers, and clipboard hijackers abusing open source package registries to compromise Web3 development environments.
Security News
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.