Skip to main content

Schema Models

PyPI PyPI - Python Version REUSE status

Use this library to turn a JSON Schema into a plain 'ol Python dataclass.

Installation

Install this package using the usual suspects.

pip install schemamodels

Usage

This library only supports JSON schemas of type: object:

    {
        "$id": "https://schema.dev/fake-schema.schema.json",
        "$schema": "http://json-schema.org/draft-07/schema#",
        "title": "fake-schema",
        "description": "Blue Blah",
        "type": "object",
        "properties": {
            "property_a": {
              "default": 5,
              "type": "integer"
            },
            "property_b": {
              "type": "string"
            }
        }
    }
from schemamodels import SchemaModelFactory

schema_string = ".."
my_json_schema = json.loads(schema_string)

factory = SchemaModelFactory()
factory.register(my_json_schema)

Use your new dataclass

from schemamodels import exceptions
from schemamodels.dynamic import FakeSchema

your_data_instance = FakeSchema(property_a=2334)  # OK

with pytest.raises(exceptions.ValueTypeViolation):
    your_data_instance = FakeSchema(property_a="hello")

Required Fields Behavior

Design Decision Note: In standard JSON Schema specifications, object properties are optional by default unless explicitly listed in the required array (see Understanding JSON Schema: required).

In schemamodels, a missing required keyword means that all fields are required. When registering an object schema without a required keyword, all defined properties without explicit default values become required fields.

To make fields optional, either:

  • Explicitly define the required keyword with only the mandatory properties (e.g., "required": ["property_a"] or "required": [] to make all fields optional).
  • Specify an explicit "default" value on the property definition.

Why this library exists

Faster than defining dataclasses manually

The class-like syntax of creating dataclasses is a useful way to model a valid JSON schema. This library essentially makes your existing JSON schemas usable as a Python dataclass.

Useable everywhere

Taking the time to construct a plain ol' Python dataclass, ensures widespread utility across of multiple domains of study and applications.

Just-enough validation

The dataclasses are not completely dumb. Every dataclass object that originated from a valid JSON schema, will perform runtime checks on the input you provide it.

These basic checks are performed every time you create a new instance of a generated dataclass:

  • Are the field names correct?
  • Are the required fields present?
  • Does the input match the datatype expectations set forth by the generated dataclass?

JSON Schema 2020-12 Structural Validation Support

Below is the current support status for the keywords defined in Section 6 (A Vocabulary for Structural Validation) of the JSON Schema 2020-12 specification:

Keyword Section Category Supported
type 6.1.1 Any Instance Type ✅
enum 6.1.2 Any Instance Type ✅
const 6.1.3 Any Instance Type ❌
multipleOf 6.2.1 Numeric Instances ✅
maximum 6.2.2 Numeric Instances ✅
exclusiveMaximum 6.2.3 Numeric Instances ✅
minimum 6.2.4 Numeric Instances ✅
exclusiveMinimum 6.2.5 Numeric Instances ✅
maxLength 6.3.1 String Instances ✅
minLength 6.3.2 String Instances ✅
pattern 6.3.3 String Instances ❌
format 7.3 String Instances (partial)
maxItems 6.4.1 Array Instances ✅
minItems 6.4.2 Array Instances ✅
uniqueItems 6.4.3 Array Instances ✅
maxContains 6.4.4 Array Instances ✅
minContains 6.4.5 Array Instances ✅
maxProperties 6.5.1 Object Instances ❌
minProperties 6.5.2 Object Instances ❌
required 6.5.3 Object Instances ✅
dependentRequired 6.5.4 Object Instances ❌

JSON Schema 2020-12 String Format Support

Below is the current support status for the string format keywords defined in Section 7.3 (Defined Formats) of the JSON Schema 2020-12 specification:

Format Section Category Supported
date-time 7.3.1 Dates, Times, and Duration ✅
date 7.3.1 Dates, Times, and Duration ✅
time 7.3.1 Dates, Times, and Duration ✅
duration 7.3.1 Dates, Times, and Duration ✅
email 7.3.2 Email Addresses ✅
idn-email 7.3.2 Email Addresses ❌
hostname 7.3.3 Hostnames ✅
idn-hostname 7.3.3 Hostnames ❌
ipv4 7.3.4 IP Addresses ✅
ipv6 7.3.4 IP Addresses ✅
uri 7.3.5 Resource Identifiers ✅
uri-reference 7.3.5 Resource Identifiers ✅
iri 7.3.5 Resource Identifiers ✅
iri-reference 7.3.5 Resource Identifiers ✅
uuid 7.3.5 Resource Identifiers ✅
uri-template 7.3.6 URI Template ❌
json-pointer 7.3.7 JSON Pointers ❌
relative-json-pointer 7.3.7 JSON Pointers ❌
regex 7.3.8 Regular Expressions ❌

Benchmark Overview

The performance benchmark evaluates SchemaModelFactory (V1, based on functor dispatch and metaprogramming) against SchemaModelFactoryV2 (V2, based on the descriptor pattern for structural validation) across 4 schema workloads (customer_record, ecommerce_order, sensor_telemetry, and simple_user) with 10,000 iterations per workload.

Benchmark Comparison

Python 3.12.3 Execution Metrics

Phase V1 Time (s) V2 Time (s) Speedup
Registration 0.008996 0.007591 1.19x
Instantiation & Validate 0.774447 0.302700 2.56x
Total Time 0.783443 0.310291 2.52x

Python 3.15.2 Execution Metrics

Phase V1 Time (s) V2 Time (s) Speedup
Registration 0.011681 0.005333 2.19x
Instantiation & Validate 0.655611 0.225569 2.91x
Total Time 0.667292 0.230902 2.89x

License

This codebase is licensed under the GPLv3+ and therefore the use, inclusion, modification, and distribution of this software is governed by the GPL.

To inquire about the commercial license by sending an email to: license AT civichacker.com.

Metadata

Release files for schemamodels 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for schemamodels 1.1.0
File Size Uploaded Provenance Actions
schemamodels-1.1.0.tar.gz 14.8 kB Without provenance Details for schemamodels-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for schemamodels 1.1.0
File Interpreter ABI Platform Actions
schemamodels-1.1.0-py3-none-any.whl Python 3 none any Details for schemamodels-1.1.0-py3-none-any.whl

Total release size: 31.9 kB

Release files / schemamodels-1.1.0.tar.gz

Download URL schemamodels-1.1.0.tar.gz
Size 14.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2496ddb68ed0a062aecb9e783deb75fc0d70de170868b8e2166b681299e93e35
BLAKE2b-256 checksum
How to use checksums
8ee6d1d465df786996051117a648be2807d76cc7d83a058c20cdc0259c2d7e00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / schemamodels-1.1.0-py3-none-any.whl

Download URL schemamodels-1.1.0-py3-none-any.whl
Size 17.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6281f13affc65bc5d3ad81cc55aa36b27ce050945543c0164edaeaa73d78cb1c
BLAKE2b-256 checksum
How to use checksums
735ea75f5321ea3ff996665bcb777ff15ed8ecbeef5205bf79be3ca6e77880cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for schemamodels 1.1.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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page