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()
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()
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
Metadata
Release files for jsonschema-models 1.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| jsonschema_models-1.3.0.tar.gz | 15.8 kB | Without provenance | Details for jsonschema_models-1.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| jsonschema_models-1.3.0-py3-none-any.whl | Python 3 | none | any | Details for jsonschema_models-1.3.0-py3-none-any.whl |
Total release size: 23.1 kB
Release files / jsonschema_models-1.3.0.tar.gz
| Download URL | jsonschema_models-1.3.0.tar.gz |
|---|---|
| Size | 15.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
645eb517cefe7a759e5dff17162d580d1cbed89a4d6b12a7fd75bc8ad00c2020
|
|
BLAKE2b-256 checksum How to use checksums |
5ff816968fdefe9ce829f1271f4c8894cbabe4a850ac5f9eb468826eb6a91375
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.13
|
Release files / jsonschema_models-1.3.0-py3-none-any.whl
| Download URL | jsonschema_models-1.3.0-py3-none-any.whl |
|---|---|
| Size | 7.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fd3e417bb517d5b91fdc520bb6bebb12bf96ad3fe815ea21034e71ab065c8a3c
|
|
BLAKE2b-256 checksum How to use checksums |
e4d588f85168ba01cb4856f9065ebbf9f857edadb9b26018a9ee6c90a07061ca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.13
|
Security for jsonschema-models 1.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.