Commit 62221fc
committed
(GH-538) Define schema extension methods
This change defines the `SchemaUtilityExtensions` trait in the
`dsc-lib-jsonschema` crate to provide useful shorthand methods for the
`schemars::Schema` type when munging and transforming schemas.
While the trait includes some methods I expect to be useful, it doesn't
try to fully map methods for retrieving every keyword. Instead, it
primarily simplifies working with the following keywords:
- `$id` to retrieve and set the identifier for a schema.
- `$defs` to retrieve and munge referenced subschemas.
- `properties` to retrieve and munge property definitions.
The trait _does_ define generic methods for retrieving keywords with an
expected type, like `get_keyword_as_object()` and `get_keyword_as_str`.
These are convenience methods to replace always calling code like the
following when you want to retrieve a keyword with a known type:
```rust
let ref schema = json_schema!({
"title": "Schema title"
});
if let Some(title) = schema.get("title").and_then(|v| v.as_str()) {
println!("Schema title is {title}");
}
```
So you can instead do:
```rust
let ref schema = json_schema!({
"title": "Schema title"
});
if let Some(title) = schema.get_keyword_as_str("title") {
println!("Schema title is {title}");
}
```
This change:
- Defines and implements the trait, providing documentation for every
new method.
- Adds tests for the behaviors of every extension method.1 parent 4a5117e commit 62221fc
7 files changed
Lines changed: 2389 additions & 4 deletions
File tree
- lib/dsc-lib-jsonschema
- src
- tests
- schema_utility_extensions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
198 | 198 | | |
199 | 199 | | |
200 | 200 | | |
201 | | - | |
| 201 | + | |
202 | 202 | | |
203 | 203 | | |
204 | 204 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
0 commit comments