Skip to content

Commit

Permalink
Extend the --resolve option to take directories of schemas (#55)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
  • Loading branch information
jviotti authored Jun 4, 2024
1 parent a8ece64 commit 10d3117
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 12 deletions.
9 changes: 8 additions & 1 deletion docs/bundle.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Bundling

```sh
jsonschema bundle <schema.json>
[--http/-h] [--verbose/-v] [--resolve/-r <schema.json> ...]
[--http/-h] [--verbose/-v] [--resolve/-r <schemas-or-directories> ...]
```

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

### Bundle a JSON Schema importing a directory of `.schema.json` schemas

```sh
jsonschema bundle path/to/my/schema.json \
--resolve path/to/schemas --extension schema.json
```

### Bundle a JSON Schema while enabling HTTP resolution

```sh
Expand Down
10 changes: 8 additions & 2 deletions docs/test.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Testing
```sh
jsonschema test [schemas-or-directories...]
[--http/-h] [--metaschema/-m] [--verbose/-v] [--resolve/-r <schema.json> ...]
[--extension/-e <extension>]
[--http/-h] [--metaschema/-m] [--verbose/-v]
[--resolve/-r <schemas-or-directories> ...] [--extension/-e <extension>]
```

Schemas are code. As such, you should run an automated unit testing suite
Expand Down Expand Up @@ -91,3 +91,9 @@ jsonschema test path/to/test.json --http
```sh
jsonschema test path/to/test.json --resolve path/to/external.json
```

### Run a single test definition importing a directory of `.schema.json` schemas

```sh
jsonschema test path/to/test.json --resolve path/to/schemas --extension schema.json
```
13 changes: 11 additions & 2 deletions docs/validate.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Validating
```sh
jsonschema validate <schema.json>
[instance.json] [--http/-h] [--metaschema/-m] [--verbose/-v] [--resolve/-r <schema.json> ...]
[instance.json] [--http/-h] [--metaschema/-m] [--verbose/-v]
[--resolve/-r <schemas-or-directories> ...]
```

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

```sh
jsonschema validate path/to/my/schema.json path/to/my/instance.json --resolve path/to/external.json
jsonschema validate path/to/my/schema.json path/to/my/instance.json \
--resolve path/to/external.json
```

### Validate a JSON instance importing a directory of `.schema.jsin` schemas

```sh
jsonschema validate path/to/my/schema.json path/to/my/instance.json \
--resolve path/to/schemas --extension schema.json
```
3 changes: 2 additions & 1 deletion src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ constexpr std::string_view USAGE_DETAILS{R"EOF(
Global Options:
--verbose, -v Enable verbose output
--resolve, -r Import the given JSON Schema into the resolution context
--resolve, -r Import the given JSON Schema (or directory of schemas)
into the resolution context
Commands:
Expand Down
14 changes: 8 additions & 6 deletions src/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,18 @@ auto resolver(const std::map<std::string, std::vector<std::string>> &options,
}};

if (options.contains("resolve")) {
for (const auto &schema_path : options.at("resolve")) {
log_verbose(options) << "Loading schema: " << schema_path << "\n";
dynamic_resolver.add(sourcemeta::jsontoolkit::from_file(schema_path));
for (const auto &entry :
for_each_json(options.at("resolve"), parse_extensions(options))) {
log_verbose(options) << "Loading schema: " << entry.first << "\n";
dynamic_resolver.add(entry.second);
}
}

if (options.contains("r")) {
for (const auto &schema_path : options.at("r")) {
log_verbose(options) << "Loading schema: " << schema_path << "\n";
dynamic_resolver.add(sourcemeta::jsontoolkit::from_file(schema_path));
for (const auto &entry :
for_each_json(options.at("r"), parse_extensions(options))) {
log_verbose(options) << "Loading schema: " << entry.first << "\n";
dynamic_resolver.add(entry.second);
}
}

Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ add_jsonschema_test_unix(validate_fail_only_metaschema)
add_jsonschema_test_unix(bundle_non_remote)
add_jsonschema_test_unix(bundle_remote_single_schema)
add_jsonschema_test_unix(bundle_remote_no_http)
add_jsonschema_test_unix(bundle_remote_directory)
add_jsonschema_test_unix(test_single_pass)
add_jsonschema_test_unix(test_single_fail)
add_jsonschema_test_unix(test_single_unsupported)
Expand Down
44 changes: 44 additions & 0 deletions test/bundle_remote_directory.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/sh

set -o errexit
set -o nounset

TMP="$(mktemp -d)"
clean() { rm -rf "$TMP"; }
trap clean EXIT

cat << 'EOF' > "$TMP/schema.json"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com",
"$ref": "nested"
}
EOF

mkdir "$TMP/schemas"
cat << 'EOF' > "$TMP/schemas/remote.json"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/nested",
"type": "string"
}
EOF

"$1" bundle "$TMP/schema.json" --resolve "$TMP/schemas" > "$TMP/result.json"

cat << 'EOF' > "$TMP/expected.json"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com",
"$ref": "nested",
"$defs": {
"https://example.com/nested": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/nested",
"type": "string"
}
}
}
EOF

diff "$TMP/result.json" "$TMP/expected.json"

0 comments on commit 10d3117

Please sign in to comment.