Skip to main content

Fast JSON Schema validation for Python implemented in Rust

Project description

Build Version Python versions License

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

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:

  1. 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, and 3.10.

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

jsonschema_rs-0.16.1.tar.gz (90.6 kB view details)

Uploaded Source

Built Distributions

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

jsonschema_rs-0.16.1-cp310-cp310-win_amd64.whl (1.7 MB view details)

Uploaded CPython 3.10Windows x86-64

jsonschema_rs-0.16.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

jsonschema_rs-0.16.1-cp310-cp310-macosx_11_0_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.10macOS 11.0+ x86-64

jsonschema_rs-0.16.1-cp39-cp39-win_amd64.whl (1.7 MB view details)

Uploaded CPython 3.9Windows x86-64

jsonschema_rs-0.16.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

jsonschema_rs-0.16.1-cp39-cp39-macosx_11_0_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.9macOS 11.0+ x86-64

jsonschema_rs-0.16.1-cp38-cp38-win_amd64.whl (1.7 MB view details)

Uploaded CPython 3.8Windows x86-64

jsonschema_rs-0.16.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

jsonschema_rs-0.16.1-cp38-cp38-macosx_11_0_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.8macOS 11.0+ x86-64

jsonschema_rs-0.16.1-cp37-cp37m-win_amd64.whl (1.7 MB view details)

Uploaded CPython 3.7mWindows x86-64

jsonschema_rs-0.16.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ x86-64

jsonschema_rs-0.16.1-cp37-cp37m-macosx_11_0_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.7mmacOS 11.0+ x86-64

File details

Details for the file jsonschema_rs-0.16.1.tar.gz.

File metadata

  • Download URL: jsonschema_rs-0.16.1.tar.gz
  • Upload date:
  • Size: 90.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.15

File hashes

Hashes for jsonschema_rs-0.16.1.tar.gz
Algorithm Hash digest
SHA256 9cadcde57f2b38fda2dd7dbe9e2484025df55367d935ad094a1b5ab09965ed51
MD5 4ffe66ad307c6478d22212d03313ff48
BLAKE2b-256 9b31e379e3406a133051697649ee23eef7aeb2aedd8495a39254ff2b568a31ee

See more details on using hashes here.

File details

Details for the file jsonschema_rs-0.16.1-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for jsonschema_rs-0.16.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 580208a6f47f00e139a4b4ee6238f6c87867e5db4ac2802314dd5a7ddf9fa357
MD5 5187112543d6efc8c804b0b67f156ffe
BLAKE2b-256 eecd214711c14c6a69719a278e6a3b08be7953933f0a45485fda694a8d75fc5d

See more details on using hashes here.

File details

Details for the file jsonschema_rs-0.16.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for jsonschema_rs-0.16.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 98cb6ae9d6747ba0f801edf82c0dfe319bcf95dabe88628672a3a806aa116950
MD5 e3d1f6cc493d4c417bba8e1bb150f174
BLAKE2b-256 fe9ea993b27c404af892d4785f4b21918201ed7acc63d7b4c96046d238e20fd2

See more details on using hashes here.

File details

Details for the file jsonschema_rs-0.16.1-cp310-cp310-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for jsonschema_rs-0.16.1-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 c63409eecee3b2e002f9b5620ac1efcff6058d14c4cff4e6f4277b11a796dc0a
MD5 7bf80fc9aa4e1737136ea7a606a0120a
BLAKE2b-256 31b1c5b06f226f71bf3b618e3808d2b2a3841a068b782356e8274d71467d4a8c

See more details on using hashes here.

File details

Details for the file jsonschema_rs-0.16.1-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for jsonschema_rs-0.16.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 b433d107b5be100c784237e4f757cf1c5f88296dd6929f7bec8b0ddabcec708d
MD5 0938e72849cec8e6fc6bd9a9c58bd0d7
BLAKE2b-256 1b6f36d1aab25fe7144bb804b0dee065752fbd54ea33dd7de3dd84e64c75d499

See more details on using hashes here.

File details

Details for the file jsonschema_rs-0.16.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for jsonschema_rs-0.16.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 54e022725ac45e10842d1dd27f2b10e3f9bcff44bad8624c765af625405bdb3e
MD5 50602d950cdd5474f212c7c3647a3edd
BLAKE2b-256 01b37e388e361253b4576ec6e20c2f8dac2d0ed11f151aa6a5d31c08187aa111

See more details on using hashes here.

File details

Details for the file jsonschema_rs-0.16.1-cp39-cp39-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for jsonschema_rs-0.16.1-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 d1fe88d386bd9cfffa6514fc5fae5389bb4b079437dceac5f668e50613e67035
MD5 e259215de014b5a2a6575f1be09587b5
BLAKE2b-256 6a39dd6064b372aa56526d929afc256a71b494d0768d394bdfa625511e892b54

See more details on using hashes here.

File details

Details for the file jsonschema_rs-0.16.1-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for jsonschema_rs-0.16.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 24b45d3116d602733e684d030f40b25058ffabdf52d895f6b12c93c3e26a7cc1
MD5 274e62e927b54e77a49a9057fa668c27
BLAKE2b-256 cafcbed4c5e8856c8221fb56fc86377060c97f90a4f48bc7bc34cd3137f48454

See more details on using hashes here.

File details

Details for the file jsonschema_rs-0.16.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for jsonschema_rs-0.16.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8cb6b027b4fa14e5b29d56e6fd8935aac7667d06f72428fb7bee9b743fd7f352
MD5 f09a4236257a240aafff0d98be459692
BLAKE2b-256 24beb2256381111740d40c83e372cb739e5b6fb2cb0857dd5622cdd6af526a16

See more details on using hashes here.

File details

Details for the file jsonschema_rs-0.16.1-cp38-cp38-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for jsonschema_rs-0.16.1-cp38-cp38-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 09cfc507309218e958c08470bd5d9514fa690fef25ff9f857a9fa9ec5ded1229
MD5 9cf8f8736c6584c48f23738f3876f585
BLAKE2b-256 d909a1eb43087af8df0735adb5bd566466def2df9c39548f035e830151b4d325

See more details on using hashes here.

File details

Details for the file jsonschema_rs-0.16.1-cp37-cp37m-win_amd64.whl.

File metadata

File hashes

Hashes for jsonschema_rs-0.16.1-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 627dbeab60388bca224c808387f03059df84058a5f97f360ee0331df1f7d1f19
MD5 f2683b60683d2ddf2147a92c4cf00b30
BLAKE2b-256 00a037e898f9c6ce2e7b72e38d945b513f00dcf4e1f073bfdd244a9529f967ce

See more details on using hashes here.

File details

Details for the file jsonschema_rs-0.16.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for jsonschema_rs-0.16.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8dda9258aeb98ef6d6009d2b07ae3a8b4965f14680471ef025255d771f97ea05
MD5 eccee360f57dec9529580e95308df34a
BLAKE2b-256 d3ac31dfb4ad5285455f3c41f77de04846ee8e459e9205d9cd0993085bd200aa

See more details on using hashes here.

File details

Details for the file jsonschema_rs-0.16.1-cp37-cp37m-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for jsonschema_rs-0.16.1-cp37-cp37m-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 e9790b587bf47f2a6586dde2f1c35edb8c5bbc034313f01e34b6cc17250620d6
MD5 c76f74d5aefda9eee4a41f7675bb805c
BLAKE2b-256 95d5479f615e2bfe9a30a0f82176729403dd730484070f7af63cf3095220623e

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