Package: jsonschema-rs
Fast JSON Schema validation for Python implemented in Rust.Supported drafts:Draft 7Draft 6Draft 4There are some notable restrictions at the moment:The underlying crate doesn’t support arbitrary precision integers yet, which may lead toSystemErrorwhen such value is used;Unicode surrogates are not supported;InstallationTo installjsonschema-rsviapiprun the following command:pipinstalljsonschema-rsUsageTo check if the input document is valid:importjsonschema_rsvalidator=jsonschema_rs.JSONSchema({"minimum":42})validator.is_valid(45)# Trueor:importjsonschema_rsvalidator=jsonschema_rs.JSONSchema({"minimum":42})validator.validate(41)# raises ValidationErrorIf you have a schema as a JSON string, then you could usejsonschema_rs.JSONSchema.from_strto avoid parsing on the Python side:importjsonschema_rsvalidator=jsonschema_rs.JSONSchema.from_str('{"minimum": 42}')...PerformanceAccording to our benchmarks,jsonschema-rsis usually faster than existing alternatives in real-life scenarios.However, for small schemas & inputs it might be slower thanfastjsonschemaorjsonschemaon PyPy.Input values and schemasZuoraOpenAPI schema (zuora.json). Validated againstOpenAPI 3.0 JSON Schema(openapi.json).KubernetesSwagger schema (kubernetes.json). Validated againstSwagger JSON Schema(swagger.json).Canadian border in GeoJSON format (canada.json). Schema is taken from theGeoJSON website(geojson.json).Concert data catalog (citm_catalog.json). Schema is inferred viainfers-jsonschema& manually adjusted (citm_catalog_schema.json).Fastis taken fromfastjsonschema benchmarks(fast_schema.json,f`ast_valid.json`andfast_invalid.json).CaseSchema sizeInstance sizeOpenAPI18 KB4.5 MBSwagger25 KB3.0 MBCanada4.8 KB2.1 MBCITM catalog2.3 KB501 KBFast (valid)595 B55 BFast (invalid)595 B60 BCompiled validators (when the input schema is compiled once and reused later).jsonschema-rscomes in three variants in the tables below:validate. This method raisesValidationErroron errors or returnsNoneon 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 thevalidatevariant.Small schemas:librarytrue{"minimum": 10}Fast (valid)Fast (invalid)jsonschema-rs[validate]93.84 ns94.83 ns1.2 us1.84 usjsonschema-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:libraryZuora (OpenAPI)Kubernetes (Swagger)Canada (GeoJSON)CITM catalogjsonschema-rs[validate]17.311 ms15.194 ms5.018 ms4.765 msjsonschema-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:fastjsonschemafails to compile the Open API spec due to the presence of theuri-referenceformat (that is not defined in Draft 4). However, unknown formats areexplicitly supportedby the spec.The bigger the input is the bigger is performance win. You can take a look at benchmarks inbenches/bench.py.Package versions:jsonschema-rs- latest version from the repositoryjsonschema-3.2.0fastjsonschema-2.15.1Measured with stable Rust 1.56, CPython 3.9.7 / PyPy3 7.3.6 on i8700K (12 cores), 32GB RAM, Arch Linux.Python supportjsonschema-rssupports CPython 3.7, 3.8, 3.9, 3.10, and 3.11.LicenseThe code in this project is licensed underMIT license.
By contributing tojsonschema-rs, you agree that your contributions
will be licensed under its MIT license.