Add separate docs for v0.8/v1
This commit is contained in:
parent
3150f98fc8
commit
d511d447f7
61 changed files with 1620 additions and 58 deletions
12
docs/_v0/examples/1-derive_jsonschema.md
Normal file
12
docs/_v0/examples/1-derive_jsonschema.md
Normal file
|
@ -0,0 +1,12 @@
|
|||
---
|
||||
title: Deriving JsonSchema
|
||||
parent: Examples
|
||||
nav_order: 1
|
||||
summary: Deriving JsonSchema on a struct and enum.
|
||||
---
|
||||
|
||||
# Deriving JsonSchema
|
||||
|
||||
This is the simplest usage of Schemars. Both types are made to derive `JsonSchema`, and the `schema_for!` macro is used to generate the schema itself.
|
||||
|
||||
{% include example_v0.md name="main" %}
|
14
docs/_v0/examples/2-serde_attrs.md
Normal file
14
docs/_v0/examples/2-serde_attrs.md
Normal file
|
@ -0,0 +1,14 @@
|
|||
---
|
||||
title: Using Serde Attributes
|
||||
parent: Examples
|
||||
nav_order: 2
|
||||
summary: "Deriving JsonSchema on types that use #[serde] attributes to customise serialization behaviour."
|
||||
---
|
||||
|
||||
# Using Serde Attributes
|
||||
|
||||
One of the main aims of this library is compatibility with [Serde](https://github.com/serde-rs/serde). Any generated schema _should_ match how [serde_json](https://github.com/serde-rs/json) would serialize/deserialize to/from JSON. To support this, Schemars will check for any `#[serde(...)]` attributes on types that derive `JsonSchema`, and adjust the generated schema accordingly.
|
||||
|
||||
The list of supported `#[serde]` attributes are [documented here]({{ site.baseurl }}{% link 1.1-attributes.md %}#supported-serde-attributes).
|
||||
|
||||
{% include example_v0.md name="serde_attrs" %}
|
12
docs/_v0/examples/3-schemars_attrs.md
Normal file
12
docs/_v0/examples/3-schemars_attrs.md
Normal file
|
@ -0,0 +1,12 @@
|
|||
---
|
||||
title: Using Schemars Attributes
|
||||
parent: Examples
|
||||
nav_order: 3
|
||||
summary: "Deriving JsonSchema on types that use #[schemars] attributes to customise serialization behaviour."
|
||||
---
|
||||
|
||||
# Using Serde Attributes
|
||||
|
||||
`#[serde(...)]` attributes can be overriden (or replaced) with `#[schemars(...)]` attributes, which behave identically. You may find this useful if you want to change the generated schema without affecting Serde's behaviour, or if you're just not using Serde.
|
||||
|
||||
{% include example_v0.md name="schemars_attrs" %}
|
12
docs/_v0/examples/4-custom_settings.md
Normal file
12
docs/_v0/examples/4-custom_settings.md
Normal file
|
@ -0,0 +1,12 @@
|
|||
---
|
||||
title: Custom Schema Settings
|
||||
parent: Examples
|
||||
nav_order: 4
|
||||
summary: Generating a schema using custom settings which changes how Option<T> is handled.
|
||||
---
|
||||
|
||||
# Custom Schema Settings
|
||||
|
||||
The `gen` module allows you to customise how schemas are generated. For example, the default behaviour for `Option<T>` is to include `null` in the schema's `type`s, but we can instead add a `nullable` property to its schema:
|
||||
|
||||
{% include example_v0.md name="custom_settings" %}
|
16
docs/_v0/examples/5-remote_derive.md
Normal file
16
docs/_v0/examples/5-remote_derive.md
Normal file
|
@ -0,0 +1,16 @@
|
|||
---
|
||||
title: Derive for Remote Crate
|
||||
parent: Examples
|
||||
nav_order: 5
|
||||
summary: Deriving JsonSchema implementations for a type in somebody else's crate.
|
||||
---
|
||||
|
||||
# Deriving JsonSchema for a Type in a Different Crate
|
||||
|
||||
Rust's [orphan rule](https://doc.rust-lang.org/book/traits.html#rules-for-implementing-traits) requires that either the trait or the type for which you are implementing the trait must be defined in the same crate as the impl, so it is not possible to implement `JsonSchema` for a type in a different crate directly.
|
||||
|
||||
To work around this, Schemars provides a way of deriving `JsonSchema` implementations for types in other people's crates. The only catch is that you have to provide a definition of the type for Schemars's derive to process.
|
||||
|
||||
This is the same way that Serde allows remote deriving, which is why this page reads so similarly to [Serde's documentation](https://serde.rs/remote-derive.html)!
|
||||
|
||||
{% include example_v0.md name="remote_derive" %}
|
12
docs/_v0/examples/6-doc_comments.md
Normal file
12
docs/_v0/examples/6-doc_comments.md
Normal file
|
@ -0,0 +1,12 @@
|
|||
---
|
||||
title: Doc Comments
|
||||
parent: Examples
|
||||
nav_order: 6
|
||||
summary: Giving schemas a custom title and/or description using doc comments.
|
||||
---
|
||||
|
||||
# Setting a Custom Title and/or Description Using Doc Comments
|
||||
|
||||
If a struct, variant or field has any [doc comments](https://doc.rust-lang.org/stable/rust-by-example/meta/doc.html#doc-comments) (or [`doc` attributes](https://doc.rust-lang.org/rustdoc/the-doc-attribute.html)), then these will be used as the generated schema's `description`. If the first line is an ATX-style markdown heading (i.e. it begins with a # character), then it will be used as the schema's `title`, and the remaining lines will be the `description`.
|
||||
|
||||
{% include example_v0.md name="doc_comments" %}
|
19
docs/_v0/examples/7-custom_serialization.md
Normal file
19
docs/_v0/examples/7-custom_serialization.md
Normal file
|
@ -0,0 +1,19 @@
|
|||
---
|
||||
title: Custom Serialization
|
||||
parent: Examples
|
||||
nav_order: 7
|
||||
summary: >-
|
||||
If a field has a #[serde(with = "path")] attribute where "path" is not a type that implements JsonSchema,
|
||||
then in order to derive JsonSchema on the type, it must also have a #[schemars(with = "Type")] attribute,
|
||||
where "Type" implements JsonSchema.
|
||||
---
|
||||
|
||||
# Deriving JsonSchema with Fields Using Custom Serialization
|
||||
|
||||
Serde allows you to change how a field is (de)serialized by setting a [`#[serde(with = "path")]`](https://serde.rs/field-attrs.html#with) attribute, where `$path::serialize` and `$path::deserialize` must be functions with the correct signature. Schemars supports the same attribute, but `path` must be a type implementing `JsonSchema`.
|
||||
|
||||
In order to derive `JsonSchema` on a type which includes a `#[serde(with = "path")]` attribute where `path` is not a type implementing `JsonSchema`, you'll need to override it with a suitable `#[schemars(with = "Type")]` or `#[schemars(schema_with = "path")]` attribute.
|
||||
|
||||
{% include example_v0.md name="custom_serialization" %}
|
||||
|
||||
Note that the `default` values in the schema are serialized as strings where appropriate.
|
13
docs/_v0/examples/8-enum_repr.md
Normal file
13
docs/_v0/examples/8-enum_repr.md
Normal file
|
@ -0,0 +1,13 @@
|
|||
---
|
||||
title: Serialize Enum as Number (serde_repr)
|
||||
parent: Examples
|
||||
nav_order: 8
|
||||
summary: >-
|
||||
Generating a schema for with a C-like enum compatible with serde_repr.
|
||||
---
|
||||
|
||||
# Serialize Enum as Number (serde_repr Compatibility)
|
||||
|
||||
If you use the `#[repr(...)]` attribute on an enum to give it a C-like representation, then you may also want to use the [serde_repr](https://github.com/dtolnay/serde-repr) crate to serialize the enum values as numbers. In this case, you should use the corresponding `JsonSchema_repr` derive to ensure the schema for your type reflects how serde formats your type.
|
||||
|
||||
{% include example_v0.md name="enum_repr" %}
|
15
docs/_v0/examples/9-from_value.md
Normal file
15
docs/_v0/examples/9-from_value.md
Normal file
|
@ -0,0 +1,15 @@
|
|||
---
|
||||
title: Generate Schema from Example Value
|
||||
parent: Examples
|
||||
nav_order: 9
|
||||
summary: >-
|
||||
Generating a schema for a serializable value.
|
||||
---
|
||||
|
||||
# Generate Schema from Example Value
|
||||
|
||||
If you want a schema for a type that can't/doesn't implement `JsonSchema`, but does implement [`serde::Serialize`](https://docs.serde.rs/serde/trait.Serialize.html), then you can generate a JSON schema from a value of that type. 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.
|
||||
|
||||
{% include example_v0.md name="from_value" %}
|
||||
|
||||
Note that the schema for the enum is not very useful in this case, since schemars doesn't know anything about the second variant.
|
Loading…
Add table
Add a link
Reference in a new issue