Add docs on generating schemas
This commit is contained in:
parent
ebb20130f7
commit
26346612b5
3 changed files with 21 additions and 2 deletions
|
@ -7,4 +7,19 @@ permalink: /generating/
|
||||||
|
|
||||||
# Generating Schemas
|
# Generating Schemas
|
||||||
|
|
||||||
Using the `schema_for!` macro, and `gen` module...
|
The easiest way to generate a schema for a type that implements is to use the [`schema_for!` macro](https://docs.rs/schemars/latest/schemars/macro.schema_for.html), like so:
|
||||||
|
```rust
|
||||||
|
let my_schema = schema_for!(MyStruct);
|
||||||
|
```
|
||||||
|
|
||||||
|
This will create a schema that conforms to [JSON Schema Draft 7](https://json-schema.org/specification-links.html#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](https://docs.rs/schemars/latest/schemars/gen/). There are two main types in this module:
|
||||||
|
* [`SchemaSettings`](https://docs.rs/schemars/0.6.1/schemars/gen/struct.SchemaSettings.html), which defines what JSON Schema features should be used when generating schemas (for example, how `Option`s should be represented).
|
||||||
|
* [`SchemaGenerator`](https://docs.rs/schemars/0.6.1/schemars/gen/struct.SchemaGenerator.html), 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.
|
||||||
|
|
||||||
|
<!-- TODO:
|
||||||
|
create and link to example
|
||||||
|
-->
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
permalink: /404
|
permalink: /404
|
||||||
layout: default
|
layout: default
|
||||||
nav_exclude: true
|
nav_exclude: true
|
||||||
|
title: Not Found
|
||||||
---
|
---
|
||||||
|
|
||||||
<style type="text/css" media="screen">
|
<style type="text/css" media="screen">
|
||||||
|
|
|
@ -2,6 +2,9 @@ use crate::schema::*;
|
||||||
use crate::{JsonSchema, Map};
|
use crate::{JsonSchema, Map};
|
||||||
|
|
||||||
/// Settings to customize how Schemas are generated.
|
/// Settings to customize how Schemas are generated.
|
||||||
|
///
|
||||||
|
/// The default settings currently conform to [JSON Schema Draft 7](https://json-schema.org/specification-links.html#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 require your generated schemas to conform to draft 7, consider using the [`draft07`](#method.draft07) method.
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub struct SchemaSettings {
|
pub struct SchemaSettings {
|
||||||
/// If `true`, schemas for [`Option<T>`](Option) will include a `nullable` property.
|
/// If `true`, schemas for [`Option<T>`](Option) will include a `nullable` property.
|
||||||
|
@ -33,7 +36,7 @@ pub struct SchemaSettings {
|
||||||
pub enum BoolSchemas {
|
pub enum BoolSchemas {
|
||||||
/// `Bool` schemas may be used.
|
/// `Bool` schemas may be used.
|
||||||
Enabled,
|
Enabled,
|
||||||
/// `Bool` schemas may only be used in a schema's [`additionalProperties`](../sschema/struct.ObjectValidation.html#structfield.additional_properties) field.
|
/// `Bool` schemas may only be used in a schema's [`additionalProperties`](../schema/struct.ObjectValidation.html#structfield.additional_properties) field.
|
||||||
AdditionalPropertiesOnly,
|
AdditionalPropertiesOnly,
|
||||||
/// `Bool` schemas will never be used.
|
/// `Bool` schemas will never be used.
|
||||||
Disabled,
|
Disabled,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue