Skip to content

Commit 62221fc

Browse files
(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

File tree

7 files changed

+2389
-4
lines changed

7 files changed

+2389
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ tree-sitter-rust = { version = "0.24" }
198198
utfx = { version = "0.1" }
199199
# dsc-lib
200200
uuid = { version = "1.18", features = ["v4"] }
201-
# dsc-lib
201+
# dsc-lib, dsc-lib-jsonschema
202202
url = { version = "2.5" }
203203
# dsc-lib
204204
urlencoding = { version = "2.1" }

lib/dsc-lib-jsonschema/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.0.0" # version stays 0.0.0 until we're ready to publish - should pi
44
edition = "2024"
55

66
[lib]
7-
doctest = false # Disable doc tests for compilation speed
7+
doctest = false # Disable doc tests by default for compilation speed
88

99
[dependencies]
1010
regex = { workspace = true }
@@ -13,6 +13,7 @@ schemars = { workspace = true }
1313
serde = { workspace = true }
1414
serde_json = { workspace = true }
1515
tracing = { workspace = true }
16+
url = { workspace = true }
1617

1718
[dev-dependencies]
1819
# Helps review complex comparisons, like schemas

lib/dsc-lib-jsonschema/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ use rust_i18n::i18n;
88
#[macro_use]
99
pub mod macros;
1010

11-
pub mod vscode;
11+
pub mod schema_utility_extensions;
1212
pub mod transforms;
13+
pub mod vscode;
1314

1415
#[cfg(test)]
1516
mod tests;

0 commit comments

Comments
 (0)