Add docs on generating schemas

This commit is contained in:
Graham Esau 2019-12-26 18:26:06 +00:00
parent ebb20130f7
commit 26346612b5
3 changed files with 21 additions and 2 deletions

View file

@ -7,4 +7,19 @@ permalink: /generating/
# 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
-->

View file

@ -2,6 +2,7 @@
permalink: /404
layout: default
nav_exclude: true
title: Not Found
---
<style type="text/css" media="screen">

View file

@ -2,6 +2,9 @@ use crate::schema::*;
use crate::{JsonSchema, Map};
/// 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)]
pub struct SchemaSettings {
/// If `true`, schemas for [`Option<T>`](Option) will include a `nullable` property.
@ -33,7 +36,7 @@ pub struct SchemaSettings {
pub enum BoolSchemas {
/// `Bool` schemas may be used.
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,
/// `Bool` schemas will never be used.
Disabled,