Skip to content

Commit 2162b2e

Browse files
committed
fix(validate): address review feedback (includes, pointer handling, tests, and behavior documentation)
Signed-off-by: Vaibhav mittal <vaibhavmittal929@gmail.com>
1 parent 7a0ac9f commit 2162b2e

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/command_validate.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <iostream> // std::cerr
1414
#include <string> // std::string
1515
#include <string_view> // std::string_view
16+
#include <utility> // std::as_const
1617

1718
#include "command.h"
1819
#include "configuration.h"
@@ -269,15 +270,19 @@ auto sourcemeta::jsonschema::validate(const sourcemeta::core::Options &options)
269270
: sourcemeta::core::read_yaml_or_json(schema_path)};
270271

271272
if (options.contains("path") && !options.at("path").empty()) {
273+
// Invalid pointer syntax is handled by to_pointer(), consistent with
274+
// --entrypoint behavior.
272275
const auto path_string{std::string{options.at("path").front()}};
273276
const auto pointer{sourcemeta::core::to_pointer(path_string)};
274277
const auto *const result{sourcemeta::core::try_get(schema, pointer)};
275278
// We intentionally reuse NotSchemaError here to align with existing CLI
276279
// error semantics without introducing a new error type.
277280
if (!result) {
278-
throw NotSchemaError{schema_from_stdin ? stdin_path()
279-
: schema_resolution_base};
281+
throw NotSchemaError{schema_resolution_base};
280282
}
283+
// Note: extracting a sub-schema may break $ref references outside the
284+
// selected subtree. This is expected behavior for --path given the current
285+
// CLI design.
281286
// `result` points into `schema`, so we must copy before reassigning to
282287
// avoid a use-after-free (the copy assignment destroys schema's storage
283288
// before reading from other when they alias).

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ add_jsonschema_test_unix(validate/pass_path_openapi)
246246
add_jsonschema_test_unix(validate/fail_path_not_found)
247247
add_jsonschema_test_unix(validate/fail_path_not_schema)
248248
add_jsonschema_test_unix(validate/fail_path_with_template)
249+
add_jsonschema_test_unix(validate/fail_path_ref_outside_subtree)
249250
add_jsonschema_test_unix(validate/pass_config_ignore)
250251
add_jsonschema_test_unix(validate/pass_config_ignore_with_cli)
251252
add_jsonschema_test_unix(validate/pass_stdin_instance)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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/document.json"
11+
{
12+
"components": {
13+
"schemas": {
14+
"Address": {
15+
"$schema": "https://json-schema.org/draft/2020-12/schema",
16+
"type": "object"
17+
},
18+
"User": {
19+
"$schema": "https://json-schema.org/draft/2020-12/schema",
20+
"$ref": "#/components/schemas/Address"
21+
}
22+
}
23+
}
24+
}
25+
EOF
26+
27+
cat << 'EOF' > "$TMP/instance.json"
28+
{}
29+
EOF
30+
31+
"$1" validate "$TMP/document.json" "$TMP/instance.json" \
32+
--path "/components/schemas/User" \
33+
> /dev/null 2>&1 \
34+
&& EXIT_CODE="$?" || EXIT_CODE="$?"
35+
# Validation should fail when $ref points outside extracted subtree
36+
test "$EXIT_CODE" -ne "0"
37+

0 commit comments

Comments
 (0)