Update examples
This commit is contained in:
parent
26346612b5
commit
d67abbdbb1
7 changed files with 311 additions and 107 deletions
207
README.md
207
README.md
|
@ -23,8 +23,10 @@ pub struct MyStruct {
|
|||
|
||||
#[derive(JsonSchema)]
|
||||
pub enum MyEnum {
|
||||
Unit,
|
||||
StringNewType(String)
|
||||
StringNewType(String),
|
||||
StructVariant {
|
||||
floats: Vec<f32>,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
@ -38,55 +40,73 @@ fn main() {
|
|||
|
||||
```json
|
||||
{
|
||||
"$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"
|
||||
}
|
||||
]
|
||||
}
|
||||
"$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"
|
||||
},
|
||||
"definitions": {
|
||||
"MyEnum": {
|
||||
"anyOf": [
|
||||
{
|
||||
"enum": [
|
||||
"Unit"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"required": [
|
||||
"StringNewType"
|
||||
],
|
||||
"properties": {
|
||||
"StringNewType": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
"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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
</details>
|
||||
|
@ -112,15 +132,16 @@ pub struct MyStruct {
|
|||
#[derive(Deserialize, Serialize, JsonSchema)]
|
||||
#[serde(untagged)]
|
||||
pub enum MyEnum {
|
||||
Unit,
|
||||
StringNewType(String)
|
||||
StringNewType(String),
|
||||
StructVariant {
|
||||
floats: Vec<f32>,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let schema = schema_for!(MyStruct);
|
||||
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
<details>
|
||||
|
@ -128,45 +149,57 @@ fn main() {
|
|||
|
||||
```json
|
||||
{
|
||||
"$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"
|
||||
}
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "MyStruct",
|
||||
"type": "object",
|
||||
"required": [
|
||||
"myBool",
|
||||
"myNumber"
|
||||
],
|
||||
"properties": {
|
||||
"myBool": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"definitions": {
|
||||
"MyEnum": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "null"
|
||||
},
|
||||
{
|
||||
"type": "string"
|
||||
}
|
||||
]
|
||||
"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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
</details>
|
||||
|
|
|
@ -9,8 +9,10 @@ pub struct MyStruct {
|
|||
|
||||
#[derive(JsonSchema)]
|
||||
pub enum MyEnum {
|
||||
Unit,
|
||||
StringNewType(String),
|
||||
StructVariant {
|
||||
floats: Vec<f32>,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
69
schemars/examples/main.schema.json
Normal file
69
schemars/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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
|
@ -14,8 +14,10 @@ pub struct MyStruct {
|
|||
#[derive(Deserialize, Serialize, JsonSchema)]
|
||||
#[serde(untagged)]
|
||||
pub enum MyEnum {
|
||||
Unit,
|
||||
StringNewType(String),
|
||||
StructVariant {
|
||||
floats: Vec<f32>,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
53
schemars/examples/serde_attrs.schema.json
Normal file
53
schemars/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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
|
@ -17,8 +17,10 @@ pub struct MyStruct {
|
|||
|
||||
#[derive(JsonSchema)]
|
||||
pub enum MyEnum {
|
||||
Unit,
|
||||
StringNewType(String)
|
||||
StringNewType(String),
|
||||
StructVariant {
|
||||
floats: Vec<f32>,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
@ -62,11 +64,6 @@ fn main() {
|
|||
"definitions": {
|
||||
"MyEnum": {
|
||||
"anyOf": [
|
||||
{
|
||||
"enum": [
|
||||
"Unit"
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"required": [
|
||||
|
@ -77,6 +74,29 @@ fn main() {
|
|||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"required": [
|
||||
"StructVariant"
|
||||
],
|
||||
"properties": {
|
||||
"StructVariant": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"floats"
|
||||
],
|
||||
"properties": {
|
||||
"floats": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "number",
|
||||
"format": "float"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -106,15 +126,16 @@ pub struct MyStruct {
|
|||
#[derive(Deserialize, Serialize, JsonSchema)]
|
||||
#[serde(untagged)]
|
||||
pub enum MyEnum {
|
||||
Unit,
|
||||
StringNewType(String)
|
||||
StringNewType(String),
|
||||
StructVariant {
|
||||
floats: Vec<f32>,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let schema = schema_for!(MyStruct);
|
||||
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
<details>
|
||||
|
@ -153,10 +174,22 @@ fn main() {
|
|||
"MyEnum": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "null"
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"type": "string"
|
||||
"type": "object",
|
||||
"required": [
|
||||
"floats"
|
||||
],
|
||||
"properties": {
|
||||
"floats": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "number",
|
||||
"format": "float"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
12
update-examples.sh
Normal file
12
update-examples.sh
Normal file
|
@ -0,0 +1,12 @@
|
|||
#!/bin/bash
|
||||
set -euxo pipefail
|
||||
|
||||
cd schemars/examples
|
||||
|
||||
rm -f *.schema.json
|
||||
|
||||
for file in *.rs
|
||||
do
|
||||
example=${file%.rs}
|
||||
cargo run --example "$example" > "$example.schema.json"
|
||||
done
|
Loading…
Add table
Add a link
Reference in a new issue