CLI: add stdin support for instance validation #614
CLI: add stdin support for instance validation #614Vaibhav701161 wants to merge 7 commits intosourcemeta:mainfrom
Conversation
ddb8312 to
9a040d7
Compare
|
Hi @jviotti , I’ve opened this PR and would appreciate a review when you have time. Thanks! |
|
Hey @Vaibhav701161 , sorry for the delay. It has been a hectic week. I'll try to get into it today |
|
I like the idea but I think there are some things to iron out:
I think for consistency, we can require the The other problem here will be line numbers. We should make sure all relevant commands handle standard input while still reporting line/column information and potentially some "special" file path like |
4d5ff3f to
112b374
Compare
|
Hey @jviotti , thanks a lot for the feedback. I’ve pushed an updated iteration that tries to address the concerns you mentioned:
|
f70d55d to
b9bb23e
Compare
|
Hi @jviotti , just a quick follow-up on this. |
|
|
||
| struct InputJSON { | ||
| std::filesystem::path first; | ||
| std::filesystem::path resolution_base; |
There was a problem hiding this comment.
If this is just a display name, maybe make a std::string? Otherwise <stdin> is not really a path
There was a problem hiding this comment.
I think this still needs to be addressed?
test/validate/pass_stdin.sh
Outdated
There was a problem hiding this comment.
Please split this test into multiple tests. If you see the other tests in the project, we have specific conventions and we always test a single thing per file. Plus you also want to add test for every other applicable command.
Finally, you didn't register this test in test/CMakeLists.txt, so it won't run at all
src/command_lint.cc
Outdated
| copy, sourcemeta::core::schema_walker, custom_resolver, | ||
| get_lint_callback(errors_array, entry, output_json), dialect, | ||
| sourcemeta::jsonschema::default_id(entry.first), | ||
| sourcemeta::core::URI::from_path(entry.resolution_base) |
There was a problem hiding this comment.
I think we still definitely want to use sourcemeta::jsonschema::default_id here, to concentrate that logic in a single place. Maybe make such function take entry directly, so that inside we can decide to use entry.resolution_base or whatever?
src/command_lint.cc
Outdated
| custom_resolver, | ||
| get_lint_callback(errors_array, entry, output_json), dialect, | ||
| sourcemeta::jsonschema::default_id(entry.first), | ||
| sourcemeta::core::URI::from_path(entry.resolution_base) |
src/input.h
Outdated
| // TODO: Print a verbose message for what is getting parsed | ||
| auto parsed{read_file(canonical)}; | ||
| result.push_back({std::move(canonical), std::move(parsed.document), | ||
| auto canonical_copy{canonical}; |
| } | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
I don't think this deletion was intended?
src/input.h
Outdated
| } | ||
| } | ||
| if (stdin_count > 1) { | ||
| throw std::runtime_error{"Standard input (-) can only be specified once"}; |
There was a problem hiding this comment.
| throw std::runtime_error{"Standard input (-) can only be specified once"}; | |
| throw std::runtime_error{"The standard input position argument `-` can only be specified once"}; |
src/input.h
Outdated
| std::size_t stdin_count{0}; | ||
| for (const auto &arg : arguments) { | ||
| if (arg == "-") { | ||
| stdin_count++; |
There was a problem hiding this comment.
I don't think counting all occurrences makes sense if you only want up to two. Maybe check that if you already got at least 2, then its an error and you can break?
test/validate/pass_stdin.sh
Outdated
| set +o errexit | ||
| cat "$TMP/schema.json" > "$TMP/schema_stdin.json" | ||
|
|
||
| OUTPUT=$("$1" validate - "$TMP/schema.json" < "$TMP/schema_stdin.json" 2>&1) |
There was a problem hiding this comment.
Please see the other shell scripts for the conventions on how we test stuff. Some obvious ones:
- We never capture into a variable. We pipe into a file and then
diffit - We never enable
set +o errexit - We never test more than one thing at once
Also your test is not registered in test/CMakeLists.txt so it won't run at all. But overall, split it so that there are multiple tests testing each one thing
Signed-off-by: Vaibhav mittal <vaibhavmittal929@gmail.com>
Signed-off-by: Vaibhav mittal <vaibhavmittal929@gmail.com>
Signed-off-by: Vaibhav mittal <vaibhavmittal929@gmail.com>
Signed-off-by: Vaibhav mittal <vaibhavmittal929@gmail.com>
Signed-off-by: Vaibhav mittal <vaibhavmittal929@gmail.com>
Signed-off-by: Vaibhav mittal <vaibhavmittal929@gmail.com>
b9bb23e to
77066a9
Compare
Signed-off-by: Vaibhav mittal <vaibhavmittal929@gmail.com>
There was a problem hiding this comment.
4 issues found across 12 files
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="src/command_fmt.cc">
<violation number="1" location="src/command_fmt.cc:25">
P2: Use a structured error type from `error.h` instead of `std::runtime_error` for stdin not supported. This enables proper machine-readable JSON output when `--json` is passed, following the pattern of other error types like `YAMLInputError`.
(Based on your team's feedback about avoiding dynamically constructed exception messages and reusing centralized error wrapper types.) [FEEDBACK_USED]</violation>
</file>
<file name="src/input.h">
<violation number="1" location="src/input.h:151">
P2: Missing YAML fallback for stdin input. The PR description states "JSON-first, YAML fallback" but `read_from_stdin()` only parses JSON. This is inconsistent with file handling which falls back to YAML if JSON parsing fails. Note: implementing YAML fallback would require first buffering stdin (since it can only be read once), then attempting JSON parse, then falling back to YAML on failure.</violation>
</file>
<file name="test/validate/fail_stdin_multiple.sh">
<violation number="1" location="test/validate/fail_stdin_multiple.sh:25">
P2: Add a JSON error assertion for this failure case so the structured error output is validated alongside the text output.
(Based on your team's feedback about adding JSON variants for failure test cases.) [FEEDBACK_USED]</violation>
</file>
<file name="test/validate/fail_stdin_schema.sh">
<violation number="1" location="test/validate/fail_stdin_schema.sh:22">
P2: Add a JSON error assertion for this fail_* test so structured error output remains covered.
(Based on your team's feedback about adding JSON variants for failure test cases.) [FEEDBACK_USED]</violation>
</file>
Since this is your first cubic review, here's how it works:
- cubic automatically reviews your code and comments on bugs and improvements
- Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
- Ask questions if you need clarification on any suggestion
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| error: The standard input position argument `-` can only be specified once | ||
| EOF | ||
|
|
||
| diff "$TMP/stderr.txt" "$TMP/expected.txt" |
There was a problem hiding this comment.
P2: Add a JSON error assertion for this failure case so the structured error output is validated alongside the text output.
(Based on your team's feedback about adding JSON variants for failure test cases.)
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At test/validate/fail_stdin_multiple.sh, line 25:
<comment>Add a JSON error assertion for this failure case so the structured error output is validated alongside the text output.
(Based on your team's feedback about adding JSON variants for failure test cases.) </comment>
<file context>
@@ -0,0 +1,25 @@
+error: The standard input position argument `-` can only be specified once
+EOF
+
+diff "$TMP/stderr.txt" "$TMP/expected.txt"
</file context>
| error: Reading the schema from standard input is not supported | ||
| EOF | ||
|
|
||
| diff "$TMP/stderr.txt" "$TMP/expected.txt" |
There was a problem hiding this comment.
P2: Add a JSON error assertion for this fail_* test so structured error output remains covered.
(Based on your team's feedback about adding JSON variants for failure test cases.)
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At test/validate/fail_stdin_schema.sh, line 22:
<comment>Add a JSON error assertion for this fail_* test so structured error output remains covered.
(Based on your team's feedback about adding JSON variants for failure test cases.) </comment>
<file context>
@@ -0,0 +1,22 @@
+error: Reading the schema from standard input is not supported
+EOF
+
+diff "$TMP/stderr.txt" "$TMP/expected.txt"
</file context>
There was a problem hiding this comment.
Can you add a failing case too?
|
|
||
| if (schema_path == "-") { | ||
| throw std::runtime_error{ | ||
| "Reading the schema from standard input is not supported"}; |
There was a problem hiding this comment.
Why not? Sounds like a valid use case, unless there is another reason why it wouldn't work that I'm not thinking about?
There was a problem hiding this comment.
So far we only have stdin tests for the validate command. But we should be testing stdin behaviour for every command that supports it. Otherwise it could be totally broken in other commands and we are not knowing about it
| for (const auto &entry : for_each_json(options)) { | ||
| if (entry.from_stdin) { | ||
| throw std::runtime_error{ | ||
| "This command does not support reading from standard input"}; |
|
I left some more comments. It's shaping up really well! |
|
@Vaibhav701161 The machinery that allows for I guess that would be most of the changes you made except the tests, and just never setting |
Adds stdin support for instance input to enable real-time editor diagnostics.
Fixes(#537)
src/input.h(JSON-first, YAML fallback).validateaccepts instances from stdin only; schemas must be file paths.$refagainst the current working directory.test/validate/pass_stdin.sh.sourcemeta/studio#61; addressessourcemeta/jsonschema#537.Refs: sourcemeta/studio#61, #537
*

Build and test: