Module schemars::visit

source ·
Expand description

Contains the Visitor trait, used to recursively modify a constructed schema and its subschemas.

Sometimes you may want to apply a change to a schema, as well as all schemas contained within it. The easiest way to achieve this is by defining a type that implements Visitor. All methods of Visitor have a default implementation that makes no change but recursively visits all subschemas. When overriding one of these methods, you will usually want to still call this default implementation.

§Example

To add a custom property to all schemas:

use schemars::schema::SchemaObject;
use schemars::visit::{Visitor, visit_schema_object};

pub struct MyVisitor;

impl Visitor for MyVisitor {
    fn visit_schema_object(&mut self, schema: &mut SchemaObject) {
        // First, make our change to this schema
        schema.extensions.insert("my_property".to_string(), serde_json::json!("hello world"));

        // Then delegate to default implementation to visit any subschemas
        visit_schema_object(self, schema);
    }
}

Structs§

  • This visitor will restructure JSON Schema objects so that the $ref property will never appear alongside any other properties.
  • This visitor will replace all boolean JSON Schemas with equivalent object schemas.
  • This visitor will remove the examples schema property and (if present) set its first value as the example property.

Traits§

  • Trait used to recursively modify a constructed schema and its subschemas.

Functions§