Skip to content

Commit bc136c2

Browse files
committed
feat: Schema bundling
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
1 parent 7e49e4f commit bc136c2

File tree

22 files changed

+2028
-116
lines changed

22 files changed

+2028
-116
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
/.vscode
1313

1414
Cargo.lock
15+
.worktrees/

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Added
66

7+
- `bundle(schema)` / `async_bundle(schema)` / `ValidationOptions::bundle`: produce a self-contained schema document with external `$ref` targets embedded in a draft-appropriate container (`definitions` for Draft 4/6/7, `$defs` for Draft 2019-09/2020-12) while preserving `$ref` values. [#791](https://github.com/Stranger6667/jsonschema/issues/791).
8+
- **CLI**: `jsonschema validate` and `jsonschema bundle` subcommands. Flat invocation (`jsonschema schema.json -i …`) is deprecated — use `jsonschema validate` instead. [#791](https://github.com/Stranger6667/jsonschema/issues/791).
79
- `ValidationError::absolute_keyword_location()` to get the absolute keyword location URI of the schema node that produced the error. [#737](https://github.com/Stranger6667/jsonschema/issues/737).
810

911
### Changed

crates/jsonschema-cli/README.md

Lines changed: 71 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[<img alt="crates.io" src="https://img.shields.io/crates/v/jsonschema-cli.svg?style=flat-square&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/jsonschema-cli)
44
[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-jsonschema-cli?style=flat-square&labelColor=555555&logo=docs.rs" height="20">](https://docs.rs/jsonschema-cli)
55

6-
A fast command-line tool for JSON Schema validation, powered by the `jsonschema` crate.
6+
A fast command-line tool for JSON Schema validation and bundling, powered by the `jsonschema` crate.
77

88
## Installation
99

@@ -31,11 +31,8 @@ Download the latest binary for your platform from the [releases page](https://gi
3131
3232
Example installation on Linux/macOS:
3333
```bash
34-
# Download and extract (replace VERSION with actual version, e.g., cli-v0.36.0)
3534
curl -LO https://github.com/Stranger6667/jsonschema-rs/releases/download/VERSION/jsonschema-cli-x86_64-unknown-linux-gnu.tar.gz
3635
tar xzf jsonschema-cli-x86_64-unknown-linux-gnu.tar.gz
37-
38-
# Move to a directory in your PATH
3936
sudo mv jsonschema-cli /usr/local/bin/
4037
```
4138

@@ -48,63 +45,103 @@ cargo install jsonschema-cli
4845
## Usage
4946

5047
```
51-
jsonschema [OPTIONS] <SCHEMA>
48+
jsonschema <COMMAND>
5249
```
5350

54-
**NOTE**: It only supports valid JSON as input.
51+
Two subcommands are available: `validate` and `bundle` (inline external refs).
52+
53+
> ⚠️ **Deprecation notice:** The flat invocation `jsonschema schema.json -i instance.json` still works but is deprecated. Migrate to `jsonschema validate schema.json -i instance.json`.
54+
55+
---
56+
57+
## `jsonschema validate` — validate instances
58+
59+
```
60+
jsonschema validate [OPTIONS] <SCHEMA>
61+
```
5562

5663
### Options
5764

58-
- `-i, --instance <FILE>`: JSON instance(s) to validate (can be used multiple times)
59-
- `--output <text|flag|list|hierarchical>`: Select output style (default: `text`). `text` prints the human-friendly summary, while the structured modes emit newline-delimited JSON (`ndjson`) records with `schema`, `instance`, and JSON Schema Output v1 payloads.
60-
- `-v, --version`: Show version information
61-
- `--help`: Display help information
65+
| Flag | Description |
66+
|---|---|
67+
| `-i, --instance <FILE>` | Instance(s) to validate (repeatable) |
68+
| `-d, --draft <DRAFT>` | Enforce a specific draft (`4`, `6`, `7`, `2019`, `2020`) |
69+
| `--assert-format` / `--no-assert-format` | Enable/disable `format` keyword validation |
70+
| `--output <text\|flag\|list\|hierarchical>` | Output style (default: `text`) |
71+
| `--errors-only` | Suppress successful validations |
72+
| `--connect-timeout <SECONDS>` | Connection timeout for remote `$ref` retrieval |
73+
| `--timeout <SECONDS>` | Total HTTP request timeout |
74+
| `-k, --insecure` | Skip TLS certificate verification |
75+
| `--cacert <FILE>` | Custom CA certificate (PEM) |
6276

63-
### Examples:
77+
### Examples
6478

6579
Validate a single instance:
6680
```
67-
jsonschema schema.json -i instance.json
81+
jsonschema validate schema.json -i instance.json
6882
```
6983

70-
Validate multiple instances:
84+
Validate multiple instances and emit structured output:
7185
```
72-
jsonschema schema.json -i instance1.json -i instance2.json
86+
jsonschema validate schema.json -i a.json -i b.json --output list
87+
{"output":"list","schema":"schema.json","instance":"a.json","payload":{"valid":true,...}}
88+
{"output":"list","schema":"schema.json","instance":"b.json","payload":{"valid":false,...}}
7389
```
7490

75-
Emit JSON Schema Output v1 (`list`) for multiple instances:
91+
---
92+
93+
## `jsonschema bundle` — inline external `$ref` targets
94+
95+
Embeds all `$ref` targets into a draft-appropriate container:
96+
- `definitions` for Draft 4/6/7
97+
- `$defs` for Draft 2019-09/2020-12
98+
99+
`$ref` values are preserved unchanged ([Appendix B](https://json-schema.org/draft/2020-12/json-schema-core#appendix-B)).
100+
76101
```
77-
jsonschema schema.json -i instance1.json -i instance2.json --output list
78-
{"output":"list","schema":"schema.json","instance":"instance1.json","payload":{"valid":true,...}}
79-
{"output":"list","schema":"schema.json","instance":"instance2.json","payload":{"valid":false,...}}
102+
jsonschema bundle [OPTIONS] <SCHEMA>
80103
```
81104

82-
## Features
105+
### Options
106+
107+
| Flag | Description |
108+
|---|---|
109+
| `--resource <URI=FILE>` | Register an external schema resource (repeatable) |
110+
| `-o, --output <FILE>` | Write result to file instead of stdout |
111+
| `--connect-timeout`, `--timeout`, `-k`, `--cacert` | Same as `validate` |
83112

84-
- Validate one or more JSON instances against a single schema
85-
- Clear, concise output with detailed error reporting
86-
- Fast validation using the `jsonschema` Rust crate
113+
### Examples
114+
115+
With a locally registered resource:
116+
```
117+
jsonschema bundle root.json --resource https://example.com/address.json=address.json
118+
```
119+
120+
Write to file:
121+
```
122+
jsonschema bundle root.json -o bundled.json
123+
```
87124

88-
## Output
125+
---
89126

90-
For each instance:
127+
## Output formats (`validate`)
91128

92-
- `text` (default): prints `<filename> - VALID` or `<filename> - INVALID. Errors:` followed by numbered error messages.
93-
- `flag|list|hierarchical`: emit newline-delimited JSON objects shaped as:
129+
| Mode | Description |
130+
|---|---|
131+
| `text` (default) | `<file> - VALID` or `<file> - INVALID. Errors: …` |
132+
| `flag` | `{"valid": true/false}` per instance (ndjson) |
133+
| `list` | Flat list of annotations/errors (ndjson) |
134+
| `hierarchical` | Nested structure following schema hierarchy (ndjson) |
94135

136+
Structured modes emit newline-delimited JSON records:
95137
```json
96-
{
97-
"output": "list",
98-
"schema": "schema.json",
99-
"instance": "instance.json",
100-
"payload": { "... JSON Schema Output v1 data ..." }
101-
}
138+
{"output":"list","schema":"schema.json","instance":"instance.json","payload":{...}}
102139
```
103140

104141
## Exit Codes
105142

106-
- 0: All instances are valid (or no instances provided)
107-
- 1: One or more instances are invalid, or there was an error
143+
- `0` — all instances valid (or no instances provided)
144+
- `1` — one or more instances invalid, or an error occurred
108145

109146
## License
110147

0 commit comments

Comments
 (0)