-
Notifications
You must be signed in to change notification settings - Fork 52
(GH-538) Define schema extension methods #1197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SteveL-MSFT
merged 4 commits into
PowerShell:main
from
michaeltlombardi:gh-538/main/extension-methods
Oct 22, 2025
Merged
(GH-538) Define schema extension methods #1197
SteveL-MSFT
merged 4 commits into
PowerShell:main
from
michaeltlombardi:gh-538/main/extension-methods
Oct 22, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
After #1171 is merged, I'll rebase this PR prior to final review and merge. |
bcc9cf6 to
970dd4e
Compare
|
@michaeltlombardi can you rebase off main so I can see just the delta? |
|
@SteveL-MSFT - I don't think so, because these changes build on the But all the changes for this PR (on top of #1171) are in this commit: |
This change begins the schema canonicalization process by adding a new library crate to contain the helpers for defining canonical and enhanced schemas ergonomically. In this initial phase, the new crate defines: - The `VSCODE_KEYWORDS` constant, enumerating the keywords that VS Code's JSON language server recognizes. - The `idiomaticize_string_enum` transformer for munging the generated schemas for string enums with annotation keywords. - The `idiomaticize_externally_tagged_enum` transformer for munging the generated schemas for externally tagged enums to a more idiomatic and readable representation.
This change uses the new `idiomaticize_*` transformers from the `dsc-lib-jsonschema` crate in the `dsc-lib` crate to ensure that the generated schemas are idiomatic.
This change updates the i18n Pester tests to account for: - Usage of the `t!` macro when the translation key is defined on the next line. - Usage of the convenience `panic_t!` macro, which simplifies sending translated strings to the `panic!` macro. - Usage of the convenience `assert_t!` macro, which simplifies sending translated strings to the panic when an `assert!` macro fails. These use cases were required for the `dsc-lib-jsonschema` crate, which uses both `panic_t!` and `assert_t!` to raise translated panic messages when a transform is invalidly applied. While this change doesn't modify the behavior for any other crates, it does lay the groundwork for translating panic messages and assertions throughout the crates.
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.
970dd4e to
62221fc
Compare
SteveL-MSFT
approved these changes
Oct 22, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Summary
This change defines the
SchemaUtilityExtensionstrait in thedsc-lib-jsonschemacrate to provide useful shorthand methods for theschemars::Schematype when munging and transforming schemas.This change:
dsc-lib-jsonschemacrate #1171PR Context
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:
$idto retrieve and set the identifier for a schema.$defsto retrieve and munge referenced subschemas.propertiesto retrieve and munge property definitions.The trait does define generic methods for retrieving keywords with an expected type, like
get_keyword_as_object()andget_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:So you can instead do: