Skip to main content

Pydantic models for representing JSON Schema objects

Project description

jsonschema-models

Pydantic models for representing JSON Schema objects.

Features

  • Data model for JSON Schema Draft 2020-12 specification
  • Python 3.12+ type annotations
  • Pydantic-based models for validation and serialization
  • Specialized schema classes for different types (string, number, array, object, etc.)
  • Support for references and nested schemas
  • Type fields can use Python types (str, int, float, bool, list, dict) or SchemaType enums
  • Sypport for specifying format validation:
    • Email formats (email, idn-email)
    • Hostname formats (hostname, idn-hostname)
    • IP address formats (ipv4, ipv6)
    • Resource identifier formats (uri, uri-reference, iri, iri-reference, uuid)
    • Date and time formats (date-time, date, time, duration)
    • URI templates (uri-template)
    • JSON pointers (json-pointer, relative-json-pointer)
    • Regular expressions (regex)

Note: Currently this library does not enforce the many validation expectations that the 2020-12 specification calls for, it only allows you to implement the shape of the data.

Installation

pip install jsonschema-models

Usage

Basic Usage

import jsonschema_models as jsm

# Create a simple schema
schema = jsm.Schema(
    title='Person',
    type=jsm.SchemaType.OBJECT,  # Or use Python type: type=dict
    properties={
        'name': jsm.Schema(type=jsm.SchemaType.STRING),  # Or use Python type: type=str
        'age': jsm.Schema(type=jsm.SchemaType.INTEGER, minimum=0)  # Or use Python type: type=int
    },
    required=['name']
)

# Convert to JSON Schema
json_schema = schema.model_dump(by_alias=True)

Using Specialized Schema Classes

import jsonschema_models as jsm

# Create a schema using specialized classes
schema = jsm.ObjectSchema(
    title='Person',
    properties={
        'name': jsm.StringSchema(min_length=1),
        'age': jsm.IntegerSchema(minimum=0),
        'hobbies': jsm.ArraySchema(items=jsm.StringSchema()),
        'email': jsm.EmailSchema(description="Contact email"),
        'website': jsm.URISchema(pattern="^https://")
    },
    required=['name', 'email']
)

Working with Format Validation

import jsonschema_models as jsm

# Using format validation with specialized schemas
user_schema = jsm.ObjectSchema(
    title='User',
    properties={
        'id': jsm.UuidSchema(),
        'email': jsm.EmailSchema(min_length=5),
        'website': jsm.URISchema(),
        'registration_date': jsm.DateTimeSchema(),
        'server_hostname': jsm.HostnameSchema(),
        'server_ip': jsm.IPv4Schema()
    },
    required=['id', 'email']
)

# Using format validation with Schema class
contact_schema = jsm.Schema(
    type=jsm.SchemaType.OBJECT,
    properties={
        'email_alternative': jsm.Schema(
            type=jsm.SchemaType.STRING,
            format=jsm.FormatType.EMAIL
        ),
        'phone': jsm.Schema(
            type=jsm.SchemaType.STRING,
            pattern=r'^\+[0-9]{1,3}-[0-9]{3,14}$'
        )
    }
)

# Convert to JSON Schema
json_schema = user_schema.model_dump(by_alias=True)

Working with References

import jsonschema_models as jsm

# Create a schema with references
schema = jsm.Schema(
    title='Root',
    type=dict,
    properties={
        'user': jsm.Schema(ref='#/$defs/user')
    },
    defs={
        'user': jsm.Schema(
            type=dict,
            properties={
                'name': jsm.Schema(type=str),
                'email': jsm.EmailSchema(),
                'created_at': jsm.DateTimeSchema()
            },
            required=['name', 'email']
        )
    }
)

License

BSD License - See LICENSE file for details

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonschema_models-1.1.2.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jsonschema_models-1.1.2-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file jsonschema_models-1.1.2.tar.gz.

File metadata

  • Download URL: jsonschema_models-1.1.2.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for jsonschema_models-1.1.2.tar.gz
Algorithm Hash digest
SHA256 88b99aaa5ea631804497f0f6bb9b5765aac7eadef324d31e6d060daf061b1ac7
MD5 36fe2d6eb6c41127c70624cd44918a6d
BLAKE2b-256 40cfccd6e1a777b7cbe8567f3bf95e278670e85cf04d7964cca60ab80c1df968

See more details on using hashes here.

File details

Details for the file jsonschema_models-1.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for jsonschema_models-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 eaa0bc9bdd2fc14d60d0fcac5b20640cf905cb855fd17fad22a6b864550ca1ba
MD5 1a575b0c1ae67eea4b576e963e7af164
BLAKE2b-256 592a89a5d5129fb00b0f64d4c955a59d6125a9bfd3bd1aeadbfb7ef7a919929b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page