Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --json to lint command #228

Merged
merged 14 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/lint.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Linting

```sh
jsonschema lint [schemas-or-directories...]
[--fix/-f] [--verbose/-v] [--extension/-e <extension>]
[--fix/-f] [--json/-j] [--verbose/-v] [--extension/-e <extension>]
[--ignore/-i <schemas-or-directories>]
```

Expand Down Expand Up @@ -61,6 +61,11 @@ result in the JSON Schema CLI *automatically* fixing the warning for you.
```sh
jsonschema lint path/to/my/schema_1.json path/to/my/schema_2.json
```
### Lint with JSON output

```sh
jsonschema lint path/to/my/schema.json --json
```

### Lint every `.json` file in a given directory (recursively)

Expand Down
56 changes: 39 additions & 17 deletions src/command_lint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
#include <sourcemeta/core/jsonpointer.h>
#include <sourcemeta/core/jsonschema.h>

#include <cstdlib> // EXIT_SUCCESS
#include <cstdlib> // EXIT_SUCCESS, EXIT_FAILURE
#include <fstream> // std::ofstream
#include <iostream> // std::cerr, std::cout
#include <sstream> // std::ostringstream

#include "command.h"
#include "utils.h"

auto sourcemeta::jsonschema::cli::lint(
const std::span<const std::string> &arguments) -> int {
const auto options{parse_options(arguments, {"f", "fix"})};
const auto options{parse_options(arguments, {"f", "fix", "json", "j"})};
const bool output_json = options.contains("json") || options.contains("j");

sourcemeta::core::SchemaTransformer bundle;
sourcemeta::core::add(bundle,
Expand All @@ -27,6 +29,7 @@ auto sourcemeta::jsonschema::cli::lint(
sourcemeta::core::AlterSchemaCategory::SyntaxSugar);

bool result{true};
auto errors_array = sourcemeta::core::JSON::make_array();

if (options.contains("f") || options.contains("fix")) {
for (const auto &entry :
Expand All @@ -47,29 +50,48 @@ auto sourcemeta::jsonschema::cli::lint(
sourcemeta::core::schema_format_compare);
output << "\n";
}
} else {
for (const auto &entry :
for_each_json(options.at(""), parse_ignore(options),
parse_extensions(options))) {
log_verbose(options) << "Linting: " << entry.first.string() << "\n";
const bool subresult = bundle.check(
entry.second, sourcemeta::core::schema_official_walker,
resolver(options),
[&entry](const auto &pointer, const auto &name, const auto &message) {
}
for (const auto &entry : for_each_json(options.at(""), parse_ignore(options),
parse_extensions(options))) {
log_verbose(options) << "Linting: " << entry.first.string() << "\n";
const bool subresult = bundle.check(
entry.second, sourcemeta::core::schema_official_walker,
resolver(options),
[&](const auto &pointer, const auto &name, const auto &message) {
if (output_json) {
auto error_obj = sourcemeta::core::JSON::make_object();

error_obj.assign("path",
sourcemeta::core::JSON{entry.first.string()});
error_obj.assign("id", sourcemeta::core::JSON{name});
error_obj.assign("message", sourcemeta::core::JSON{message});

std::ostringstream pointer_stream;
sourcemeta::core::stringify(pointer, pointer_stream);
error_obj.assign("schemaLocation",
sourcemeta::core::JSON{pointer_stream.str()});

errors_array.push_back(error_obj);
} else {
std::cout << entry.first.string() << ":\n";
std::cout << " " << message << " (" << name << ")\n";
std::cout << " at schema location \"";
sourcemeta::core::stringify(pointer, std::cout);
std::cout << "\"\n";
});
}
});

if (subresult) {
log_verbose(options) << "PASS: " << entry.first.string() << "\n";
} else {
result = false;
}
if (!subresult) {
result = false;
}
}
if (output_json) {
auto output_json_object = sourcemeta::core::JSON::make_object();
output_json_object.assign("valid", sourcemeta::core::JSON{result});
output_json_object.assign("errors", sourcemeta::core::JSON{errors_array});
sourcemeta::core::prettify(output_json_object, std::cout);
std::cout << "\n";
}

return result ? EXIT_SUCCESS : EXIT_FAILURE;
}
3 changes: 2 additions & 1 deletion src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ Global Options:
Format the input schemas in-place or check they are formatted.
This command does not support YAML schemas yet.

lint [schemas-or-directories...] [--fix/-f] [--extension/-e <extension>]
lint [schemas-or-directories...] [--fix/-f] [--json/-j] [--extension/-e <extension>]
[--ignore/-i <schemas-or-directories>]

Lint the input schemas and potentially fix the reported issues.
The --fix/-f option is not supported when passing YAML schemas.
Use --json/-j to output lint errors in JSON.

bundle <schema.json|.yaml> [--http/-h] [--extension/-e <extension>]
[--ignore/-i <schemas-or-directories>] [--without-id/-w]
Expand Down
3 changes: 3 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ add_jsonschema_test_unix(lint/fail_lint)
add_jsonschema_test_unix(lint/fail_lint_yaml)
add_jsonschema_test_unix(lint/fail_lint_fix_yaml)
add_jsonschema_test_unix(lint/fail_lint_fix_yml)
add_jsonschema_test_unix(lint/fail_lint_json)
add_jsonschema_test_unix(lint/pass_lint_json)
add_jsonschema_test_unix(lint/pass_lint_fix_json)
add_jsonschema_test_unix(lint/fail_bigint)

# Encode
Expand Down
35 changes: 35 additions & 0 deletions test/lint/fail_lint_json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/sh

set -o errexit
set -o nounset

TMP="$(mktemp -d)"
clean() { rm -rf "$TMP"; }
trap clean EXIT

cat << 'EOF' > "$TMP/schema.json"
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "string",
"enum": [ "foo" ]
}
EOF

"$1" lint "$TMP/schema.json" --json >"$TMP/output.json" 2>&1 && CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.json"
{
"valid": false,
"errors": [
{
"path": "$(realpath "$TMP")/schema.json",
"id": "enum_with_type",
"message": "Setting \`type\` alongside \`enum\` is considered an anti-pattern, as the enumeration choices already imply their respective types",
"schemaLocation": ""
}
]
}
EOF

diff "$TMP/output.json" "$TMP/expected.json"
36 changes: 36 additions & 0 deletions test/lint/pass_lint_fix_json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/sh

set -o errexit
set -o nounset

TMP="$(mktemp -d)"
clean() { rm -rf "$TMP"; }
trap clean EXIT

cat << 'EOF' > "$TMP/schema.json"
{
"$schema": "http://json-schema.org/draft-06/schema#",
"type": "string",
"enum": [ "foo" ]
}
EOF

"$1" lint "$TMP/schema.json" --fix --json >"$TMP/output.json" 2>&1

cat << EOF > "$TMP/expected.json"
{
"valid": true,
"errors": []
}
EOF

diff "$TMP/output.json" "$TMP/expected.json"

cat << 'EOF' > "$TMP/expected_fixed.json"
{
"$schema": "http://json-schema.org/draft-06/schema#",
"const": "foo"
}
EOF

diff "$TMP/schema.json" "$TMP/expected_fixed.json"
26 changes: 26 additions & 0 deletions test/lint/pass_lint_json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh

set -o errexit
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you leave a blank line after the hash bang, like other scripts? Same in the other ones you added here

set -o nounset

TMP="$(mktemp -d)"
clean() { rm -rf "$TMP"; }
trap clean EXIT

cat << 'EOF' > "$TMP/schema.json"
{
"$schema": "http://json-schema.org/draft-04/schema#",
"enum": [ "foo" ]
}
EOF

"$1" lint "$TMP/schema.json" --json >"$TMP/output.json" 2>&1

cat << EOF > "$TMP/expected.json"
{
"valid": true,
"errors": []
}
EOF

diff "$TMP/output.json" "$TMP/expected.json"