This page is for the current stable release of Schemars (v0.8.x).
To view this page for the v1 version of Schemars, which is still under development, click here.
Generating Schemas
The easiest way to generate a schema for a type that implements is to use the schema_for!
macro, like so:
let my_schema = schema_for!(MyStruct);
This will create a schema that conforms to JSON Schema Draft 7, but this is liable to change in a future version of Schemars if support for other JSON Schema versions is added.
If you want more control over how the schema is generated, you can use the gen
module. There are two main types in this module:
SchemaSettings
, which defines what JSON Schema features should be used when generating schemas (for example, howOption
s should be represented).SchemaGenerator
, which manages the generation of a schema document.
See the API documentation for more info on how to use those types for custom schema generation.
Schema from Example Value
If you want a schema for a type that can't/doesn't implement JsonSchema
, but does implement serde::Serialize
, then you can generate a JSON schema from a value of that type using the schema_for_value!
macro. However, this schema will generally be less precise than if the type implemented JsonSchema
- particularly when it involves enums, since schemars will not make any assumptions about the structure of an enum based on a single variant.
let value = MyStruct { foo = 123 };
let my_schema = schema_for_value!(value);