macro_rules! schema_for_value {
($value:expr) => { ... };
}
Expand description
Generates a Schema
for the given example value using default settings.
The default settings currently conform to JSON Schema 2020-12, but this is liable to change in a future version of Schemars if support for other JSON Schema versions is added.
The value must implement Serialize
. If the value also implements
JsonSchema
, then prefer using the schema_for!(Type)
macro
which will generally produce a more precise schema, particularly when the value contains any
enums.
If the Serialize
implementation of the value decides to fail, this macro will panic.
For a non-panicking alternative, create a SchemaGenerator
and use
its into_root_schema_for_value
method.
ยงExample
use schemars::schema_for_value;
#[derive(serde::Serialize)]
struct MyStruct {
foo: i32,
}
let my_schema = schema_for_value!(MyStruct { foo: 123 });