json-schema-to-pydantic


Namejson-schema-to-pydantic JSON
Version 0.2.1 PyPI version JSON
download
home_pageNone
SummaryA Python library for automatically generating Pydantic v2 models from JSON Schema definitions
upload_time2025-01-08 11:55:11
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseNone
keywords conversion json-schema pydantic schema validation
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # JSON Schema to Pydantic

A Python library for automatically generating Pydantic v2 models from JSON Schema definitions.

![PyPI - Version](https://img.shields.io/pypi/v/json-schema-to-pydantic)
[![codecov](https://codecov.io/github/richard-gyiko/json-schema-to-pydantic/graph/badge.svg?token=YA2Y769H1K)](https://codecov.io/github/richard-gyiko/json-schema-to-pydantic)

## Features

- Converts JSON Schema to Pydantic v2 models
- Supports complex schema features including:
  - References ($ref) with circular reference detection
  - Combiners (allOf, anyOf, oneOf) with proper type discrimination
  - Type constraints and validations
  - Array and object validations
  - Format validations (email, uri, uuid, date-time)
- Full type hinting support
- Clean, simple API

## Installation

```bash
pip install json-schema-to-pydantic
```

## Development Setup

1. Clone the repository
2. Install development dependencies:
```bash
# Using uv (recommended)
uv pip install -e ".[dev]"

# Or using pip
pip install -e ".[dev]"
```

3. Run tests:
```bash
pytest
```

## Quick Start

```python
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
)
```

## Advanced Usage

For more complex scenarios, you can use the `PydanticModelBuilder` directly:

```python
from json_schema_to_pydantic import PydanticModelBuilder

builder = PydanticModelBuilder()
model = builder.create_pydantic_model(schema, root_schema)
```

## Error Handling

The library provides specific exceptions for different error cases:

```python
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}")
```

## Documentation

See [docs/features.md](docs/features.md) for detailed documentation of supported JSON Schema features.

## Contributing

1. Fork the repository
2. Create a new branch for your feature
3. Make your changes
4. Run tests and ensure they pass
5. Submit a pull request

## License

This project is licensed under the terms of the license included in the repository.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "json-schema-to-pydantic",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "conversion, json-schema, pydantic, schema, validation",
    "author": null,
    "author_email": "Richard Gyiko <gyiko.richard@outlook.com>",
    "download_url": "https://files.pythonhosted.org/packages/9e/e6/b5f4e17c62d4042ab71ded80b0298609582cff563995de2fcc75469767ab/json_schema_to_pydantic-0.2.1.tar.gz",
    "platform": null,
    "description": "# JSON Schema to Pydantic\n\nA Python library for automatically generating Pydantic v2 models from JSON Schema definitions.\n\n![PyPI - Version](https://img.shields.io/pypi/v/json-schema-to-pydantic)\n[![codecov](https://codecov.io/github/richard-gyiko/json-schema-to-pydantic/graph/badge.svg?token=YA2Y769H1K)](https://codecov.io/github/richard-gyiko/json-schema-to-pydantic)\n\n## Features\n\n- Converts JSON Schema to Pydantic v2 models\n- Supports complex schema features including:\n  - References ($ref) with circular reference detection\n  - Combiners (allOf, anyOf, oneOf) with proper type discrimination\n  - Type constraints and validations\n  - Array and object validations\n  - Format validations (email, uri, uuid, date-time)\n- Full type hinting support\n- Clean, simple API\n\n## Installation\n\n```bash\npip install json-schema-to-pydantic\n```\n\n## Development Setup\n\n1. Clone the repository\n2. Install development dependencies:\n```bash\n# Using uv (recommended)\nuv pip install -e \".[dev]\"\n\n# Or using pip\npip install -e \".[dev]\"\n```\n\n3. Run tests:\n```bash\npytest\n```\n\n## Quick Start\n\n```python\nfrom json_schema_to_pydantic import create_model\n\n# Define your JSON Schema\nschema = {\n    \"title\": \"User\",\n    \"type\": \"object\",\n    \"properties\": {\n        \"name\": {\"type\": \"string\"},\n        \"email\": {\"type\": \"string\", \"format\": \"email\"},\n        \"age\": {\"type\": \"integer\", \"minimum\": 0}\n    },\n    \"required\": [\"name\", \"email\"]\n}\n\n# Generate your Pydantic model\nUserModel = create_model(schema)\n\n# Use the model\nuser = UserModel(\n    name=\"John Doe\",\n    email=\"john@example.com\",\n    age=30\n)\n```\n\n## Advanced Usage\n\nFor more complex scenarios, you can use the `PydanticModelBuilder` directly:\n\n```python\nfrom json_schema_to_pydantic import PydanticModelBuilder\n\nbuilder = PydanticModelBuilder()\nmodel = builder.create_pydantic_model(schema, root_schema)\n```\n\n## Error Handling\n\nThe library provides specific exceptions for different error cases:\n\n```python\nfrom json_schema_to_pydantic import (\n    SchemaError,     # Base class for all schema errors\n    TypeError,       # Invalid or unsupported type\n    CombinerError,   # Error in schema combiners\n    ReferenceError,  # Error in schema references\n)\n\ntry:\n    model = create_model(schema)\nexcept TypeError as e:\n    print(f\"Invalid type in schema: {e}\")\nexcept ReferenceError as e:\n    print(f\"Invalid reference: {e}\")\n```\n\n## Documentation\n\nSee [docs/features.md](docs/features.md) for detailed documentation of supported JSON Schema features.\n\n## Contributing\n\n1. Fork the repository\n2. Create a new branch for your feature\n3. Make your changes\n4. Run tests and ensure they pass\n5. Submit a pull request\n\n## License\n\nThis project is licensed under the terms of the license included in the repository.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A Python library for automatically generating Pydantic v2 models from JSON Schema definitions",
    "version": "0.2.1",
    "project_urls": {
        "Bug-Tracker": "https://github.com/richard-gyiko/json-schema-to-pydantic/issues",
        "Documentation": "https://github.com/richard-gyiko/json-schema-to-pydantic#readme",
        "Homepage": "https://github.com/richard-gyiko/json-schema-to-pydantic"
    },
    "split_keywords": [
        "conversion",
        " json-schema",
        " pydantic",
        " schema",
        " validation"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b3edcbbd4b74164b0f7b9be1edac29df0fb5437df4157053a41501b71645a404",
                "md5": "e2ead7234572c459271bf5247b3d7d8c",
                "sha256": "6c01e98f136102e417192882f425c1f22980cac78c506a600d4b9d022a0d9f94"
            },
            "downloads": -1,
            "filename": "json_schema_to_pydantic-0.2.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e2ead7234572c459271bf5247b3d7d8c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 11322,
            "upload_time": "2025-01-08T11:55:10",
            "upload_time_iso_8601": "2025-01-08T11:55:10.007516Z",
            "url": "https://files.pythonhosted.org/packages/b3/ed/cbbd4b74164b0f7b9be1edac29df0fb5437df4157053a41501b71645a404/json_schema_to_pydantic-0.2.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9ee6b5f4e17c62d4042ab71ded80b0298609582cff563995de2fcc75469767ab",
                "md5": "ed0578efde67ab7c9c7b4052e1d5728c",
                "sha256": "7322760b40fe6988760695f0b5081c359e8edb4f0c3a8fc2dc5de845a23cee67"
            },
            "downloads": -1,
            "filename": "json_schema_to_pydantic-0.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "ed0578efde67ab7c9c7b4052e1d5728c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 34579,
            "upload_time": "2025-01-08T11:55:11",
            "upload_time_iso_8601": "2025-01-08T11:55:11.308487Z",
            "url": "https://files.pythonhosted.org/packages/9e/e6/b5f4e17c62d4042ab71ded80b0298609582cff563995de2fcc75469767ab/json_schema_to_pydantic-0.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-08 11:55:11",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "richard-gyiko",
    "github_project": "json-schema-to-pydantic",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "json-schema-to-pydantic"
}
        
Elapsed time: 0.44253s