Skip to content
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: 7 additions & 0 deletions docs/format.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Formatting
jsonschema fmt [schemas-or-directories...]
[--check/-c] [--verbose/-v] [--extension/-e <extension>]
[--ignore/-i <schemas-or-directories>] [--keep-ordering/-k]
[--indentation/-n <spaces>]
```

Schemas are code. As such, they are expected follow consistent stylistic
Expand Down Expand Up @@ -54,6 +55,12 @@ jsonschema fmt path/to/my/schema_1.json path/to/my/schema_2.json
jsonschema fmt path/to/my/schema_1.json path/to/my/schema_2.json --keep-ordering
```

### Format JSON Schemas in-place while indenting on 4 spaces

```sh
jsonschema fmt path/to/my/schema_1.json path/to/my/schema_2.json --indentation 4
```

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

```sh
Expand Down
8 changes: 7 additions & 1 deletion docs/lint.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jsonschema lint [schemas-or-directories...] [--http/-h] [--fix/-f]
[--json/-j] [--verbose/-v] [--resolve/-r <schemas-or-directories> ...]
[--extension/-e <extension>] [--ignore/-i <schemas-or-directories>]
[--exclude/-x <rule-name>] [--only/-o <rule-name>] [--list/-l]
[--default-dialect/-d <uri>] [--strict/-s]
[--default-dialect/-d <uri>] [--strict/-s] [--indentation/-n <spaces>]
```

JSON Schema is a surprisingly expressive schema language. Like with traditional
Expand Down Expand Up @@ -133,6 +133,12 @@ jsonschema lint --extension .schema.json
jsonschema lint path/to/my/schema.json --fix
```

### Fix lint warnings on a single schema while indenting on 4 spaces

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

### Fix lint warnings on a single schema while preserving keyword ordering

```sh
Expand Down
11 changes: 7 additions & 4 deletions src/command_fmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

auto sourcemeta::jsonschema::cli::fmt(const sourcemeta::core::Options &options)
-> int {
const auto indentation{parse_indentation(options)};
for (const auto &entry :
for_each_json(options.positional(), parse_ignore(options),
parse_extensions(options))) {
Expand All @@ -28,10 +29,11 @@ auto sourcemeta::jsonschema::cli::fmt(const sourcemeta::core::Options &options)
std::ostringstream expected;

if (options.contains("keep-ordering")) {
sourcemeta::core::prettify(entry.second, expected);
sourcemeta::core::prettify(entry.second, expected, indentation);
} else {
sourcemeta::core::prettify(entry.second, expected,
sourcemeta::core::schema_format_compare);
sourcemeta::core::schema_format_compare,
indentation);
}

expected << "\n";
Expand All @@ -50,10 +52,11 @@ auto sourcemeta::jsonschema::cli::fmt(const sourcemeta::core::Options &options)
std::ofstream output{entry.first};

if (options.contains("keep-ordering")) {
sourcemeta::core::prettify(entry.second, output);
sourcemeta::core::prettify(entry.second, output, indentation);
} else {
sourcemeta::core::prettify(entry.second, output,
sourcemeta::core::schema_format_compare);
sourcemeta::core::schema_format_compare,
indentation);
}

output << "\n";
Expand Down
5 changes: 3 additions & 2 deletions src/command_lint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ auto sourcemeta::jsonschema::cli::lint(const sourcemeta::core::Options &options)
auto errors_array = sourcemeta::core::JSON::make_array();
std::vector<std::uint8_t> scores;
const auto dialect{default_dialect(options)};
const auto indentation{parse_indentation(options)};
const auto custom_resolver{
resolver(options, options.contains("http"), dialect)};

Expand Down Expand Up @@ -243,7 +244,7 @@ auto sourcemeta::jsonschema::cli::lint(const sourcemeta::core::Options &options)
if (wrapper_result == EXIT_SUCCESS) {
if (copy != entry.second) {
std::ofstream output{entry.first};
sourcemeta::core::prettify(copy, output);
sourcemeta::core::prettify(copy, output, indentation);
output << "\n";
}
} else {
Expand Down Expand Up @@ -298,7 +299,7 @@ auto sourcemeta::jsonschema::cli::lint(const sourcemeta::core::Options &options)
}

output_json_object.assign("errors", sourcemeta::core::JSON{errors_array});
sourcemeta::core::prettify(output_json_object, std::cout);
sourcemeta::core::prettify(output_json_object, std::cout, indentation);
std::cout << "\n";
}

Expand Down
6 changes: 5 additions & 1 deletion src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,22 @@ Global Options:

fmt [schemas-or-directories...] [--check/-c] [--extension/-e <extension>]
[--ignore/-i <schemas-or-directories>] [--keep-ordering/-k]
[--indentation/-n <spaces>]

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] [--json/-j]
[--extension/-e <extension>] [--ignore/-i <schemas-or-directories>]
[--exclude/-x <rule-name>] [--only/-o <rule-name>] [--list/-l]
[--strict/-s]
[--strict/-s] [--indentation/-n <spaces>]

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.
Use --list/-l to print a summary of all enabled rules.
Use --strict/-s to enable additional opinionated strict rules.
Use --indentation/-n to keep indentation when auto-fixing

