Skip to content

Commit

Permalink
Consume built-in lint rules from JSON Toolkit
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed May 31, 2024
1 parent 0e4a455 commit 457eff7
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 43 deletions.
2 changes: 1 addition & 1 deletion DEPENDENCIES
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
vendorpull https://github.com/sourcemeta/vendorpull dea311b5bfb53b6926a4140267959ae334d3ecf4
noa https://github.com/sourcemeta/noa 5ff4024902642afc9cc2f9a9e02ae9dff9d15d4f
jsontoolkit https://github.com/sourcemeta/jsontoolkit 503146c9412c040bcbcdb9a09a6da42c300d3435
jsontoolkit https://github.com/sourcemeta/jsontoolkit 72dde22434cfa827201dbde10af976c82bd99a43
hydra https://github.com/sourcemeta/hydra d5e0c314dae88b0bf2ac4eeff2c7395910e2c7e9
3 changes: 1 addition & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ add_executable(jsonschema_cli
command_bundle.cc
command_test.cc
command_lint.cc
command_validate.cc
lint/enum_with_type.h)
command_validate.cc)
intelligence_jsonschema_add_compile_options(jsonschema_cli)
set_target_properties(jsonschema_cli PROPERTIES OUTPUT_NAME jsonschema)
target_link_libraries(jsonschema_cli PRIVATE sourcemeta::jsontoolkit::uri)
Expand Down
13 changes: 8 additions & 5 deletions src/command_lint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
#include "command.h"
#include "utils.h"

#include "lint/enum_with_type.h"

// TODO: Implement a --fix flag
auto intelligence::jsonschema::cli::lint(
const std::span<const std::string> &arguments) -> int {
const auto options{parse_options(arguments, {})};

sourcemeta::jsontoolkit::SchemaTransformBundle bundle;
bundle.add<EnumWithType>();
bool result{true};
bundle.add(
sourcemeta::jsontoolkit::SchemaTransformBundle::Category::Modernize);
bundle.add(
sourcemeta::jsontoolkit::SchemaTransformBundle::Category::AntiPattern);

bool result{true};
for (const auto &entry : for_each_json(options.at(""))) {
const bool subresult = bundle.check(
entry.second, sourcemeta::jsontoolkit::default_schema_walker,
Expand All @@ -29,7 +30,9 @@ auto intelligence::jsonschema::cli::lint(
std::cout << " " << message << " (" << name << ")\n";
});

result = result || subresult;
if (!subresult) {
result = false;
}
}

return result ? EXIT_SUCCESS : EXIT_FAILURE;
Expand Down
35 changes: 0 additions & 35 deletions src/lint/enum_with_type.h

This file was deleted.

2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ add_jsonschema_test_unix(bundle_remote_single_schema)
add_jsonschema_test_unix(test_single_pass)
add_jsonschema_test_unix(test_single_fail)
add_jsonschema_test_unix(test_single_unsupported)
add_jsonschema_test_unix(lint_pass)
add_jsonschema_test_unix(lint_fail)
28 changes: 28 additions & 0 deletions test/lint_fail.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/sh

set -o errexit
set -o nounset

TMP="$(mktemp -d)"
# shellcheck disable=SC2317
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" && CODE="$?" || CODE="$?"

if [ "$CODE" = "0" ]
then
echo "FAIL" 1>&2
exit 1
else
echo "PASS" 1>&2
exit 0
fi
18 changes: 18 additions & 0 deletions test/lint_pass.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh

set -o errexit
set -o nounset

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

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

"$1" lint "$TMP/schema.json"
3 changes: 3 additions & 0 deletions vendor/jsontoolkit/src/jsonschema/CMakeLists.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions vendor/jsontoolkit/src/jsonschema/rules/const_with_type.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions vendor/jsontoolkit/src/jsonschema/rules/enum_to_const.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions vendor/jsontoolkit/src/jsonschema/rules/enum_with_type.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions vendor/jsontoolkit/src/jsonschema/transform_bundle.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 457eff7

Please sign in to comment.