-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a
compile
command to separately preprocess schemas
Signed-off-by: Juan Cruz Viotti <[email protected]>
- Loading branch information
Showing
6 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
Compile | ||
======= | ||
|
||
> [!WARNING] | ||
> Only JSON Schema Draft 4, Draft 6, and Draft 7 are supported at this point in | ||
> time. We have plans to support *every* dialect of JSON Schema from Draft 0 to | ||
> Draft 2020-12 soon. | ||
```sh | ||
jsonschema compile <schema.json> | ||
``` | ||
|
||
Evaluating a JSON Schema is a complex process with a non-trivial associated | ||
performance overhead. For faster evaluation, we internally pre-process a given | ||
JSON Schema into a compiled low-level form. This command allows you to obtain | ||
the schema compilation result as a separate step. | ||
|
||
**At least for now, this command is mainly for internal debugging purposes. As | ||
an end-user, you are better served by the [`validate`](./validate.markdown) | ||
command, which internally compiles schemas.** | ||
|
||
Examples | ||
-------- | ||
|
||
For example, consider the following simple schema: | ||
|
||
```json | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"properties": { | ||
"foo": { "type": "string" } | ||
} | ||
} | ||
``` | ||
|
||
The compilation process will result in something like this: | ||
|
||
``` | ||
TODO | ||
``` | ||
|
||
### Compile a JSON Schema | ||
|
||
```sh | ||
jsonschema compile path/to/my/schema.json | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <sourcemeta/jsontoolkit/json.h> | ||
#include <sourcemeta/jsontoolkit/jsonschema.h> | ||
|
||
#include <cstdlib> // EXIT_SUCCESS | ||
#include <iostream> // std::cout, std::endl | ||
|
||
#include "command.h" | ||
#include "utils.h" | ||
|
||
auto intelligence::jsonschema::cli::compile( | ||
const std::span<const std::string> &arguments) -> int { | ||
const auto options{parse_options(arguments, {})}; | ||
|
||
if (options.at("").size() < 1) { | ||
std::cerr | ||
<< "error: This command expects a path to a schema. For example:\n\n" | ||
<< " jsonschema compile path/to/schema.json\n"; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
const auto schema{sourcemeta::jsontoolkit::from_file(options.at("").front())}; | ||
|
||
const auto compiled_schema{sourcemeta::jsontoolkit::compile( | ||
schema, sourcemeta::jsontoolkit::default_schema_walker, | ||
resolver(options, options.contains("h") || options.contains("http")), | ||
sourcemeta::jsontoolkit::default_schema_compiler)}; | ||
|
||
const sourcemeta::jsontoolkit::JSON result{ | ||
sourcemeta::jsontoolkit::to_json(compiled_schema)}; | ||
sourcemeta::jsontoolkit::prettify(result, std::cout); | ||
std::cout << std::endl; | ||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters