Skip to content

Commit 72b90f8

Browse files
authored
Support printing errors as JSON given --json (#524)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 2088595 commit 72b90f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+989
-187
lines changed

docs/bundle.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Bundling
55
jsonschema bundle <schema.json|.yaml>
66
[--http/-h] [--verbose/-v] [--resolve/-r <schemas-or-directories> ...]
77
[--extension/-e <extension>] [--ignore/-i <schemas-or-directories>]
8-
[--without-id/-w] [--default-dialect/-d <uri>]
8+
[--without-id/-w] [--default-dialect/-d <uri>] [--json/-j]
99
```
1010

1111
A schema may contain references to remote schemas outside the scope of the

docs/compile.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Compiling
88
jsonschema compile <schema.json|.yaml> [--http/-h] [--verbose/-v]
99
[--resolve/-r <schemas-or-directories> ...] [--extension/-e <extension>]
1010
[--ignore/-i <schemas-or-directories>] [--fast/-f] [--default-dialect/-d <uri>]
11-
[--minify/-m]
11+
[--minify/-m] [--json/-j]
1212
```
1313

1414
The `validate` command will first compile the schema into an optimised

docs/decode.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Decode
33

44
```sh
55
jsonschema decode <output.binpack> <output.json|.jsonl>
6-
[--default-dialect/-d <uri>]
6+
[--default-dialect/-d <uri>] [--json/-j]
77
```
88

99
This command decodes a JSON document using [JSON

docs/encode.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Encode
33

44
```sh
55
jsonschema encode <document.json|.jsonl> <output.binpack>
6-
[--default-dialect/-d <uri>]
6+
[--default-dialect/-d <uri>] [--json/-j]
77
```
88

99
This command encodes a JSON document using [JSON

docs/format.markdown

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ Formatting
44
```sh
55
jsonschema fmt [schemas-or-directories...]
66
[--check/-c] [--verbose/-v] [--resolve/-r <schemas-or-directories> ...]
7-
[--extension/-e <extension>] [--ignore/-i <schemas-or-directories>]
7+
[--extension/-e <extension>] [--ignore/-i <schemas-or-directories>]
88
[--keep-ordering/-k] [--indentation/-n <spaces>] [--default-dialect/-d <uri>]
9+
[--json/-j]
910
```
1011

1112
Schemas are code. As such, they are expected follow consistent stylistic

docs/test.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Testing
88
jsonschema test [schemas-or-directories...]
99
[--http/-h] [--verbose/-v] [--resolve/-r <schemas-or-directories> ...]
1010
[--extension/-e <extension>] [--ignore/-i <schemas-or-directories>]
11-
[--default-dialect/-d <uri>]
11+
[--default-dialect/-d <uri>] [--json/-j]
1212
```
1313

1414
Schemas are code. As such, you should run an automated unit testing suite

src/command_lint.cc

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -202,29 +202,34 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
202202

203203
auto copy = entry.second;
204204

205-
const auto wrapper_result = sourcemeta::jsonschema::try_catch([&]() {
206-
try {
207-
bundle.apply(
208-
copy, sourcemeta::core::schema_official_walker, custom_resolver,
209-
get_lint_callback(errors_array, entry, output_json), dialect,
210-
sourcemeta::core::URI::from_path(entry.first).recompose());
211-
return EXIT_SUCCESS;
212-
} catch (const sourcemeta::core::SchemaTransformRuleProcessedTwiceError
213-
&error) {
214-
throw LintAutoFixError{error.what(), entry.first, error.location()};
215-
} catch (const sourcemeta::core::SchemaBrokenReferenceError &error) {
216-
throw LintAutoFixError{
217-
"Could not autofix the schema without breaking its internal "
218-
"references",
219-
entry.first, error.location()};
220-
} catch (const sourcemeta::core::SchemaUnknownBaseDialectError &) {
221-
throw FileError<sourcemeta::core::SchemaUnknownBaseDialectError>(
222-
entry.first);
223-
} catch (const sourcemeta::core::SchemaResolutionError &error) {
224-
throw FileError<sourcemeta::core::SchemaResolutionError>(entry.first,
225-
error);
226-
}
227-
});
205+
const auto wrapper_result =
206+
sourcemeta::jsonschema::try_catch(options, [&]() {
207+
try {
208+
bundle.apply(
209+
copy, sourcemeta::core::schema_official_walker,
210+
custom_resolver,
211+
get_lint_callback(errors_array, entry, output_json), dialect,
212+
sourcemeta::core::URI::from_path(entry.first).recompose());
213+
return EXIT_SUCCESS;
214+
} catch (
215+
const sourcemeta::core::SchemaTransformRuleProcessedTwiceError
216+
&error) {
217+
throw LintAutoFixError{error.what(), entry.first,
218+
error.location()};
219+
} catch (
220+
const sourcemeta::core::SchemaBrokenReferenceError &error) {
221+
throw LintAutoFixError{
222+
"Could not autofix the schema without breaking its internal "
223+
"references",
224+
entry.first, error.location()};
225+
} catch (const sourcemeta::core::SchemaUnknownBaseDialectError &) {
226+
throw FileError<sourcemeta::core::SchemaUnknownBaseDialectError>(
227+
entry.first);
228+
} catch (const sourcemeta::core::SchemaResolutionError &error) {
229+
throw FileError<sourcemeta::core::SchemaResolutionError>(
230+
entry.first, error);
231+
}
232+
});
228233

229234
if (wrapper_result == EXIT_SUCCESS) {
230235
if (copy != entry.second) {
@@ -246,27 +251,28 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
246251
resolver(options, options.contains("http"), dialect, configuration)};
247252
LOG_VERBOSE(options) << "Linting: " << entry.first.string() << "\n";
248253

249-
const auto wrapper_result = sourcemeta::jsonschema::try_catch([&]() {
250-
try {
251-
const auto subresult = bundle.check(
252-
entry.second, sourcemeta::core::schema_official_walker,
253-
custom_resolver,
254-
get_lint_callback(errors_array, entry, output_json), dialect,
255-
sourcemeta::core::URI::from_path(entry.first).recompose());
256-
scores.emplace_back(subresult.second);
257-
if (subresult.first) {
258-
return EXIT_SUCCESS;
259-
} else {
260-
return EXIT_FAILURE;
261-
}
262-
} catch (const sourcemeta::core::SchemaUnknownBaseDialectError &) {
263-
throw FileError<sourcemeta::core::SchemaUnknownBaseDialectError>(
264-
entry.first);
265-
} catch (const sourcemeta::core::SchemaResolutionError &error) {
266-
throw FileError<sourcemeta::core::SchemaResolutionError>(entry.first,
267-
error);
268-
}
269-
});
254+
const auto wrapper_result =
255+
sourcemeta::jsonschema::try_catch(options, [&]() {
256+
try {
257+
const auto subresult = bundle.check(
258+
entry.second, sourcemeta::core::schema_official_walker,
259+
custom_resolver,
260+
get_lint_callback(errors_array, entry, output_json), dialect,
261+
sourcemeta::core::URI::from_path(entry.first).recompose());
262+
scores.emplace_back(subresult.second);
263+
if (subresult.first) {
264+
return EXIT_SUCCESS;
265+
} else {
266+
return EXIT_FAILURE;
267+
}
268+
} catch (const sourcemeta::core::SchemaUnknownBaseDialectError &) {
269+
throw FileError<sourcemeta::core::SchemaUnknownBaseDialectError>(
270+
entry.first);
271+
} catch (const sourcemeta::core::SchemaResolutionError &error) {
272+
throw FileError<sourcemeta::core::SchemaResolutionError>(
273+
entry.first, error);
274+
}
275+
});
270276

271277
if (wrapper_result != EXIT_SUCCESS) {
272278
result = false;

0 commit comments

Comments
 (0)