Add transform = ... attribute (#312)

This allows running arbitrary transforms on generated schemas when deriving `JsonSchema`
This commit is contained in:
Graham Esau 2024-08-10 09:56:52 +01:00 committed by GitHub
parent 29067a0331
commit 14b06e71ba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 173 additions and 10 deletions

View file

@ -49,6 +49,7 @@ TABLE OF CONTENTS
- [`deprecated`](#deprecated)
- [`crate`](#crate)
- [`extend`](#extend)
- [`transform`](#transform)
- [Doc Comments (`doc`)](#doc)
</details>
@ -326,10 +327,31 @@ Set on a container, variant or field to add properties (or replace existing prop
The key must be a quoted string, and the value can be any expression that produces a type implementing `serde::Serialize`. The value can also be a JSON literal which can interpolate other values.
```plaintext
#[derive(JsonSchema)]
#[schemars(extend("simple" = "string value", "complex" = {"array": [1, 2, 3]}))]
struct Struct;
```
<h3 id="transform">
`#[schemars(transform = some::transform)]`
</h3>
Set on a container, variant or field to run a `schemars::transform::Transform` against the generated schema. This can be specified multiple times to run multiple transforms.
The `Transform` trait is implemented on functions with the signature `fn(&mut Schema) -> ()`, allowing you to do this:
```rust
fn my_transform(schema: &mut Schema) {
todo!()
}
#[derive(JsonSchema)]
#[schemars(transform = my_transform)]
struct Struct;
```
<h3 id="doc">
Doc Comments (`#[doc = "..."]`)

View file

@ -24,7 +24,3 @@ install_if -> { RUBY_PLATFORM =~ %r!mingw|mswin|java! } do
gem "tzinfo", "~> 1.2"
gem "tzinfo-data"
end
# Performance-booster for watching directories on Windows
gem "wdm", "~> 0.1.1", :install_if => Gem.win_platform?