Skip to content
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 --path Option to Target Specific Schema Parts in validate Command #220

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Karan-Palan
Copy link
Contributor

What kind of change does this PR introduce?

  • Feature: Added support for the --path (or -p) option in the validate command to allow targeting specific parts of a JSON schema using JSON Pointers.

Issue Number:

Screenshots/videos:

Screencast.from.2025-02-02.12-53-07.webm

If relevant, did you update the documentation?

Not yet

Summary

This PR introduces the --path (-p) option to the validate command, enabling users to specify a JSON Pointer for validating a sub-schema within a larger JSON schema. This is especially useful for OpenAPI specs and complex JSON structures where validation may be needed for specific nested sections.

Key changes:

  • Added --path/-p option to CLI parser.
  • Integrated JSON Pointer resolution to extract the specified sub-schema.
  • Added error handling for invalid pointers or sub-schemas.

Does this PR introduce a breaking change?

  • No breaking changes. The new option is fully backward-compatible. Existing workflows without the --path option will continue to function as expected.

@jviotti
Copy link
Member

jviotti commented Feb 3, 2025

Awesome! Great first step. Can you fix the formatting issues and add a couple of test cases (at least one where the pointer exists and one where it doesn't)?

@Karan-Palan Karan-Palan force-pushed the feat/add-path-to-validate branch 3 times, most recently from c14a91b to 7647e43 Compare February 3, 2025 09:42
@Karan-Palan Karan-Palan force-pushed the feat/add-path-to-validate branch from 7647e43 to c39d4c2 Compare February 3, 2025 09:48
@Karan-Palan
Copy link
Contributor Author

Awesome! Great first step. Can you fix the formatting issues and add a couple of test cases (at least one where the pointer exists and one where it doesn't)?

Thanks!
I fixed the linting errors - they were because my clang default was set to chromium's source. I also added some tests and docs (it's my first time writing tests in shell :p)
Please review when you get the time!

@jviotti
Copy link
Member

jviotti commented Feb 4, 2025

@Karan-Palan I think you forgot to actually register the tests, so they are not really running right now. You can do so like this:

diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 0f9840b..a3baa6d 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -74,6 +74,8 @@ add_jsonschema_test_unix(validate/pass_many_verbose)
 add_jsonschema_test_unix(validate/fail_many)
 add_jsonschema_test_unix(validate/fail_many_verbose)
 add_jsonschema_test_unix(validate/fail_yaml)
+add_jsonschema_test_unix(validate/pass_pointer_exists)
+add_jsonschema_test_unix(validate/fail_pointer_not_exists)

 # Metaschema
 add_jsonschema_test_unix(metaschema/pass_trace)

With that, running make does reveal a test failure!

const auto &arg = *it;

// a) --path=/something
if (arg.rfind("--path=", 0) == 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe options.at("p") and options.at("path") will give you what you want? Look at how we take options in other commands, like for --resolve

if ! "$1" validate \
"$TMP/schema.json" \
"$TMP/instance.json" \
--path=/properties/nested \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of accessing a subschema of the schema, to make the test more representative, can you make the schema a very concise OpenAPI spec (in JSON) and access one of its schema components?

set -o nounset

TMP="$(mktemp -d)"
cleanup() { rm -rf "$TMP"; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor thing, but other commands call this prelude clean. Can you do the same for consistency?

}
EOF

if ! "$1" validate \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, you don't need the explicit if check. Because of set -o errexit, the entire script will fail if any of its commands fail. See how we do it for other "pass" tests

exit 1
fi

echo "pass_pointer_exists.sh: PASS"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to print these things. ctest will already nicely report the specific test cases

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a test where the JSON Pointer passed to --path is indeed valid, but it's not an actual schema? Maybe /type or something like that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a --path option to commands like validate
2 participants