Fast JSON Schema validation for Python implemented in Rust
Project description
Fast JSON Schema validation for Python implemented in Rust.
Supported drafts:
Draft 7
Draft 6
Draft 4
There are some notable restrictions at the moment:
The underlying crate doesn’t support arbitrary precision integers yet, which may lead to SystemError when such value is used;
Unicode surrogates are not supported;
Installation
To install jsonschema-rs via pip run the following command:
pip install jsonschema-rs
Usage
To check if the input document is valid:
import jsonschema_rs
validator = jsonschema_rs.JSONSchema({"minimum": 42})
validator.is_valid(45) # True
or:
import jsonschema_rs
validator = jsonschema_rs.JSONSchema({"minimum": 42})
validator.validate(41) # raises ValidationError
If you have a schema as a JSON string, then you could use jsonschema_rs.JSONSchema.from_str to avoid parsing on the Python side:
import jsonschema_rs
validator = jsonschema_rs.JSONSchema.from_str('{"minimum": 42}')
...
Performance
According to our benchmarks, jsonschema-rs is usually faster than existing alternatives in real-life scenarios.
However, for small schemas & inputs it might be slower than fastjsonschema or jsonschema on PyPy.
Input values and schemas
Zuora OpenAPI schema (zuora.json). Validated against OpenAPI 3.0 JSON Schema (openapi.json).
Kubernetes Swagger schema (kubernetes.json). Validated against Swagger JSON Schema (swagger.json).
Canadian border in GeoJSON format (canada.json). Schema is taken from the GeoJSON website (geojson.json).
Concert data catalog (citm_catalog.json). Schema is inferred via infers-jsonschema & manually adjusted (citm_catalog_schema.json).
Fast is taken from fastjsonschema benchmarks (fast_schema.json, f`ast_valid.json` and fast_invalid.json).
Case |
Schema size |
Instance size |
---|---|---|
OpenAPI |
18 KB |
4.5 MB |
Swagger |
25 KB |
3.0 MB |
Canada |
4.8 KB |
2.1 MB |
CITM catalog |
2.3 KB |
501 KB |
Fast (valid) |
595 B |
55 B |
Fast (invalid) |
595 B |
60 B |
Compiled validators (when the input schema is compiled once and reused later). jsonschema-rs comes in three variants in the tables below:
validate. This method raises ValidationError on errors or returns None on their absence.
is_valid. A faster method that returns a boolean result whether the instance is valid.
overhead. Only transforms data to underlying Rust types and do not perform any validation. Shows the Python -> Rust data conversion cost.
Ratios are given against the validate variant.
Small schemas:
library |
true |
{"minimum": 10} |
Fast (valid) |
Fast (invalid) |
---|---|---|---|---|
jsonschema-rs[validate] |
93.84 ns |
94.83 ns |
1.2 us |
1.84 us |
jsonschema-rs[is_valid] |
70.22 ns (x0.74) |
68.26 ns (x0.71) |
688.70 ns (x0.57) |
1.26 us (x0.68) |
jsonschema-rs[overhead] |
65.27 ns (x0.69) |
66.90 ns (x0.70) |
461.53 ns (x0.38) |
925.16 ns (x0.50) |
fastjsonschema[CPython] |
58.19 ns (x0.62) |
105.77 ns (x1.11) |
3.98 us (x3.31) |
4.57 us (x2.48) |
fastjsonschema[PyPy] |
10.39 ns (x0.11) |
34.96 ns (x0.36) |
866 ns (x0.72) |
916 ns (x0.49) |
jsonschema[CPython] |
235.06 ns (x2.50) |
1.86 us (x19.6) |
56.26 us (x46.88) |
59.39 us (x32.27) |
jsonschema[PyPy] |
40.83 ns (x0.43) |
232.41 ns (x2.45) |
21.82 us (x18.18) |
22.23 us (x12.08) |
Large schemas:
library |
Zuora (OpenAPI) |
Kubernetes (Swagger) |
Canada (GeoJSON) |
CITM catalog |
---|---|---|---|---|
jsonschema-rs[validate] |
17.311 ms |
15.194 ms |
5.018 ms |
4.765 ms |
jsonschema-rs[is_valid] |
16.605 ms (x0.95) |
12.610 ms (x0.82) |
4.954 ms (x0.98) |
2.792 ms (x0.58) |
jsonschema-rs[overhead] |
12.017 ms (x0.69) |
8.005 ms (x0.52) |
3.702 ms (x0.73) |
2.303 ms (x0.48) |
fastjsonschema[CPython] |
– (1) |
90.305 ms (x5.94) |
32.389 ms (6.45) |
12.020 ms (x2.52) |
fastjsonschema[PyPy] |
– (1) |
37.204 ms (x2.44) |
8.450 ms (x1.68) |
4.888 ms (x1.02) |
jsonschema[CPython] |
764.172 ms (x44.14) |
1.063 s (x69.96) |
1.301 s (x259.26) |
115.362 ms (x24.21) |
jsonschema[PyPy] |
604.557 ms (x34.92) |
619.744 ms (x40.78) |
524.275 ms (x104.47) |
25.275 ms (x5.30) |
Notes:
fastjsonschema fails to compile the Open API spec due to the presence of the uri-reference format (that is not defined in Draft 4). However, unknown formats are explicitly supported by the spec.
The bigger the input is the bigger is performance win. You can take a look at benchmarks in benches/bench.py.
Package versions:
jsonschema-rs - latest version from the repository
jsonschema - 3.2.0
fastjsonschema - 2.15.1
Measured with stable Rust 1.56, CPython 3.9.7 / PyPy3 7.3.6 on i8700K (12 cores), 32GB RAM, Arch Linux.
Python support
jsonschema-rs supports CPython 3.7, 3.8, 3.9, 3.10, and 3.11.
License
The code in this project is licensed under MIT license. By contributing to jsonschema-rs, you agree that your contributions will be licensed under its MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for jsonschema_rs-0.16.2-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb5ead2669ed4ea528648304e80b14a137a4b9f572a9b5464237d9449b5c0773 |
|
MD5 | bc34be0396b0f44c6757f6bf246d4ec6 |
|
BLAKE2b-256 | 7922141bb74beb1e0e25fb6a9292316ba6a0f5fa90f06760bf279609b331fde6 |
Hashes for jsonschema_rs-0.16.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52452f24d851437b6dc7bff2046485735ade0966851f4e83830c8adcc5d831e9 |
|
MD5 | 388ff2eced347fc91b7111ef2b125a06 |
|
BLAKE2b-256 | 6c2d397b401d663ef5bb6b928399726f6d95594703af2073739c6ff100e9f4fb |
Hashes for jsonschema_rs-0.16.2-cp311-cp311-macosx_11_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6c3a424379865b5730da28679b28dce8743b68f27f0e612216a0bc4d9793b2b |
|
MD5 | f6f56542649496a43d8c05c203c503ca |
|
BLAKE2b-256 | c06a7fb441a3258d7601081170495a7463a1d26661382798388ddbb87b44a0b9 |
Hashes for jsonschema_rs-0.16.2-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ddc62fcbe726a804abc287c7e746d3e14debb4f6ffa35f174a57fab3a728f3f |
|
MD5 | 77c293bd665c6f5429f552063dbbd770 |
|
BLAKE2b-256 | 22ccf18c1af83f34764211bf2670718d2619aec0b3359849080aa0546f4e2483 |
Hashes for jsonschema_rs-0.16.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ade40d291d8cc124ace49a8757e684bd75dc15bc7304e21e4ae8a4b1a8f951d |
|
MD5 | 15fa60b396db13ac958dba871708dd6f |
|
BLAKE2b-256 | 93cf9b5aa41677a7fa13d075fa195698070e6fa10a8d9db83a3cf1455cc67a81 |
Hashes for jsonschema_rs-0.16.2-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc8d27add8ec9165e9d4f93818dc299f1929f46a806e0a6dead09f10e2f8948a |
|
MD5 | 4d771121192ff5dbac2bfdbb5a4dc000 |
|
BLAKE2b-256 | cdda7ebb9c83c1cef0ac1ed5b03533883e9a34076e647f623ce65b87b0c65aba |
Hashes for jsonschema_rs-0.16.2-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dff2dfeca617650d248377c0669908994b292e46ae7d20fe275bb9cca9d74f8e |
|
MD5 | b65de96810e2cd6bd68438788b49bb86 |
|
BLAKE2b-256 | 9256556f44cc4552d56db240fe0e94a8fbecbe1aa560d20111170629cad619c7 |
Hashes for jsonschema_rs-0.16.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1e2d6ee84181aa483eaaf054c4373de14af36a88871ea44f9a70e88479b462a |
|
MD5 | 03a79b333163c8375d09576383afb6fe |
|
BLAKE2b-256 | f7013dd65e366e5064c466471574b78e9b5e18e7d4afd0a57b3c497c156ba16a |
Hashes for jsonschema_rs-0.16.2-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a19650cd791999118cf58f84abffdd573730ede69cb03bbde10639186526a64f |
|
MD5 | 72cb0ebf2ee4a4e18df4dad21b4a5085 |
|
BLAKE2b-256 | af118f3d5dd60990d5e1e258673d58db9fa4fb9e29558d7b6cb731ed491dd159 |
Hashes for jsonschema_rs-0.16.2-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf76314a287c5fc2fb7b48969d54d204093c089764b2d1dca1ad9a92ea1d134f |
|
MD5 | 8004c099fd7f5712b89ff70631652bdd |
|
BLAKE2b-256 | 542ed792b03f02fd62fcff5b20920d8f6fb72ab4ac2d366049044fa09e0756d2 |
Hashes for jsonschema_rs-0.16.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e88faa5e3b62870a3d2acbe237e871e5e3253e9d1a9ef47fb47b2a003d521e58 |
|
MD5 | 09c3d461aaa438e9520833af2f882f4a |
|
BLAKE2b-256 | b30422856552c4418cb99000284195842050ffab264313d6ec96a7d4de0a5bec |
Hashes for jsonschema_rs-0.16.2-cp38-cp38-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b06ec6f69c06a4ae28faec8a0c7d11ce9cd77243665a5f4ccb399c3d3e1ba9c |
|
MD5 | e6fd017675026a4ee06b542556cd795f |
|
BLAKE2b-256 | 698b3ea4903dacae680fb8f58d5095960a7f5fcfc357abe03a4b6f7fd4586e0e |
Hashes for jsonschema_rs-0.16.2-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | be91e69ee35ebbe78eff3370c43b2f8beba53e27ba916b7bac75e5fbf362e4b7 |
|
MD5 | e0999b35dc5860299cfbfb4dcfff653d |
|
BLAKE2b-256 | 7c9c66924958cf90d88c229b337d4f0124eb1801b937a701172ae35ae81d3998 |
Hashes for jsonschema_rs-0.16.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 126613b14f319487131f657f0e87620d9e302e31b3f69c562d9cc1d46bb7a340 |
|
MD5 | 1b08e367ab1ac2c73ee6bf04d78405c7 |
|
BLAKE2b-256 | 91a8d8a76125b59607d08fb52b4e2ab08c624bf5906bba8c224e49385eede629 |
Hashes for jsonschema_rs-0.16.2-cp37-cp37m-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e9abc865eee8f1c268cc9c151f747ee9fe6489c79ded80799b7ffcd2ec7fc923 |
|
MD5 | 9c9fc9b9b685df6632ab68ec1331e322 |
|
BLAKE2b-256 | 80124f4fe18ee26df08e82f06e42ff713e08a5de926ab782a5e9a4427705720d |