-
Notifications
You must be signed in to change notification settings - Fork 16
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
feat: add --json to lint command #228
Conversation
Signed-off-by: karan-palan <[email protected]>
Based on this discussion. @jviotti PTAL, if it looks fine, I'll add some tests and update the docs. |
@Karan-Palan Great start! Some minor tweaks to be more JSON Schema idiomatic:
Overall, just trying to make it match JSON Schema Standard Output Formats a bit, at least in style! |
src/command_lint.cc
Outdated
@@ -47,20 +49,46 @@ auto sourcemeta::jsonschema::cli::lint( | |||
sourcemeta::core::schema_format_compare); | |||
output << "\n"; | |||
} | |||
|
|||
// If --fix and --json were both requested, optionally print a small JSON: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In similar cases, other linters will output exactly the same, just that if you run it again, there will be no issues. Let's adopt that, as developers will be familiar with that
I also left a tiny comment, but looks pretty good. Looking forward to some tests and the docs |
Signed-off-by: karan-palan <[email protected]>
Signed-off-by: karan-palan <[email protected]>
… and lint detecting type alongside enum as an anti-pattern Signed-off-by: karan-palan <[email protected]>
Done, PTAL |
docs/lint.markdown
Outdated
@@ -27,6 +27,8 @@ automatically fix many of them. | |||
|
|||
**The `--fix/-f` option is not supported when passing YAML schemas.** | |||
|
|||
**The `--json/-j` option prints results as a JSON object rather than human‐readable text.** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would remove this line. We don't have it for inspect
(which also supports the same flag) and I guess it is pretty self explanatory
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is not exercising the --json
flag? We have linting tests already. The point is to do linting with --json
on the new ones!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here. This test is not exercising the --json
flag.
Signed-off-by: karan-palan <[email protected]>
15a2aae
to
919197b
Compare
Done, ptal |
src/command_lint.cc
Outdated
|
||
if (output_json) { | ||
auto msg = sourcemeta::core::JSON::make_object(); | ||
msg.assign("fixApplied", sourcemeta::core::JSON{true}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want this. If --fix
was passed, still print the normal JSON output
src/main.cc
Outdated
[--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 and warnings in JSON format. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use --json/-j to output lint errors and warnings in JSON format. | |
Use --json/-j to output lint errors in JSON. |
Looking good! Can you add one more tests exercising |
Signed-off-by: karan-palan <[email protected]>
Signed-off-by: karan-palan <[email protected]>
Signed-off-by: KaranPalan <[email protected]>
Done, ptal |
test/lint/pass_lint_fix_json.sh
Outdated
{ | ||
"valid": true, | ||
"errors": [], | ||
"fixApplied": true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's get rid of this flag. We shouldn't report it. Usually what linters do with --fix
is that they will still report ALL the errors that were fixed (as if you didn't pass --fix
) but output code 0. And if you run it again, you get no errors reported at all
test/lint/pass_lint_json.sh
Outdated
EOF | ||
|
||
"$1" lint "$TMP/schema.json" --json >"$TMP/output.json" 2>&1 && CODE="$?" || CODE="$?" | ||
test "$CODE" = "0" || exit 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test "$CODE" = "0" || exit 1 | |
test "$CODE" = "0" |
You don't need the OR. The entire script should fail on any line failures
test/lint/pass_lint_json.sh
Outdated
cat << 'EOF' > "$TMP/schema.json" | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"const": "foo" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This schema is invalid. const
is not present in Draft 4. That's a linter rule we should have in the future I guess. For now, can you make sure this schema is at least valid? i.e.
"const": "foo" | |
"enum": [ "foo" ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just added those in my rules :D
@@ -0,0 +1,26 @@ | |||
#!/bin/sh | |||
set -o errexit |
There was a problem hiding this comment.
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
src/command_lint.cc
Outdated
} | ||
sourcemeta::core::prettify(output_json_object, std::cout); | ||
std::cout << "\n"; | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to have an else
clause that does nothing. Can you delete it?
src/command_lint.cc
Outdated
output_json_object.assign("valid", sourcemeta::core::JSON{result}); | ||
output_json_object.assign("errors", sourcemeta::core::JSON{errors_array}); | ||
if (fix) { | ||
output_json_object.assign("fixApplied", sourcemeta::core::JSON{true}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comment below. We shouldn't be reporting this
Signed-off-by: karan-palan <[email protected]>
…/jsonschema into feat/add-json-output-to-lint
Signed-off-by: karan-palan <[email protected]>
Signed-off-by: karan-palan <[email protected]>
Done. Apologies for the extra commits for adding blanks after hashes, my formatter for bash files removes extra lines (below hash and last line) |
I left a few minor remaining suggestions, but its almost there! |
Actually, I guess I can just accept them myself and just merge. Let me see |
Signed-off-by: Juan Cruz Viotti <[email protected]>
Signed-off-by: Juan Cruz Viotti <[email protected]>
Thanks for your patience, @jviotti i! I really appreciate the feedback. I'll ensure that my future test cases follow the same format. |
Thank a lot for the work! I'll release a new version of the CLI in a bit |
Done! See v7.1.0 |
What kind of change does this PR introduce?
--json
(or-j
) option in thelint
command.Issue Number:
lint
andvalidate
results in JSON if--json
is passed #58Screenshots/videos:
If relevant, did you update the documentation?
Not yet