Add examples to docs
This commit is contained in:
parent
d67abbdbb1
commit
f387a0ec56
11 changed files with 223 additions and 17 deletions
14
docs/_includes/example.md
Normal file
14
docs/_includes/example.md
Normal file
|
@ -0,0 +1,14 @@
|
|||
{% capture input %}examples/{{ include.name }}.rs{% endcapture %}
|
||||
{% capture output %}examples/{{ include.name }}.schema.json{% endcapture %}
|
||||
|
||||
```rust
|
||||
{% include {{ input }} %}
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Click to see the output JSON schema...</summary>
|
||||
|
||||
```json
|
||||
{% include {{ output }} %}
|
||||
```
|
||||
</details>
|
21
docs/_includes/examples/main.rs
Normal file
21
docs/_includes/examples/main.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
use schemars::{schema_for, JsonSchema};
|
||||
|
||||
#[derive(JsonSchema)]
|
||||
pub struct MyStruct {
|
||||
pub my_int: i32,
|
||||
pub my_bool: bool,
|
||||
pub my_nullable_enum: Option<MyEnum>,
|
||||
}
|
||||
|
||||
#[derive(JsonSchema)]
|
||||
pub enum MyEnum {
|
||||
StringNewType(String),
|
||||
StructVariant {
|
||||
floats: Vec<f32>,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let schema = schema_for!(MyStruct);
|
||||
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
||||
}
|
69
docs/_includes/examples/main.schema.json
Normal file
69
docs/_includes/examples/main.schema.json
Normal file
|
@ -0,0 +1,69 @@
|
|||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "MyStruct",
|
||||
"type": "object",
|
||||
"required": [
|
||||
"my_bool",
|
||||
"my_int",
|
||||
"my_nullable_enum"
|
||||
],
|
||||
"properties": {
|
||||
"my_bool": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"my_int": {
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
},
|
||||
"my_nullable_enum": {
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/definitions/MyEnum"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"definitions": {
|
||||
"MyEnum": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "object",
|
||||
"required": [
|
||||
"StringNewType"
|
||||
],
|
||||
"properties": {
|
||||
"StringNewType": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"required": [
|
||||
"StructVariant"
|
||||
],
|
||||
"properties": {
|
||||
"StructVariant": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"floats"
|
||||
],
|
||||
"properties": {
|
||||
"floats": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "number",
|
||||
"format": "float"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
26
docs/_includes/examples/serde_attrs.rs
Normal file
26
docs/_includes/examples/serde_attrs.rs
Normal file
|
@ -0,0 +1,26 @@
|
|||
use schemars::{schema_for, JsonSchema};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Deserialize, Serialize, JsonSchema)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct MyStruct {
|
||||
#[serde(rename = "myNumber")]
|
||||
pub my_int: i32,
|
||||
pub my_bool: bool,
|
||||
#[serde(default)]
|
||||
pub my_nullable_enum: Option<MyEnum>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, JsonSchema)]
|
||||
#[serde(untagged)]
|
||||
pub enum MyEnum {
|
||||
StringNewType(String),
|
||||
StructVariant {
|
||||
floats: Vec<f32>,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let schema = schema_for!(MyStruct);
|
||||
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
||||
}
|
53
docs/_includes/examples/serde_attrs.schema.json
Normal file
53
docs/_includes/examples/serde_attrs.schema.json
Normal file
|
@ -0,0 +1,53 @@
|
|||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "MyStruct",
|
||||
"type": "object",
|
||||
"required": [
|
||||
"myBool",
|
||||
"myNumber"
|
||||
],
|
||||
"properties": {
|
||||
"myBool": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"myNullableEnum": {
|
||||
"default": null,
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/definitions/MyEnum"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"myNumber": {
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
}
|
||||
},
|
||||
"definitions": {
|
||||
"MyEnum": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"required": [
|
||||
"floats"
|
||||
],
|
||||
"properties": {
|
||||
"floats": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "number",
|
||||
"format": "float"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue