Expand description
Contains the Transform
trait, used to modify a constructed schema and optionally its subschemas.
This trait is automatically implemented for functions of the form fn(&mut Schema) -> ()
.
§Recursive Transforms
To make a transform recursive (i.e. apply it to subschemas), you have two options:
- call the
transform_subschemas
function within the transform function - wrap the
Transform
in aRecursiveTransform
§Examples
To add a custom property to all object schemas:
use schemars::transform::{Transform, transform_subschemas};
pub struct MyTransform;
impl Transform for MyTransform {
fn transform(&mut self, schema: &mut Schema) {
// First, make our change to this schema
schema.insert("my_property".to_string(), "hello world".into());
// Then apply the transform to any subschemas
transform_subschemas(self, schema);
}
}
let mut schema = json_schema!({
"type": "array",
"items": {}
});
MyTransform.transform(&mut schema);
assert_eq!(
schema,
json_schema!({
"type": "array",
"items": {
"my_property": "hello world"
},
"my_property": "hello world"
})
);
The same example with a fn
transform:
use schemars::transform::transform_subschemas;
fn add_property(schema: &mut Schema) {
schema.insert("my_property".to_string(), "hello world".into());
transform_subschemas(&mut add_property, schema)
}
let mut schema = json_schema!({
"type": "array",
"items": {}
});
add_property(&mut schema);
assert_eq!(
schema,
json_schema!({
"type": "array",
"items": {
"my_property": "hello world"
},
"my_property": "hello world"
})
);
And the same example using a closure wrapped in a RecursiveTransform
:
use schemars::transform::{Transform, RecursiveTransform};
let mut transform = RecursiveTransform(|schema: &mut Schema| {
schema.insert("my_property".to_string(), "hello world".into());
});
let mut schema = json_schema!({
"type": "array",
"items": {}
});
transform.transform(&mut schema);
assert_eq!(
schema,
json_schema!({
"type": "array",
"items": {
"my_property": "hello world"
},
"my_property": "hello world"
})
);
Structs§
- AddNullable
- Adds a
"nullable": true
property to schemas that allownull
types. - Recursive
Transform - A helper struct that can wrap a non-recursive
Transform
(i.e. one that does not apply to subschemas) into a recursive one. - Remove
RefSiblings - Restructures JSON Schema objects so that the
$ref
property will never appear alongside any other properties. - Replace
Bool Schemas - Replaces boolean JSON Schemas with equivalent object schemas.
- Replace
Const Value - Replaces the
const
schema property with a single-valuedenum
property. - Replace
Prefix Items - Rename the
prefixItems
schema property toitems
. - Replace
Unevaluated Properties - Replaces the
unevaluatedProperties
schema property with theadditionalProperties
property, adding properties from a schema’s subschemas to itsproperties
where necessary. - Restrict
Formats - Removes any
format
values that are not defined by the JSON Schema standard or explicitly allowed by a custom list. - SetSingle
Example - Removes the
examples
schema property and (if present) set its first value as theexample
property.
Traits§
- Transform
- Trait used to modify a constructed schema and optionally its subschemas.
Functions§
- transform_
subschemas - Applies the given
Transform
to all direct subschemas of theSchema
.