Skip to content

Commit 10d3117

Browse files
authored
Extend the --resolve option to take directories of schemas (#55)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent a8ece64 commit 10d3117

File tree

7 files changed

+82
-12
lines changed

7 files changed

+82
-12
lines changed

docs/bundle.markdown

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Bundling
33

44
```sh
55
jsonschema bundle <schema.json>
6-
[--http/-h] [--verbose/-v] [--resolve/-r <schema.json> ...]
6+
[--http/-h] [--verbose/-v] [--resolve/-r <schemas-or-directories> ...]
77
```
88

99
A schema may contain references to remote schemas outside the scope of the
@@ -77,6 +77,13 @@ jsonschema bundle path/to/my/schema.json \
7777
--resolve path/to/three.json
7878
```
7979

80+
### Bundle a JSON Schema importing a directory of `.schema.json` schemas
81+
82+
```sh
83+
jsonschema bundle path/to/my/schema.json \
84+
--resolve path/to/schemas --extension schema.json
85+
```
86+
8087
### Bundle a JSON Schema while enabling HTTP resolution
8188

8289
```sh

docs/test.markdown

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ Testing
77
88
```sh
99
jsonschema test [schemas-or-directories...]
10-
[--http/-h] [--metaschema/-m] [--verbose/-v] [--resolve/-r <schema.json> ...]
11-
[--extension/-e <extension>]
10+
[--http/-h] [--metaschema/-m] [--verbose/-v]
11+
[--resolve/-r <schemas-or-directories> ...] [--extension/-e <extension>]
1212
```
1313

1414
Schemas are code. As such, you should run an automated unit testing suite
@@ -91,3 +91,9 @@ jsonschema test path/to/test.json --http
9191
```sh
9292
jsonschema test path/to/test.json --resolve path/to/external.json
9393
```
94+
95+
### Run a single test definition importing a directory of `.schema.json` schemas
96+
97+
```sh
98+
jsonschema test path/to/test.json --resolve path/to/schemas --extension schema.json
99+
```

docs/validate.markdown

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ Validating
77
88
```sh
99
jsonschema validate <schema.json>
10-
[instance.json] [--http/-h] [--metaschema/-m] [--verbose/-v] [--resolve/-r <schema.json> ...]
10+
[instance.json] [--http/-h] [--metaschema/-m] [--verbose/-v]
11+
[--resolve/-r <schemas-or-directories> ...]
1112
```
1213

1314
The most popular use case of JSON Schema is to validate JSON documents. The
@@ -72,5 +73,13 @@ jsonschema validate path/to/my/schema.json path/to/my/instance.json --http
7273
### Validate a JSON instance importing a single local schema
7374

7475
```sh
75-
jsonschema validate path/to/my/schema.json path/to/my/instance.json --resolve path/to/external.json
76+
jsonschema validate path/to/my/schema.json path/to/my/instance.json \
77+
--resolve path/to/external.json
78+
```
79+
80+
### Validate a JSON instance importing a directory of `.schema.jsin` schemas
81+
82+
```sh
83+
jsonschema validate path/to/my/schema.json path/to/my/instance.json \
84+
--resolve path/to/schemas --extension schema.json
7685
```

src/main.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ constexpr std::string_view USAGE_DETAILS{R"EOF(
1616
Global Options:
1717
1818
--verbose, -v Enable verbose output
19-
--resolve, -r Import the given JSON Schema into the resolution context
19+
--resolve, -r Import the given JSON Schema (or directory of schemas)
20+
into the resolution context
2021
2122
Commands:
2223

src/utils.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,18 @@ auto resolver(const std::map<std::string, std::vector<std::string>> &options,
202202
}};
203203

204204
if (options.contains("resolve")) {
205-
for (const auto &schema_path : options.at("resolve")) {
206-
log_verbose(options) << "Loading schema: " << schema_path << "\n";
207-
dynamic_resolver.add(sourcemeta::jsontoolkit::from_file(schema_path));
205+
for (const auto &entry :
206+
for_each_json(options.at("resolve"), parse_extensions(options))) {
207+
log_verbose(options) << "Loading schema: " << entry.first << "\n";
208+
dynamic_resolver.add(entry.second);
208209
}
209210
}
210211

211212
if (options.contains("r")) {
212-
for (const auto &schema_path : options.at("r")) {
213-
log_verbose(options) << "Loading schema: " << schema_path << "\n";
214-
dynamic_resolver.add(sourcemeta::jsontoolkit::from_file(schema_path));
213+
for (const auto &entry :
214+
for_each_json(options.at("r"), parse_extensions(options))) {
215+
log_verbose(options) << "Loading schema: " << entry.first << "\n";
216+
dynamic_resolver.add(entry.second);
215217
}
216218
}
217219

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ add_jsonschema_test_unix(validate_fail_only_metaschema)
3333
add_jsonschema_test_unix(bundle_non_remote)
3434
add_jsonschema_test_unix(bundle_remote_single_schema)
3535
add_jsonschema_test_unix(bundle_remote_no_http)
36+
add_jsonschema_test_unix(bundle_remote_directory)
3637
add_jsonschema_test_unix(test_single_pass)
3738
add_jsonschema_test_unix(test_single_fail)
3839
add_jsonschema_test_unix(test_single_unsupported)

test/bundle_remote_directory.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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/schema.json"
11+
{
12+
"$schema": "https://json-schema.org/draft/2020-12/schema",
13+
"$id": "https://example.com",
14+
"$ref": "nested"
15+
}
16+
EOF
17+
18+
mkdir "$TMP/schemas"
19+
cat << 'EOF' > "$TMP/schemas/remote.json"
20+
{
21+
"$schema": "https://json-schema.org/draft/2020-12/schema",
22+
"$id": "https://example.com/nested",
23+
"type": "string"
24+
}
25+
EOF
26+
27+
"$1" bundle "$TMP/schema.json" --resolve "$TMP/schemas" > "$TMP/result.json"
28+
29+
cat << 'EOF' > "$TMP/expected.json"
30+
{
31+
"$schema": "https://json-schema.org/draft/2020-12/schema",
32+
"$id": "https://example.com",
33+
"$ref": "nested",
34+
"$defs": {
35+
"https://example.com/nested": {
36+
"$schema": "https://json-schema.org/draft/2020-12/schema",
37+
"$id": "https://example.com/nested",
38+
"type": "string"
39+
}
40+
}
41+
}
42+
EOF
43+
44+
diff "$TMP/result.json" "$TMP/expected.json"

0 commit comments

Comments
 (0)