bundle <schema.json|.yaml> [--http/-h] [--extension/-e <extension>]
[--ignore/-i <schemas-or-directories>] [--without-id/-w]
Expand Down Expand Up @@ -120,6 +122,7 @@ auto jsonschema_main(const std::string &program, const std::string &command,
app.flag("keep-ordering", {"k"});
app.option("extension", {"e"});
app.option("ignore", {"i"});
app.option("indentation", {"n"});
app.parse(argc, argv, {.skip = 1});
return sourcemeta::jsonschema::cli::fmt(app);
} else if (command == "inspect") {
Expand All @@ -141,6 +144,7 @@ auto jsonschema_main(const std::string &program, const std::string &command,
app.option("exclude", {"x"});
app.option("only", {"o"});
app.option("ignore", {"i"});
app.option("indentation", {"n"});
app.parse(argc, argv, {.skip = 1});
return sourcemeta::jsonschema::cli::lint(app);
} else if (command == "validate") {
Expand Down
9 changes: 9 additions & 0 deletions src/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,13 @@ auto default_dialect(const sourcemeta::core::Options &options)
return std::nullopt;
}

auto parse_indentation(const sourcemeta::core::Options &options)
-> std::size_t {
if (options.contains("indentation")) {
return std::stoull(std::string{options.at("indentation").front()});
}

return 2;
}

} // namespace sourcemeta::jsonschema::cli
2 changes: 2 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ auto parse_ignore(const sourcemeta::core::Options &options)
auto default_dialect(const sourcemeta::core::Options &options)
-> std::optional<std::string>;

auto parse_indentation(const sourcemeta::core::Options &options) -> std::size_t;

} // namespace sourcemeta::jsonschema::cli

#endif
4 changes: 4 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ add_jsonschema_test_unix(format_check_single_pass_keep_ordering)
add_jsonschema_test_unix(format_directory_ignore_directory)
add_jsonschema_test_unix(format_directory_ignore_file)
add_jsonschema_test_unix(format_check_single_invalid)
add_jsonschema_test_unix(format_check_single_fail_indentation)
add_jsonschema_test_unix(format_check_single_pass_indentation)
add_jsonschema_test_unix(format_single_indentation)

# Validate
add_jsonschema_test_unix(validate/fail_instance_directory)
Expand Down Expand Up @@ -255,6 +258,7 @@ add_jsonschema_test_unix(lint/fail_lint_default)
add_jsonschema_test_unix(lint/pass_lint_json)
add_jsonschema_test_unix(lint/pass_lint_fix_json)
add_jsonschema_test_unix(lint/pass_lint_fix_strict)
add_jsonschema_test_unix(lint/pass_lint_fix_indentation)
add_jsonschema_test_unix(lint/pass_lint_ignore_short)
add_jsonschema_test_unix(lint/pass_lint_ignore_long)
add_jsonschema_test_unix(lint/pass_lint_examples_fix)
Expand Down
45 changes: 45 additions & 0 deletions test/format_check_single_fail_indentation.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/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": 1
}
EOF

"$1" fmt "$TMP/schema.json" --indentation 4 --check 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/error.txt"
FAIL: $(realpath "$TMP")/schema.json
Got:
{
"\$schema": "http://json-schema.org/draft-04/schema#",
"type": 1
}

But expected:
{
"\$schema": "http://json-schema.org/draft-04/schema#",
"type": 1
}

EOF

diff "$TMP/stderr.txt" "$TMP/error.txt"

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

diff "$TMP/schema.json" "$TMP/expected.json"
26 changes: 26 additions & 0 deletions test/format_check_single_pass_indentation.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/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": 1
}
EOF

"$1" fmt "$TMP/schema.json" --check --indentation 4

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

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

set -o errexit
set -o nounset

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

cat << 'EOF' > "$TMP/schema.json"
{
"additionalProperties": false,
"title": "Hello World",
"properties": {"foo": {}, "bar": {}}
}
EOF

"$1" fmt "$TMP/schema.json" --indentation 4

cat << 'EOF' > "$TMP/expected.json"
{
"title": "Hello World",
"properties": {
"bar": {},
"foo": {}
},
"additionalProperties": false
}
EOF

diff "$TMP/schema.json" "$TMP/expected.json"
29 changes: 29 additions & 0 deletions test/lint/pass_lint_fix_indentation.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/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",
"const": "foo",
"title": "I should not be moved up"
}
EOF

"$1" lint "$TMP/schema.json" --fix --indentation 4

cat << 'EOF' > "$TMP/expected.json"
{
"$schema": "http://json-schema.org/draft-06/schema#",
"const": "foo",
"title": "I should not be moved up"
}
EOF

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