corvus-json-schema 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- corvus_json_schema-0.1.0/.gitignore +9 -0
- corvus_json_schema-0.1.0/LICENSE +201 -0
- corvus_json_schema-0.1.0/PKG-INFO +245 -0
- corvus_json_schema-0.1.0/README.md +224 -0
- corvus_json_schema-0.1.0/VERSIONHISTORY.md +15 -0
- corvus_json_schema-0.1.0/pyproject.toml +66 -0
- corvus_json_schema-0.1.0/src/corvus_json_schema/__init__.py +260 -0
- corvus_json_schema-0.1.0/src/corvus_json_schema/_metaschemas.py +27 -0
- corvus_json_schema-0.1.0/src/corvus_json_schema/cli.py +95 -0
- corvus_json_schema-0.1.0/src/corvus_json_schema/codegen.py +1864 -0
- corvus_json_schema-0.1.0/src/corvus_json_schema/collecting.py +743 -0
- corvus_json_schema-0.1.0/src/corvus_json_schema/compiler.py +803 -0
- corvus_json_schema-0.1.0/src/corvus_json_schema/dialect.py +104 -0
- corvus_json_schema-0.1.0/src/corvus_json_schema/formats.py +693 -0
- corvus_json_schema-0.1.0/src/corvus_json_schema/loader.py +345 -0
- corvus_json_schema-0.1.0/src/corvus_json_schema/node.py +325 -0
- corvus_json_schema-0.1.0/src/corvus_json_schema/options.py +60 -0
- corvus_json_schema-0.1.0/src/corvus_json_schema/pattern.py +522 -0
- corvus_json_schema-0.1.0/src/corvus_json_schema/py.typed +0 -0
- corvus_json_schema-0.1.0/src/corvus_json_schema/results.py +213 -0
- corvus_json_schema-0.1.0/src/corvus_json_schema/runtime.py +212 -0
- corvus_json_schema-0.1.0/src/corvus_json_schema/uri.py +174 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: corvus-json-schema
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A high-performance JSON Schema evaluator (draft 4 to 2020-12) that compiles schemas to specialised Python, with results collection and annotations, ported from the Corvus.Text.Json V5 runtime evaluator.
|
|
5
|
+
Project-URL: Homepage, https://github.com/corvus-dotnet/Corvus.JsonSchema/tree/main/src-py/corvus-json-schema
|
|
6
|
+
Project-URL: Repository, https://github.com/corvus-dotnet/Corvus.JsonSchema
|
|
7
|
+
Project-URL: Issues, https://github.com/corvus-dotnet/Corvus.JsonSchema/issues
|
|
8
|
+
Author: endjin
|
|
9
|
+
License-Expression: Apache-2.0
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: draft-07,draft-2019-09,draft-2020-12,json-schema,jsonschema,validation,validator
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Topic :: File Formats :: JSON :: JSON Schema
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Requires-Dist: regex>=2024.11.6
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# corvus-json-schema
|
|
23
|
+
|
|
24
|
+
A JSON Schema evaluator for Python (draft 4, 6, 7, 2019-09 and 2020-12), ported from the Corvus.Text.Json V5
|
|
25
|
+
standalone evaluator (`Corvus.Text.Json.RuntimeEvaluator`, see [docs/RuntimeEvaluator.md](../../docs/RuntimeEvaluator.md))
|
|
26
|
+
by way of its TypeScript port. It compiles a schema once into specialised Python and validates parsed JSON values
|
|
27
|
+
(`json.loads` output) against it. Pure Python: its one dependency is [`regex`](https://pypi.org/project/regex/), for
|
|
28
|
+
the Unicode properties of ECMA-262 patterns.
|
|
29
|
+
|
|
30
|
+
For the same evaluator in Rust, behind the same Python API, see
|
|
31
|
+
[corvus-json-schema-rs](../corvus-json-schema-rs/README.md).
|
|
32
|
+
|
|
33
|
+
- **Conformant**: passes all 7,967 tests of the JSON-Schema-Test-Suite (required, optional and `optional/format`,
|
|
34
|
+
every draft), including `draft4/optional/zeroTerminatedFloats.json`, which the C# and TypeScript evaluators exclude
|
|
35
|
+
because their parsers cannot tell `1.0` from `1` (`json.loads` can).
|
|
36
|
+
- **Fast**: see [Performance](#performance).
|
|
37
|
+
- **Results and annotations**: evaluate with a results collector at the Basic, Detailed or Verbose level for the same
|
|
38
|
+
rows (locations, messages, order) as the C# `JsonSchemaResultsCollector`, and annotations as
|
|
39
|
+
`JsonSchemaAnnotationProducer` extracts them; passes all of the suite's annotation tests.
|
|
40
|
+
- **Standalone**: a schema can be emitted as a Python module that depends only on the small runtime.
|
|
41
|
+
- Python 3.10 or later; typed (`py.typed`, checked with `mypy --strict` and pyright).
|
|
42
|
+
|
|
43
|
+
## Install
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
pip install corvus-json-schema
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
import corvus_json_schema as cjs
|
|
53
|
+
|
|
54
|
+
validate = cjs.compile({
|
|
55
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
56
|
+
"type": "object",
|
|
57
|
+
"properties": {"id": {"type": "integer", "minimum": 1}},
|
|
58
|
+
"required": ["id"],
|
|
59
|
+
"unevaluatedProperties": False,
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
validate({"id": 3}) # True
|
|
63
|
+
validate({"id": 0}) # False
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`compile` takes the schema as a parsed value or as JSON text, and these options, as keywords or as a `CompileOptions`
|
|
67
|
+
(the Python counterparts of `JsonSchemaEvaluatorOptions`):
|
|
68
|
+
|
|
69
|
+
| Option | Meaning |
|
|
70
|
+
|---|---|
|
|
71
|
+
| `default_dialect` | Dialect for schemas without `$schema` (`Dialect.DRAFT4` … `Dialect.DRAFT202012`, default 2020-12). |
|
|
72
|
+
| `assert_format` | `True` asserts `format`, `False` never does; `None` (the default) follows the vocabularies (2020-12 `format-assertion`). |
|
|
73
|
+
| `assert_format_in_legacy_drafts` | With `assert_format` unset, also assert `format` in drafts 4 to 7. |
|
|
74
|
+
| `assert_content` | Assert `contentEncoding`/`contentMediaType` in draft 7 (default `True`). |
|
|
75
|
+
| `formats` | Custom format assertions by name, e.g. `{"even": lambda s: len(s) % 2 == 0}`. |
|
|
76
|
+
| `resolve_document` | `(uri) -> schema \| JSON text \| None`, for remote `$ref`s. The standard metaschemas are built in. |
|
|
77
|
+
| `base_uri` | Base URI of the root document. |
|
|
78
|
+
| `entry_point` | Evaluate from a subschema, e.g. `#/$defs/item`. |
|
|
79
|
+
| `max_depth` | Depth limit for in-place recursion on a cycle (default 128); exceeding it raises `SchemaEvaluationDepthError`. |
|
|
80
|
+
|
|
81
|
+
An unresolvable reference or an invalid pattern raises `SchemaCompilationError` at compile time.
|
|
82
|
+
|
|
83
|
+
Instances are the values `json.loads` produces: `dict`, `list`, `str`, `int`, `float`, `bool` and `None`. The generated
|
|
84
|
+
code tests exact types (`type(x) is dict`), so subclasses such as `OrderedDict` are not objects to it; `True` is a
|
|
85
|
+
boolean, never the number 1.
|
|
86
|
+
|
|
87
|
+
### Results and annotations
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
validate = cjs.compile(schema)
|
|
91
|
+
collector = cjs.JsonSchemaResultsCollector.create(cjs.ResultsLevel.DETAILED)
|
|
92
|
+
validate.evaluate({"id": 0}, collector) # False
|
|
93
|
+
for r in collector.results:
|
|
94
|
+
... # r.is_match, r.message, r.evaluation_location, r.schema_evaluation_location, r.document_evaluation_location
|
|
95
|
+
|
|
96
|
+
verbose = cjs.JsonSchemaResultsCollector.create(cjs.ResultsLevel.VERBOSE)
|
|
97
|
+
validate.evaluate({"id": 3}, verbose)
|
|
98
|
+
cjs.collect_annotations(verbose) # {"": {"title": {"#": "Person"}}, "/id": {...}}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
The levels and rows are those of the C# collector: `BASIC` records failures without message text, `DETAILED` adds the
|
|
102
|
+
text, `VERBOSE` records every keyword (passing ones and annotations included). Evaluation without a collector runs
|
|
103
|
+
the generated code; with one, an interpreter over the same compiled graph evaluates every keyword and reports it
|
|
104
|
+
(`collecting.py`), so results collection costs nothing when unused.
|
|
105
|
+
|
|
106
|
+
### Standalone modules
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
from pathlib import Path
|
|
110
|
+
|
|
111
|
+
Path("person_validator.py").write_text(cjs.generate_module(schema))
|
|
112
|
+
# from person_validator import validate, evaluate -- needs only corvus_json_schema.runtime
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Or from the command line:
|
|
116
|
+
|
|
117
|
+
```sh
|
|
118
|
+
corvus-json-schema generate schema.json -o validator.py
|
|
119
|
+
corvus-json-schema validate schema.json doc1.json doc2.json
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## How it works
|
|
123
|
+
|
|
124
|
+
The pipeline follows the C# evaluator stage for stage:
|
|
125
|
+
|
|
126
|
+
1. **Load** (`loader.py`): documents, resources (`$id`/`id`), anchors, `$recursiveAnchor`, and the dialect and
|
|
127
|
+
vocabularies of each resource, including custom metaschemas read through `$vocabulary`.
|
|
128
|
+
2. **Compile** (`compiler.py`): one `SchemaNode` per distinct schema location with its keywords pre-digested; `$ref`
|
|
129
|
+
resolved at compile time; `$dynamicRef`/`$recursiveRef` resolved statically wherever the entry resource decides
|
|
130
|
+
them, so a dynamic scope is kept only for references that stay dynamic.
|
|
131
|
+
3. **Analyse**: which nodes can mark evaluated properties/items, in-place recursion cycles (only those nodes carry a
|
|
132
|
+
depth guard), and `oneOf`/`anyOf` discriminators.
|
|
133
|
+
4. **Generate** (`codegen.py`), as the TypeScript port does: each node becomes one function with exactly the checks it
|
|
134
|
+
needs, compiled once with `compile()`/`exec()`, so a validation is a single call into that code. The analyses that
|
|
135
|
+
select C# plans select the shape of the code:
|
|
136
|
+
- pure-`$ref` chains are elided; leaf schemas (type, `const`/`enum`, string and number keywords) are tested at
|
|
137
|
+
their call sites as one expression, with no call;
|
|
138
|
+
- small objects are checked by direct lookups (`"name" in x`), `required` and `additionalProperties: false` by set
|
|
139
|
+
operations on `x.keys()`, and larger objects by one pass dispatching each name through a dict;
|
|
140
|
+
- an `allOf`/`$ref` composition of plain object schemas checks an object in one pass over the merged names (the C#
|
|
141
|
+
and Rust flat fused plan);
|
|
142
|
+
- `oneOf`/`anyOf` narrow by a discriminator property or by type dispatch;
|
|
143
|
+
- `unevaluatedProperties`/`unevaluatedItems` are decided from static coverage where their contributors allow it,
|
|
144
|
+
with conditions under `if`/`then`/`else` and dependencies decided once per object;
|
|
145
|
+
- patterns that are literals (a prefix, an exact value, alternatives, a substring) match without a regular
|
|
146
|
+
expression; others are ECMA-262 translated to Python's syntax (`pattern.py`) and run by `re`, or by `regex` for
|
|
147
|
+
Unicode properties;
|
|
148
|
+
- structurally identical functions are merged by partition refinement.
|
|
149
|
+
|
|
150
|
+
Generated code that keeps state (a live dynamic scope or an in-place depth count) evaluates under a lock, so a
|
|
151
|
+
validator can be shared between threads.
|
|
152
|
+
|
|
153
|
+
## Performance
|
|
154
|
+
|
|
155
|
+
Measured with [jsonschema-benchmark](https://github.com/sourcemeta-research/jsonschema-benchmark)'s corpora and harness
|
|
156
|
+
protocol (`bench/corpora.py`: a fresh process per corpus and implementation, warm-up, then the best of five passes),
|
|
157
|
+
against the Rust-backed [corvus-json-schema-rs](../corvus-json-schema-rs/README.md), the
|
|
158
|
+
[jsonschema-rs](https://pypi.org/project/jsonschema-rs/) bindings and [fastjsonschema](https://pypi.org/project/fastjsonschema/)
|
|
159
|
+
(another generator of Python code, draft 4 to 7). See [Performance results](#performance-results) for the figures.
|
|
160
|
+
|
|
161
|
+
```sh
|
|
162
|
+
python bench/corpora.py --schemas <jsonschema-benchmark>/schemas --impls corvus,corvus-rs,jsonschema-rs,fastjsonschema
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Performance results
|
|
166
|
+
|
|
167
|
+
CPython 3.12.13 on an Intel Core i7-13800H (WSL2, pinned to the performance cores), jsonschema-rs 0.58.3,
|
|
168
|
+
fastjsonschema 2.22.2, 2026-10-01. The validation columns are one warm pass over all of a corpus's instances (the best of
|
|
169
|
+
five passes after warm-up, the median of three processes); "compile" is the schema compilation (for this package, the
|
|
170
|
+
generation of the Python code and, for programs over 64 KB of source, the compilation of only the functions an instance
|
|
171
|
+
reaches, on first use).
|
|
172
|
+
|
|
173
|
+
| Corpus | Instances | corvus (pure) | corvus-rs | jsonschema-rs | fastjsonschema | corvus compile | corvus-rs compile |
|
|
174
|
+
|---|---:|---:|---:|---:|---:|---:|---:|
|
|
175
|
+
| ansible-meta | 333 | 345.0 µs | 105.1 µs | 286.2 µs | 7.96 ms | 10.92 ms | 876.6 µs |
|
|
176
|
+
| aws-cdk | 483 | 88.8 µs | 45.7 µs | 61.5 µs | 229.9 µs | 599.4 µs | 78.8 µs |
|
|
177
|
+
| babelrc | 794 | 260.2 µs | 97.4 µs | 130.9 µs | 898.2 µs | 1.54 ms | 189.6 µs |
|
|
178
|
+
| clang-format | 133 | 140.0 µs | 29.0 µs | 38.1 µs | 571.0 µs | 8.46 ms | 765.5 µs |
|
|
179
|
+
| cmake-presets | 967 | 12.29 ms | 3.65 ms | 6.64 ms | 250.54 ms | 19.01 ms | 1.58 ms |
|
|
180
|
+
| code-climate | 2484 | 354.0 µs | 284.6 µs | 329.4 µs | 1.77 ms | 909.5 µs | 121.1 µs |
|
|
181
|
+
| cql2 | 109 | 193.3 µs | 61.2 µs | 127.3 µs | 5.07 ms (!) | 12.54 ms | 800.1 µs |
|
|
182
|
+
| cspell | 981 | 1.52 ms | 1.16 ms | n/a | 52.35 ms | 12.24 ms | 1.68 ms |
|
|
183
|
+
| cypress | 981 | 279.8 µs | 108.8 µs | 185.5 µs | 1.34 ms | 2.76 ms | 267.4 µs |
|
|
184
|
+
| deno | 987 | 831.0 µs | 235.4 µs | 313.3 µs | 2.86 ms | 3.28 ms | 333.8 µs |
|
|
185
|
+
| dependabot | 967 | 1.10 ms | 334.4 µs | 435.5 µs | 2.84 ms | 2.22 ms | 154.9 µs |
|
|
186
|
+
| draft-04 | 563 | 12.55 ms | 4.39 ms | 6.05 ms | 16.83 ms | 2.35 ms | 185.6 µs |
|
|
187
|
+
| fabric-mod | 911 | 1.50 ms | 368.0 µs | 608.8 µs | 42.99 ms | 3.11 ms | 300.2 µs |
|
|
188
|
+
| geojson | 500 | 49.76 ms | 16.65 ms | 19.56 ms | 2173.86 ms | 10.41 ms | 516.9 µs |
|
|
189
|
+
| gitpod-configuration | 986 | 698.3 µs | 222.5 µs | 274.0 µs | 1.82 ms | 3.23 ms | 258.0 µs |
|
|
190
|
+
| helm-chart-lock | 3888 | 1.90 ms | 736.7 µs | 1.11 ms | 6.07 ms | 650.1 µs | 104.3 µs |
|
|
191
|
+
| importmap | 964 | 253.7 µs | 122.3 µs | 150.6 µs | 825.1 µs | 577.2 µs | 79.0 µs |
|
|
192
|
+
| jasmine | 980 | 338.8 µs | 99.0 µs | 197.6 µs | 2.74 ms | 1.39 ms | 123.1 µs |
|
|
193
|
+
| jsconfig | 981 | 906.9 µs | 301.4 µs | 338.1 µs | 19.70 ms | 8.38 ms | 1.70 ms |
|
|
194
|
+
| jshintrc | 966 | 1.16 ms | 329.9 µs | 523.8 µs | 3.96 ms | 1.80 ms | 207.2 µs |
|
|
195
|
+
| krakend | 47 | 698.0 µs | 139.7 µs | n/a | 4.65 ms | 29.33 ms | 4.22 ms |
|
|
196
|
+
| lazygit | 280 | 338.5 µs | 77.7 µs | 106.1 µs | 9.18 ms | 10.74 ms | 956.1 µs |
|
|
197
|
+
| lerna | 985 | 217.9 µs | 138.2 µs | 156.7 µs | 659.5 µs | 1.45 ms | 113.8 µs |
|
|
198
|
+
| nest-cli | 1025 | 579.8 µs | 186.4 µs | 231.9 µs | 3.85 ms | 3.79 ms | 370.0 µs |
|
|
199
|
+
| omnisharp | 987 | 719.2 µs | 215.1 µs | 253.6 µs | 2.21 ms | 2.96 ms | 325.3 µs |
|
|
200
|
+
| openapi | 107 | 16.90 ms | 5.23 ms | 10.91 ms | 88.9 µs | 13.63 ms | 1.44 ms |
|
|
201
|
+
| pre-commit-hooks | 985 | 1.28 ms | 308.9 µs | 467.0 µs | 22.78 ms | 1.44 ms | 195.4 µs |
|
|
202
|
+
| pulumi | 3807 | 1.67 ms | 627.3 µs | 786.2 µs | 19.22 ms | 3.54 ms | 291.8 µs |
|
|
203
|
+
| semantic-release | 794 | 291.5 µs | 118.5 µs | 168.0 µs | 9.30 ms | 1.43 ms | 125.5 µs |
|
|
204
|
+
| stale | 961 | 466.8 µs | 147.5 µs | 214.8 µs | 1.16 ms | 1.25 ms | 157.6 µs |
|
|
205
|
+
| stylecop | 983 | 934.6 µs | 253.1 µs | 296.6 µs | 3.26 ms | 3.56 ms | 236.8 µs |
|
|
206
|
+
| tmuxinator | 382 | 206.2 µs | 80.0 µs | 98.7 µs | 5.77 ms | 1.67 ms | 153.3 µs |
|
|
207
|
+
| ui5 | 942 | 1.33 ms | 624.4 µs | 527.8 µs | 10.50 ms | 20.00 ms | 2.36 ms |
|
|
208
|
+
| ui5-manifest | 611 | 6.81 ms | 2.18 ms | 2.47 ms | n/a | 70.67 ms | 8.92 ms |
|
|
209
|
+
| unreal-engine-uproject | 859 | 2.17 ms | 400.9 µs | 534.8 µs | 12.30 ms | 2.38 ms | 284.7 µs |
|
|
210
|
+
| vercel | 710 | 671.9 µs | 191.0 µs | 293.7 µs | 2.81 ms | 9.46 ms | 653.5 µs |
|
|
211
|
+
| yamllint | 966 | 61.3 µs | 61.9 µs | 65.7 µs | 169.1 µs | 310.3 µs | 78.4 µs |
|
|
212
|
+
|
|
213
|
+
- **corvus-json-schema (pure Python)** is faster than fastjsonschema, the other generator of Python code, on 34 of the
|
|
214
|
+
35 corpora both can run: its warm time is a sixth of fastjsonschema's (geometric mean 5.86 times faster).
|
|
215
|
+
fastjsonschema supports drafts 4 to 7 only, so on openapi, a 2020-12 schema, it applies few of the keywords (its one
|
|
216
|
+
faster time), on cql2 it rejects valid instances (!), and it cannot compile ui5-manifest.
|
|
217
|
+
- **[corvus-json-schema-rs](../corvus-json-schema-rs/README.md)**, the same API backed by the Rust evaluator, takes
|
|
218
|
+
0.35 of the pure package's time (geometric mean over all 37 corpora), and 0.72 of jsonschema-rs's (faster on 34 of
|
|
219
|
+
the 35 corpora jsonschema-rs can run; it rejects the patterns of cspell and krakend).
|
|
220
|
+
|
|
221
|
+
## Tests
|
|
222
|
+
|
|
223
|
+
```sh
|
|
224
|
+
pip install -e . pytest
|
|
225
|
+
python -m pytest # API and results tests
|
|
226
|
+
python tests/suite.py # the JSON-Schema-Test-Suite (--draft, --filter, --verbose; --module through
|
|
227
|
+
# generate_module; --collect basic|detailed|verbose through a results collector;
|
|
228
|
+
# --impl corvus_json_schema_rs for the Rust-backed package)
|
|
229
|
+
python tests/annotations.py # the suite's annotation tests through a verbose collector
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
The suite is read from the repository's `JSON-Schema-Test-Suite` submodule (`git submodule update --init
|
|
233
|
+
JSON-Schema-Test-Suite`), or from `$JSON_SCHEMA_TEST_SUITE`.
|
|
234
|
+
|
|
235
|
+
## Limitations and differences from the C# evaluator
|
|
236
|
+
|
|
237
|
+
- **Results collection** matches the C# collector's rows; annotation values are the values re-serialised
|
|
238
|
+
(`json.dumps`), not the schema's source text, because schemas arrive as parsed values, and numbers in messages are
|
|
239
|
+
written as the Rust evaluator writes them (`2.0`, `1e+21`).
|
|
240
|
+
- **Numbers** are Python's: integers are exact at any size, and floats are doubles. `multipleOf` with a fractional
|
|
241
|
+
divisor is computed exactly on the shortest decimal forms of the doubles.
|
|
242
|
+
- **Regular expressions** are ECMA-262 translated to Python's syntax with ECMA's meaning (`\d` and `\w` are ASCII, `.`
|
|
243
|
+
excludes every line terminator, `$` matches only at the end). A pattern only valid without the `u` flag (an identity
|
|
244
|
+
escape such as `\-`, a literal brace) is accepted as JavaScript's `RegExp` would accept it without the flag.
|
|
245
|
+
- Custom `formats` functions cannot be serialised into standalone modules.
|