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.0.tar.gz (90.2 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.0-cp310-cp310-win_amd64.whl (1.7 MB view details)

Uploaded CPython 3.10Windows x86-64

jsonschema_rs-0.16.0-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.0-cp310-cp310-macosx_11_0_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.10macOS 11.0+ x86-64

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

Uploaded CPython 3.9Windows x86-64

jsonschema_rs-0.16.0-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.0-cp39-cp39-macosx_11_0_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.9macOS 11.0+ x86-64

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

Uploaded CPython 3.8Windows x86-64

jsonschema_rs-0.16.0-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.0-cp38-cp38-macosx_11_0_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.8macOS 11.0+ x86-64

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

Uploaded CPython 3.7mWindows x86-64

jsonschema_rs-0.16.0-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.0-cp37-cp37m-macosx_11_0_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.7mmacOS 11.0+ x86-64

File details

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

File metadata

  • Download URL: jsonschema_rs-0.16.0.tar.gz
  • Upload date:
  • Size: 90.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.13

File hashes

Hashes for jsonschema_rs-0.16.0.tar.gz
Algorithm Hash digest
SHA256 e652735f5e04340ff876161a358274525395a067a2d6280774031301b07b0e84
MD5 b186d8cfbaf085b10235889ce04d8db9
BLAKE2b-256 8fedeb95b5334e6371c2947fbbaa12310ebbc9d9bc47f720959c57ea615be0ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonschema_rs-0.16.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 78a5ff7ac7492fb04c5bd66e33fa559524d0a99e8622fcb80c45946a735e03f7
MD5 05434108c87a410b4e887e03baf63cda
BLAKE2b-256 fa42fc7b2e63077eff500f77beecb210c66a7bcc35b25c2c040ee68c8f7fd53d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonschema_rs-0.16.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2ebc5bf0a4a519c9353384d1a0d8435962a475be634cb6f20b2d069507a492ab
MD5 41462d3dd1c89f0d83c3c636a0b48c99
BLAKE2b-256 2f35fe0785123ef5388bab9ab44e1d1f8ef54952d8aa50973f2964e3ae68ff43

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonschema_rs-0.16.0-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 fb0653a1f014619d6af3198ae4d9076fc7be69a42e0167359eced5b81d6db7ca
MD5 34f1ae7038fc723d1afa50bdd0e052a6
BLAKE2b-256 ea3c08b1953912457ce7b08f75ebe74c28e017452fb2bf4f580c408731b2984a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonschema_rs-0.16.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 4fce25660b19d0018e25f8d207cf01fd5e69a4c09476a20377c1c94bf74187d2
MD5 53d337930a5cd8fc40ff35bfaaf6196e
BLAKE2b-256 7578b36faa6636d78fbff940dfb09d53ebbcf024eb12629998998ede80d27405

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonschema_rs-0.16.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9bc1d0832d9f622b192b7906e46cdced1eb82749e7728c39468f2347915d9e8b
MD5 1361019e8fb75eeb9eb771367f4c162b
BLAKE2b-256 be77641e25cb7fc0e49c78dae65034191ba3831bdba6546c93727ad4798349c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonschema_rs-0.16.0-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 6ce9d0c3a89a7f96914d7058acc9e8987c31362c75b2e815ef786d84896feb1b
MD5 865f553c6ff685996f540d11d7a45406
BLAKE2b-256 9c8194ac35bef0c431cd223816f5ec10be6abbc56ff6a0bf589d665a5a636395

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonschema_rs-0.16.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 954582ee8fab8428776a0eafbc717a7a459c892aed46e319732a973892073ea8
MD5 a05afd8aabc12e2e9ff0748f18a1d0b7
BLAKE2b-256 1e97f770665db43f40ec330ffc8568cf178b3f43e1f575570a7741545cf3cf73

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonschema_rs-0.16.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bfd1ea307c560d8d0ad11edcc8c303cd3b698e19bb52385861d81bced9277e90
MD5 6f0d0735584c830efb98c5723a0b5f45
BLAKE2b-256 62271eb78da3bed3fa3d5a175a0fbc3bfe8d59e759d8cb1223e044b5238446a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonschema_rs-0.16.0-cp38-cp38-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 d258791a5f91b1ffc2fde5a90e154d50340066a572f88f128b15ed3249a3be18
MD5 6278b2a2b4af535263f32fbbd9e9f1ba
BLAKE2b-256 fdc2b414b397faa5494eb06c7e95cf29e2b5fe55e2509b4d4186f25b559997cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonschema_rs-0.16.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 c46afdbf743d21bd98fee174c25cc72f8ce594217d21c09567bbea0912f2650a
MD5 02fc7b66d86d36f2b91a43e2689bf921
BLAKE2b-256 aa7a585d7ac535a1e04f86c7704d8f9067cc6d3113d3a1094342370c78b01620

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonschema_rs-0.16.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5363f02c68da03bfa7aa6c1169dbc06c21c59d47612d555f38ada7b96262c5bc
MD5 170282b364a846951a7573e0d99a6b31
BLAKE2b-256 30d9d1a0932e0ef4e1199661806c6ad8d28f3e21bcd4c54ba389af1967fbe05b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jsonschema_rs-0.16.0-cp37-cp37m-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 9ddfda53baf15c507921aa6a80c9d65c8919f51073d1385aef8c8f692f387176
MD5 6f5a6bef37f73ad7e6d02395ccb162b9
BLAKE2b-256 67bdf986f31cdfbdc66d020a311499295a8f19a2b4470fd4c1027b318b4d66b6

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