Skip to content

Commit 3395194

Browse files
committed
Support disabling lint rules by name with a --disable/-d option
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 58e5633 commit 3395194

File tree

14 files changed

+214
-18
lines changed

14 files changed

+214
-18
lines changed

DEPENDENCIES

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
vendorpull https://github.com/sourcemeta/vendorpull dea311b5bfb53b6926a4140267959ae334d3ecf4
2-
core https://github.com/sourcemeta/core 64063dc926a074dd9b7add8478d891dd39a8fc9f
2+
core https://github.com/sourcemeta/core 639d617d927709d8a58a7c88fa2aaeee5ded0f7f
33
hydra https://github.com/sourcemeta/hydra a67b879df800e834ed8a2d056f98398f7211d870
44
jsonbinpack https://github.com/sourcemeta/jsonbinpack c7bb7f4d903c3b6925b62ce2bb5b8d360e01ce84
55
blaze https://github.com/sourcemeta/blaze a453075f2013ea8fc27d0dded1da6521dbc8474e

docs/lint.markdown

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Linting
44
```sh
55
jsonschema lint [schemas-or-directories...]
66
[--fix/-f] [--json/-j] [--verbose/-v] [--extension/-e <extension>]
7-
[--ignore/-i <schemas-or-directories>]
7+
[--ignore/-i <schemas-or-directories>] [--disable/-d <rule-name>]
88
```
99

1010
JSON Schema is a surprisingly expressive schema language. Like with traditional
@@ -61,6 +61,13 @@ result in the JSON Schema CLI *automatically* fixing the warning for you.
6161
```sh
6262
jsonschema lint path/to/my/schema_1.json path/to/my/schema_2.json
6363
```
64+
65+
### Lint while disabling certain rules
66+
67+
```sh
68+
jsonschema lint path/to/my/schema.json --disable enum_with_type --disable const_with_type
69+
```
70+
6471
### Lint with JSON output
6572

