Debian Codegraph @

share/README.md

raw
1 # JSON Schema Test Suite
2
3 [![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](https://github.com/json-schema-org/.github/blob/main/CODE_OF_CONDUCT.md)
4 [![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
5 [![Financial Contributors on Open Collective](https://opencollective.com/json-schema/all/badge.svg?label=financial+contributors)](https://opencollective.com/json-schema)
6
7 [![DOI](https://zenodo.org/badge/5952934.svg)](https://zenodo.org/badge/latestdoi/5952934)
8 [![Build Status](https://github.com/json-schema-org/JSON-Schema-Test-Suite/workflows/Test%20Suite%20Sanity%20Checking/badge.svg)](https://github.com/json-schema-org/JSON-Schema-Test-Suite/actions?query=workflow%3A%22Test+Suite+Sanity+Checking%22)
9
10 This repository contains a set of JSON objects that implementers of JSON Schema validation libraries can use to test their validators.
11 The test suite repository exists to verify specified behavior defined by the JSON Schema specification and should not be confused with a style guide. It is not intended to demonstrate how schemas ought to be written. Tests may appear unusual or unintuitive, but they exist solely to exercise behavior prescribed by the specification.
12
13 It is meant to be language agnostic and should require only a JSON parser.
14 The conversion of the JSON objects into tests within a specific language and test framework of choice is left to be done by the validator implementer.
15
16 The recommended workflow of this test suite is to clone the `main` branch of this repository as a `git submodule` or `git subtree`. The `main` branch is always stable.
17
18 ## Coverage
19
20 All JSON Schema specification releases should be well covered by this suite, including versions draft-2020-12, draft-2019-09, draft-07, draft-06, draft-04 and draft-03.
21 Versions draft-04 and draft-03 are considered "frozen" in that less effort is put in to backport new tests to these versions.
22
23 Additional coverage is always welcome, particularly for bugs encountered in real-world implementations.
24 If you see anything missing or incorrect, please feel free to [file an issue](https://github.com/json-schema-org/JSON-Schema-Test-Suite/issues) or [submit a PR](https://github.com/json-schema-org/JSON-Schema-Test-Suite).
25
26 @gregsdennis has also started a separate [test suite](https://github.com/gregsdennis/json-schema-vocab-test-suites) that is modelled after this suite to cover third-party vocabularies.
27
28 ## Introduction to the Test Suite Structure
29
30 The tests in this suite are contained in the `tests` directory at the root of this repository.
31 Inside that directory is a subdirectory for each released version of the specification.
32
33 The structure and contents of each file in these directories is described below.
34
35 In addition to the version-specific subdirectories, one additional directory is present:
36
37 1. `latest/`: a symbolic link which points to the directory which is the most recent release (which may be useful for implementations providing specific entry points for validating against the latest version of the specification)
38
39 Inside each version directory there are a number of `.json` files each containing a collection of related tests.
40 Often the grouping is by property under test, but not always.
41 In addition to the `.json` files, each version directory contains one or more special subdirectories whose purpose is [described below](#subdirectories-within-each-version-directory), and which contain additional `.json` files.
42
43 Each `.json` file consists of a single JSON array of test cases.
44
45 ### Terminology
46
47 For clarity, we first define this document's usage of some testing terminology:
48
49 | term | definition |
50 |-----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|
51 | **test suite** | the entirety of the contents of this repository, containing tests for multiple different releases of the JSON Schema specification |
52 | **test case** | a single schema, along with a description and an array of *test*s |
53 | **test** | within a *test case*, a single test example, containing a description, instance and a boolean indicating whether the instance is valid under the test case schema |
54 | **test runner** | a program, external to this repository and authored by a user of this suite, which is executing each of the tests in the suite |
55
56 An example illustrating this structure is immediately below, and a JSON Schema containing a formal definition of the contents of test cases can be found [alongside this README](./test-schema.json).
57
58 ### Sample Test Case
59
60 Here is a single *test case*, containing one or more tests:
61
62 ```json
63 {
64 "description": "The test case description",
65 "schema": { "type": "string" },
66 "tests": [
67 {
68 "description": "a test with a valid instance",
69 "data": "a string",
70 "valid": true
71 },
72 {
73 "description": "a test with an invalid instance",
74 "data": 15,
75 "valid": false
76 }
77 ]
78 }
79 ```
80
81 ### Subdirectories Within Each Version directory
82
83 A specification version test directory may contain one or more subdirectories.
84
85 These are:
86
87 1. `optional/`: Contains tests that are considered optional. Note that this subdirectory currently conflates many reasons why a test may be optional -- it may be because tests within a particular file are indeed not required by the specification but still potentially useful to an implementer, or it may be because tests within it only apply to programming languages with particular functionality (in which case they are not truly optional in such a language). In the future this directory structure will be made richer to reflect these differences more clearly.
88
89 2. `proposals/`: Contains a subfolder for each active proposal to the specification. If the proposal is a keyword (generally the case), then the subfolder will bear the name of that keyword. Inside the proposal subfolder is a series of test files that would contain amendments to the required test suite should the proposal be incorporated into the specification. These tests should be considered volatile while the proposal is in development; however, implementations claiming to support the proposal are expected to pass its tests.
90
91 ## Using the Suite to Test a Validator Implementation
92
93 The test suite structure was described [above](#introduction-to-the-test-suite-structure).
94
95 If you are authoring a new validator implementation, or adding support for an additional version of the specification, this section describes:
96
97 1. How to implement a test runner which passes tests to your validator
98 2. Assumptions the suite makes about how the test runner will configure your validator
99 3. Invariants the test suite claims to hold for its tests
100
101 ### How to Implement a Test Runner
102
103 Presented here is a possible implementation of a test runner.
104 The precise steps described do not need to be followed exactly, but the results of your own procedure should produce the same effects.
105
106 To test a specific version:
107
108 * For 2019-09 and later published versions, implementations that are able to detect the version of each schema via `$schema` SHOULD be configured to do so
109 * For draft-07 and earlier, v1 (not yet released), and implementations unable to detect via `$schema`, implementations MUST be configured to expect the version matching the test directory name
110 * Load any remote references [described below](#additional-assumptions) and configure your implementation to retrieve them via their URIs
111 * Walk the filesystem tree for that version's subdirectory and for each `.json` file found:
112
113 * if the file is located in the root of the version directory:
114
115 * for each test case present in the file:
116
117 * load the schema from the `"schema"` property
118 * load (or log) the test case description from the `"description"` property for debugging or outputting
119 * for each test in the `"tests"` property:
120
121 * load the instance to be tested from the `"data"` property
122 * load (or log) the individual test description from the `"description"` property for debugging or outputting
123
124 * use the schema loaded above to validate whether the instance is considered valid under your implementation
125
126 * if the result from your implementation matches the value found in the `"valid"` property, your implementation correctly implements the specific example
127 * if the result does not match, or your implementation errors or crashes, your implementation does not correctly implement the specific example
128
129 * otherwise it is located in a special subdirectory as described above.
130 Follow the additional assumptions and restrictions for the containing subdirectory, then run the test case as above.
131
132 If your implementation supports multiple versions, run the above procedure for each version supported, configuring your implementation as appropriate to call each version individually.
133
134 ### Additional Assumptions
135
136 1. The suite, notably in its `refRemote.json` file in each specification version directory, expects a number of remote references to be configured.
137 These are JSON documents, identified by URI, which are used by the suite to test the behavior of the `$ref` keyword (and related keywords).
138 Depending on your implementation, you may configure how to "register" these *either*:
139
140 * by directly retrieving them off the filesystem from the `remotes/` directory, in which case you should load each schema with a retrieval URI of `http://localhost:1234` followed by the relative path from the remotes directory -- e.g. a `$ref` to `http://localhost:1234/foo/bar/baz.json` is expected to resolve to the contents of the file at `remotes/foo/bar/baz.json`
141
142 * or alternatively, by executing `bin/jsonschema_suite remotes` using the executable in the `bin/` directory, which will output a JSON object containing all of the remotes combined, e.g.:
143
144 ```
145
146 $ bin/jsonschema_suite remotes
147 ```
148 ```json
149 {
150 "http://localhost:1234/baseUriChange/folderInteger.json": {
151 "type": "integer"
152 },
153 "http://localhost:1234/baseUriChangeFolder/folderInteger.json": {
154 "type": "integer"
155 }
156 }
157 ```
158
159 2. Test cases found within [special subdirectories](#subdirectories-within-each-version-directory) may require additional configuration to run.
160 In particular, when running tests within the `optional/format` subdirectory, test runners should configure implementations to enable format validation, where the implementation supports it.
161
162 ### Invariants & Guarantees
163
164 The test suite guarantees a number of things about tests it defines.
165 Any deviation from the below is generally considered a bug.
166 If you suspect one, please [file an issue](https://github.com/json-schema-org/JSON-Schema-Test-Suite/issues/new):
167
168 1. All files containing test cases are valid JSON.
169 2. The contents of the `"schema"` property in a test case are always valid
170 JSON Schemas under the corresponding specification.
171
172 The rationale behind this is that we are testing instances in a test's `"data"` element, and not the schema itself.
173 A number of tests *do* test the validity of a schema itself, but do so by representing the schema as an instance inside a test, with the associated meta-schema in the `"schema"` property (via the `"$ref"` keyword):
174
175 ```json
176 {
177 "description": "Test the \"type\" schema keyword",
178 "schema": {
179 "$ref": "https://json-schema.org/draft/2019-09/schema"
180 },
181 "tests": [
182 {
183 "description": "Valid: string",
184 "data": {
185 "type": "string"
186 },
187 "valid": true
188 },
189 {
190 "description": "Invalid: null",
191 "data": {
192 "type": null
193 },
194 "valid": false
195 }
196 ]
197 }
198 ```
199 See below for some [known limitations](#known-limitations).
200
201 ## Known Limitations
202
203 This suite expresses its assertions about the behavior of an implementation *within* JSON Schema itself.
204 Each test is the application of a schema to a particular instance.
205 This means that the suite of tests can test against any behavior a schema can describe, and conversely cannot test against any behavior which a schema is incapable of representing, even if the behavior is mandated by the specification.
206
207 For example, a schema can require that a string is a _URI-reference_ and even that it matches a certain pattern, but even though the specification contains [recommendations about URIs being normalized](https://json-schema.org/draft/2020-12/json-schema-core.html#name-the-id-keyword), a JSON schema cannot today represent this assertion within the core vocabularies of the specifications, so no test covers this behavior.
208
209 ## Who Uses the Test Suite
210
211 This suite is being used by:
212
213 ### Clojure
214
215 * [jinx](https://github.com/juxt/jinx)
216 * [json-schema](https://github.com/tatut/json-schema)
217 * [M3](https://github.com/JulesGosnell/m3) (all drafts, pure Clojure — also usable from Java, Kotlin, Scala, and JavaScript)
218
219 ### Coffeescript
220
221 * [jsck](https://github.com/pandastrike/jsck)
222
223 ### Common Lisp
224
225 * [json-schema](https://github.com/fisxoj/json-schema)
226
227 ### C++
228
229 * [Blaze](https://github.com/sourcemeta/blaze)
230 * [Modern C++ JSON schema validator](https://github.com/pboettch/json-schema-validator)
231 * [Valijson](https://github.com/tristanpenman/valijson)
232
233 ### Dart
234
235 * [json\_schema](https://github.com/patefacio/json_schema)
236
237 ### Elixir
238
239 * [ex\_json\_schema](https://github.com/jonasschmidt/ex_json_schema)
240
241 ### Erlang
242
243 * [jesse](https://github.com/for-GET/jesse)
244
245 ### Go
246
247 * [gojsonschema](https://github.com/sigu-399/gojsonschema)
248 * [validate-json](https://github.com/cesanta/validate-json)
249
250 ### Haskell
251
252 * [aeson-schema](https://github.com/timjb/aeson-schema)
253 * [hjsonschema](https://github.com/seagreen/hjsonschema)
254
255 ### Java
256
257 * [json-schema-validation-comparison](https://www.creekservice.org/json-schema-validation-comparison/functional) (Comparison site for JVM-based validator implementations)
258 * [json-schema-validator](https://github.com/daveclayton/json-schema-validator)
259 * [everit-org/json-schema](https://github.com/everit-org/json-schema)
260 * [networknt/json-schema-validator](https://github.com/networknt/json-schema-validator)
261 * [Justify](https://github.com/leadpony/justify)
262 * [Snow](https://github.com/ssilverman/snowy-json)
263 * [jsonschemafriend](https://github.com/jimblackler/jsonschemafriend)
264 * [OpenAPI JSON Schema Generator](https://github.com/openapi-json-schema-tools/openapi-json-schema-generator)
265 * [M3](https://github.com/JulesGosnell/m3) (all drafts, pure Clojure — also usable from Kotlin, Scala, and JavaScript)
266
267 ### JavaScript
268
269 * [json-schema-benchmark](https://github.com/Muscula/json-schema-benchmark)
270 * [direct-schema](https://github.com/IreneKnapp/direct-schema)
271 * [is-my-json-valid](https://github.com/mafintosh/is-my-json-valid)
272 * [jassi](https://github.com/iclanzan/jassi)
273 * [JaySchema](https://github.com/natesilva/jayschema)
274 * [json-schema-valid](https://github.com/ericgj/json-schema-valid)
275 * [Jsonary](https://github.com/jsonary-js/jsonary)
276 * [jsonschema](https://github.com/tdegrunt/jsonschema)
277 * [request-validator](https://github.com/bugventure/request-validator)
278 * [skeemas](https://github.com/Prestaul/skeemas)
279 * [tv4](https://github.com/geraintluff/tv4)
280 * [z-schema](https://github.com/zaggino/z-schema)
281 * [jsen](https://github.com/bugventure/jsen)
282 * [ajv](https://github.com/epoberezkin/ajv)
283 * [djv](https://github.com/korzio/djv)
284 * [M3](https://github.com/JulesGosnell/m3) (all drafts, pure Clojure — also usable from Java, Kotlin, and Scala)
285 * [TypeBox](https://github.com/sinclairzx81/typebox)
286
287 ### Kotlin
288
289 * [json-schema-validation-comparison](https://www.creekservice.org/json-schema-validation-comparison/functional) (Comparison site for JVM-based validator implementations)
290 * [kotlinx-schema](https://github.com/Kotlin/kotlinx-schema)
291 * [M3](https://github.com/JulesGosnell/m3) (all drafts, pure Clojure — also usable from Java, Scala, and JavaScript)
292
293 ### Lua
294
295 * [lua-schema](https://framagit.org/fperrad/lua-schema)
296
297 ### Node.js
298
299 For node.js developers, the suite is also available as an [npm](https://www.npmjs.com/package/@json-schema-org/tests) package.
300
301 Node-specific support is maintained in a [separate repository](https://github.com/json-schema-org/json-schema-test-suite-npm) which also welcomes your contributions!
302
303 ### .NET
304
305 * [JsonSchema.Net](https://github.com/json-everything/json-everything)
306 * [Newtonsoft.Json.Schema](https://github.com/JamesNK/Newtonsoft.Json.Schema)
307
308 ### Perl
309
310 * [Test::JSON::Schema::Acceptance](https://github.com/karenetheridge/Test-JSON-Schema-Acceptance) (a wrapper of this test suite)
311 * [JSON::Schema::Modern](https://github.com/karenetheridge/JSON-Schema-Modern)
312 * [JSON::Schema::Tiny](https://github.com/karenetheridge/JSON-Schema-Tiny)
313
314 ### PHP
315
316 * [opis/json-schema](https://github.com/opis/json-schema)
317 * [json-schema](https://github.com/justinrainbow/json-schema)
318 * [json-guard](https://github.com/thephpleague/json-guard)
319
320 ### PostgreSQL
321
322 * [postgres-json-schema](https://github.com/gavinwahl/postgres-json-schema)
323 * [is\_jsonb\_valid](https://github.com/furstenheim/is_jsonb_valid)
324
325 ### Python
326
327 * [jsonschema](https://github.com/Julian/jsonschema)
328 * [fastjsonschema](https://github.com/seznam/python-fastjsonschema)
329 * [hypothesis-jsonschema](https://github.com/Zac-HD/hypothesis-jsonschema)
330 * [jschon](https://github.com/marksparkza/jschon)
331 * [OpenAPI JSON Schema Generator](https://github.com/openapi-json-schema-tools/openapi-json-schema-generator)
332
333 ### Ruby
334
335 * [json-schema](https://github.com/hoxworth/json-schema)
336 * [json\_schemer](https://github.com/davishmcclurg/json_schemer)
337
338 ### Rust
339
340 * [jsonschema](https://github.com/Stranger6667/jsonschema-rs)
341 * [valico](https://github.com/rustless/valico)
342
343 ### Scala
344
345 * [json-schema-validation-comparison](https://www.creekservice.org/json-schema-validation-comparison/functional) (Comparison site for JVM-based validator implementations)
346 * [M3](https://github.com/JulesGosnell/m3) (all drafts, pure Clojure — also usable from Java, Kotlin, and JavaScript)
347
348 ### Swift
349
350 * [JSONSchema](https://github.com/kylef/JSONSchema.swift)
351 * [swift-json-schema](https://github.com/ajevans99/swift-json-schema)
352
353 ### Unison
354
355 * [typed-json](https://share.unison-lang.org/@frawa/typed-json)
356
357 If you use it as well, please fork and send a pull request adding yourself to
358 the list :).
359
360 ## Contributing
361
362 If you see something missing or incorrect, a pull request is most welcome!
363
364 There are some sanity checks in place for testing the test suite. You can run
365 them with `bin/jsonschema_suite check` or `tox`. They will be run automatically
366 by [GitHub Actions](https://github.com/json-schema-org/JSON-Schema-Test-Suite/actions?query=workflow%3A%22Test+Suite+Sanity+Checking%22)
367 as well.
368
369 This repository is maintained by the JSON Schema organization, and will be governed by the JSON Schema steering committee (once it exists).