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_subschemasfunction within the transform function - wrap the
Transformin 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": trueproperty to schemas that allownulltypes. - 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
$refproperty will never appear alongside any other properties. - Replace
Bool Schemas - Replaces boolean JSON Schemas with equivalent object schemas.
- Replace
Const Value - Replaces the
constschema property with a single-valuedenumproperty. - Replace
Prefix Items - Rename the
prefixItemsschema property toitems. - Replace
Unevaluated Properties - Replaces the
unevaluatedPropertiesschema property with theadditionalPropertiesproperty, adding properties from a schema’s subschemas to itspropertieswhere necessary. - Restrict
Formats - Removes any
formatvalues that are not defined by the JSON Schema standard or explicitly allowed by a custom list. - SetSingle
Example - Removes the
examplesschema property and (if present) set its first value as theexampleproperty.
Traits§
- Transform
- Trait used to modify a constructed schema and optionally its subschemas.
Functions§
- transform_
subschemas - Applies the given
Transformto all direct subschemas of theSchema.