6673
```sh

src/command_lint.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@
1111
#include "command.h"
1212
#include "utils.h"
1313

14+
template <typename Options, typename Iterator>
15+
static auto disable_lint_rules(sourcemeta::core::SchemaTransformer &bundle,
16+
const Options &options, Iterator first,
17+
Iterator last) -> void {
18+
for (auto iterator = first; iterator != last; ++iterator) {
19+
if (bundle.remove(*iterator)) {
20+
sourcemeta::jsonschema::cli::log_verbose(options)
21+
<< "Disabling rule: " << *iterator << "\n";
22+
} else {
23+
sourcemeta::jsonschema::cli::log_verbose(options)
24+
<< "warning: Cannot disable unknown rule: " << *iterator << "\n";
25+
}
26+
}
27+
}
28+
1429
auto sourcemeta::jsonschema::cli::lint(
1530
const std::span<const std::string> &arguments) -> int {
1631
const auto options{parse_options(arguments, {"f", "fix", "json", "j"})};
@@ -28,6 +43,16 @@ auto sourcemeta::jsonschema::cli::lint(
2843
sourcemeta::core::add(bundle,
2944
sourcemeta::core::AlterSchemaCategory::SyntaxSugar);
3045

46+
if (options.contains("disable")) {
47+
disable_lint_rules(bundle, options, options.at("disable").cbegin(),
48+
options.at("disable").cend());
49+
}
50+
51+
if (options.contains("d")) {
52+
disable_lint_rules(bundle, options, options.at("d").cbegin(),
53+
options.at("d").cend());
54+
}
55+
3156
bool result{true};
3257
auto errors_array = sourcemeta::core::JSON::make_array();
3358

src/main.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Global Options:
5353
5454
lint [schemas-or-directories...] [--fix/-f] [--json/-j]
5555
[--extension/-e <extension>] [--ignore/-i <schemas-or-directories>]
56+
[--disable/-d <rule-name>]
5657
5758
Lint the input schemas and potentially fix the reported issues.
5859
The --fix/-f option is not supported when passing YAML schemas.

test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ add_jsonschema_test_unix(lint/fail_lint_json)
179179
add_jsonschema_test_unix(lint/pass_lint_json)
180180
add_jsonschema_test_unix(lint/pass_lint_fix_json)
181181
add_jsonschema_test_unix(lint/fail_bigint)
182+
add_jsonschema_test_unix(lint/fail_lint_disable_one)
183+
add_jsonschema_test_unix(lint/fail_lint_disable_one_verbose)
184+
add_jsonschema_test_unix(lint/fail_lint_disable_unknown_verbose)
185+
add_jsonschema_test_unix(lint/fail_lint_disable_many)
182186

183187
# Encode
184188
add_jsonschema_test_unix(encode/pass_schema_less)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/sh
2+
3+
set -o errexit
4+
set -o nounset
5+
6+
TMP="$(mktemp -d)"
7+
clean() { rm -rf "$TMP"; }
8+
trap clean EXIT
9+
10+
cat << 'EOF' > "$TMP/schema.json"
11+
{
12+
"$schema": "https://json-schema.org/draft/2020-12/schema",
13+
"type": "string",
14+
"contentMediaType": "application/json",
15+
"enum": [ "foo" ]
16+
}
17+
EOF
18+
19+
"$1" lint "$TMP/schema.json" --disable enum_to_const -d enum_with_type >"$TMP/stderr.txt" 2>&1 && CODE="$?" || CODE="$?"
20+
test "$CODE" = "1" || exit 1
21+
22+
cat << EOF > "$TMP/expected.txt"
23+
$(realpath "$TMP")/schema.json:
24+
The \`contentMediaType\` keyword is meaningless without the presence of the \`contentEncoding\` keyword (content_media_type_without_encoding)
25+
at schema location ""
26+
EOF
27+
28+
diff "$TMP/stderr.txt" "$TMP/expected.txt"

test/lint/fail_lint_disable_one.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/sh
2+
3+
set -o errexit
4+
set -o nounset
5+
6+
TMP="$(mktemp -d)"
7+
clean() { rm -rf "$TMP"; }
8+
trap clean EXIT
9+
10+
cat << 'EOF' > "$TMP/schema.json"
11+
{
12+
"$schema": "https://json-schema.org/draft/2020-12/schema",
13+
"type": "string",
14+
"contentMediaType": "application/json",
15+
"enum": [ "foo" ]
16+
}
17+
EOF
18+
19+
"$1" lint "$TMP/schema.json" --disable enum_to_const >"$TMP/stderr.txt" 2>&1 && CODE="$?" || CODE="$?"
20+
test "$CODE" = "1" || exit 1
21+
22+
cat << EOF > "$TMP/expected.txt"
23+
$(realpath "$TMP")/schema.json:
24+
The \`contentMediaType\` keyword is meaningless without the presence of the \`contentEncoding\` keyword (content_media_type_without_encoding)
25+
at schema location ""
26+
$(realpath "$TMP")/schema.json:
27+
Setting \`type\` alongside \`enum\` is considered an anti-pattern, as the enumeration choices already imply their respective types (enum_with_type)
28+
at schema location ""
29+
EOF
30+
31+
diff "$TMP/stderr.txt" "$TMP/expected.txt"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
3+
set -o errexit
4+
set -o nounset
5+
6+
TMP="$(mktemp -d)"
7+
clean() { rm -rf "$TMP"; }
8+
trap clean EXIT
9+
10+
cat << 'EOF' > "$TMP/schema.json"
11+
{
12+
"$schema": "https://json-schema.org/draft/2020-12/schema",
13+
"type": "string",
14+
"contentMediaType": "application/json",
15+
"enum": [ "foo" ]
16+
}
17+
EOF
18+
19+
"$1" lint "$TMP/schema.json" --verbose --disable enum_to_const >"$TMP/stderr.txt" 2>&1 && CODE="$?" || CODE="$?"
20+
test "$CODE" = "1" || exit 1
21+
22+
cat << EOF > "$TMP/expected.txt"
23+
Disabling rule: enum_to_const
24+
Linting: $(realpath "$TMP")/schema.json
25+
$(realpath "$TMP")/schema.json:
26+
The \`contentMediaType\` keyword is meaningless without the presence of the \`contentEncoding\` keyword (content_media_type_without_encoding)
27+
at schema location ""
28+
$(realpath "$TMP")/schema.json:
29+
Setting \`type\` alongside \`enum\` is considered an anti-pattern, as the enumeration choices already imply their respective types (enum_with_type)
30+
at schema location ""
31+
EOF
32+
33+
diff "$TMP/stderr.txt" "$TMP/expected.txt"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/sh
2+
3+
set -o errexit
4+
set -o nounset
5+
6+
TMP="$(mktemp -d)"
7+
clean() { rm -rf "$TMP"; }
8+
trap clean EXIT
9+
10+
cat << 'EOF' > "$TMP/schema.json"
11+
{
12+
"$schema": "https://json-schema.org/draft/2020-12/schema",
13+
"type": "string",
14+
"contentMediaType": "application/json",
15+
"enum": [ "foo" ]
16+
}
17+
EOF
18+
19+
"$1" lint "$TMP/schema.json" --verbose --disable foo_bar >"$TMP/stderr.txt" 2>&1 && CODE="$?" || CODE="$?"
20+
test "$CODE" = "1" || exit 1
21+
22+
cat << EOF > "$TMP/expected.txt"
23+
warning: Cannot disable unknown rule: foo_bar
24+
Linting: $(realpath "$TMP")/schema.json
25+
$(realpath "$TMP")/schema.json:
26+
The \`contentMediaType\` keyword is meaningless without the presence of the \`contentEncoding\` keyword (content_media_type_without_encoding)
27+
at schema location ""
28+
$(realpath "$TMP")/schema.json:
29+
An \`enum\` of a single value can be expressed as \`const\` (enum_to_const)
30+
at schema location ""
31+
$(realpath "$TMP")/schema.json:
32+
Setting \`type\` alongside \`enum\` is considered an anti-pattern, as the enumeration choices already imply their respective types (enum_with_type)
33+
at schema location ""
34+
EOF
35+
36+
diff "$TMP/stderr.txt" "$TMP/expected.txt"

vendor/core/cmake/common/defaults.cmake

Lines changed: 28 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)