Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 49 additions & 1 deletion src/command_lint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,54 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
}

if (output_json) {
std::vector<std::size_t> indices(errors_array.size());
for (std::size_t i = 0; i < errors_array.size(); ++i) {
indices[i] = i;
}

std::sort(indices.begin(), indices.end(),
[&errors_array](const std::size_t left_idx,
const std::size_t right_idx) -> bool {
const auto &left = errors_array.at(left_idx);
const auto &right = errors_array.at(right_idx);

const auto &left_path = left.at("path");
const auto &right_path = right.at("path");
if (left_path.to_string() != right_path.to_string()) {
return left_path.to_string() < right_path.to_string();
}

const auto &left_pos = left.at("position");
const auto &right_pos = right.at("position");

const bool left_is_null = left_pos.is_null();
const bool right_is_null = right_pos.is_null();
if (left_is_null && right_is_null) {
return false;
}
if (left_is_null) {
return false;
}
if (right_is_null) {
return true;
}

const auto left_line = left_pos.at(0).to_integer();
const auto right_line = right_pos.at(0).to_integer();
if (left_line != right_line) {
return left_line < right_line;
}

const auto left_col = left_pos.at(1).to_integer();
const auto right_col = right_pos.at(1).to_integer();
return left_col < right_col;
});

auto sorted_errors = sourcemeta::core::JSON::make_array();
for (const auto idx : indices) {
sorted_errors.push_back(errors_array.at(idx));
}

auto output_json_object = sourcemeta::core::JSON::make_object();
output_json_object.assign("valid", sourcemeta::core::JSON{result});

Expand All @@ -308,7 +356,7 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
"health", sourcemeta::core::JSON{static_cast<std::size_t>(health)});
}

output_json_object.assign("errors", sourcemeta::core::JSON{errors_array});
output_json_object.assign("errors", sourcemeta::core::JSON{sorted_errors});
sourcemeta::core::prettify(output_json_object, std::cout, indentation);
std::cout << "\n";
}
Expand Down
8 changes: 4 additions & 4 deletions test/lint/fail_lint_directory_json.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ cat << EOF > "$TMP/expected.json"
"id": "enum_with_type",
"message": "Setting \`type\` alongside \`enum\` is considered an anti-pattern, as the enumeration choices already imply their respective types",
"description": null,
"schemaLocation": "/enum",
"position": [ 7, 3, 7, 33 ]
"schemaLocation": "/type",
"position": [ 6, 3, 6, 18 ]
},
{
"path": "$(realpath "$TMP")/schemas/foo.json",
"id": "enum_with_type",
"message": "Setting \`type\` alongside \`enum\` is considered an anti-pattern, as the enumeration choices already imply their respective types",
"description": null,
"schemaLocation": "/type",
"position": [ 6, 3, 6, 18 ]
"schemaLocation": "/enum",
"position": [ 7, 3, 7, 33 ]
}
]
}
Expand Down
8 changes: 4 additions & 4 deletions test/lint/fail_lint_json.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ cat << EOF > "$TMP/expected.json"
"id": "enum_with_type",
"message": "Setting \`type\` alongside \`enum\` is considered an anti-pattern, as the enumeration choices already imply their respective types",
"description": null,
"schemaLocation": "/enum",
"position": [ 6, 3, 6, 19 ]
"schemaLocation": "/type",
"position": [ 5, 3, 5, 18 ]
},
{
"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",
"description": null,
"schemaLocation": "/type",
"position": [ 5, 3, 5, 18 ]
"schemaLocation": "/enum",
"position": [ 6, 3, 6, 19 ]
}
]
}
Expand Down