-
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.
Replace
--metaschema
from lint
/test
with a new metaschema
com…
…mand Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
- Loading branch information
Showing
19 changed files
with
234 additions
and
194 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,75 @@ | ||
Formatting | ||
========== | ||
|
||
```sh | ||
jsonschema metaschema [schemas-or-directories...] | ||
[--verbose/-v] [--http/-h] [--extension/-e <extension>] | ||
[--ignore/-i <schemas-or-directories>] | ||
``` | ||
|
||
Ensure that a schema or a set of schemas are considered valid with regards to | ||
their metaschemas. | ||
|
||
Examples | ||
-------- | ||
|
||
For example, consider this fictitious JSON Schema that follows the Draft 4 | ||
dialect but sets the `type` property to an invalid value: | ||
|
||
```json | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"type": 1 | ||
} | ||
``` | ||
|
||
Running the `metaschema` command on it will surface an error: | ||
|
||
```sh | ||
$ jsonschema metaschema schema.json | ||
error: The target document is expected to be one of the given values | ||
at instance location "/type" | ||
at evaluate path "/properties/type/anyOf/0/$ref/enum" | ||
error: Mark the current position of the evaluation process for future jumps | ||
at instance location "/type" | ||
at evaluate path "/properties/type/anyOf/0/$ref" | ||
error: The target document is expected to be of the given type | ||
at instance location "/type" | ||
at evaluate path "/properties/type/anyOf/1/type" | ||
error: The target is expected to match at least one of the given assertions | ||
at instance location "/type" | ||
at evaluate path "/properties/type/anyOf" | ||
error: The target is expected to match all of the given assertions | ||
at instance location "" | ||
at evaluate path "/properties" | ||
``` | ||
|
||
### Validate the metaschema of JSON Schemas in-place | ||
|
||
```sh | ||
jsonschema metaschema path/to/my/schema_1.json path/to/my/schema_2.json | ||
``` | ||
|
||
### Validate the metaschema of every `.json` file in a given directory (recursively) | ||
|
||
```sh | ||
jsonschema metaschema path/to/schemas/ | ||
``` | ||
|
||
### Validate the metaschema of every `.json` file in the current directory (recursively) | ||
|
||
```sh | ||
jsonschema metaschema | ||
``` | ||
|
||
### Validate the metaschema of every `.json` file in a given directory while ignoring another | ||
|
||
```sh | ||
jsonschema metaschema path/to/schemas/ --ignore path/to/schemas/nested | ||
``` | ||
|
||
### Validate the metaschema of every `.schema.json` file in the current directory (recursively) | ||
|
||
```sh | ||
jsonschema metaschema --extension .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
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,50 @@ | ||
#include <sourcemeta/jsontoolkit/json.h> | ||
#include <sourcemeta/jsontoolkit/jsonschema.h> | ||
|
||
#include <cstdlib> // EXIT_SUCCESS, EXIT_FAILURE | ||
#include <iostream> // std::cerr | ||
#include <set> // std::set | ||
#include <string> // std::string | ||
|
||
#include "command.h" | ||
#include "utils.h" | ||
|
||
// TODO: Add a flag to emit output using the standard JSON Schema output format | ||
auto intelligence::jsonschema::cli::metaschema( | ||
const std::span<const std::string> &arguments) -> int { | ||
const auto options{parse_options(arguments, {"h", "http"})}; | ||
const auto custom_resolver{ | ||
resolver(options, options.contains("h") || options.contains("http"))}; | ||
bool result{true}; | ||
|
||
for (const auto &entry : for_each_json(options.at(""), parse_ignore(options), | ||
parse_extensions(options))) { | ||
if (!sourcemeta::jsontoolkit::is_schema(entry.second)) { | ||
std::cerr << "Not a schema: " << entry.first.string() << "\n"; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
// TODO: Cache this somehow for performance reasons? | ||
const auto metaschema_template{sourcemeta::jsontoolkit::compile( | ||
sourcemeta::jsontoolkit::metaschema(entry.second, custom_resolver), | ||
sourcemeta::jsontoolkit::default_schema_walker, custom_resolver, | ||
sourcemeta::jsontoolkit::default_schema_compiler)}; | ||
|
||
std::ostringstream error; | ||
if (sourcemeta::jsontoolkit::evaluate( | ||
metaschema_template, entry.second, | ||
sourcemeta::jsontoolkit::SchemaCompilerEvaluationMode::Fast, | ||
pretty_evaluate_callback(error))) { | ||
log_verbose(options) | ||
<< entry.first.string() | ||
<< ": The schema is valid with respect to its metaschema\n"; | ||
} else { | ||
std::cerr << error.str(); | ||
std::cerr << entry.first.string() | ||
<< ": The schema is NOT valid with respect to its metaschema\n"; | ||
result = false; | ||
} | ||
} | ||
|
||
return result ? EXIT_SUCCESS : EXIT_FAILURE; | ||
} |
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
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
Oops, something went wrong.