Update examples

This commit is contained in:
Graham Esau 2019-12-26 20:39:18 +00:00
parent 26346612b5
commit d67abbdbb1
7 changed files with 311 additions and 107 deletions

211
README.md
View file

@ -22,9 +22,11 @@ pub struct MyStruct {
}
#[derive(JsonSchema)]
pub enum MyEnum {
Unit,
StringNewType(String)
pub enum MyEnum {
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>
@ -111,16 +131,17 @@ pub struct MyStruct {
#[derive(Deserialize, Serialize, JsonSchema)]
#[serde(untagged)]
pub enum MyEnum {
Unit,
StringNewType(String)
pub enum MyEnum {
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>

View file

@ -8,9 +8,11 @@ pub struct MyStruct {
}
#[derive(JsonSchema)]
pub enum MyEnum {
Unit,
pub enum MyEnum {
StringNewType(String),
StructVariant {
floats: Vec<f32>,
}
}
fn main() {

View 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"
}
}
}
}
}
}
]
}
}
}

View file

@ -13,9 +13,11 @@ pub struct MyStruct {
#[derive(Deserialize, Serialize, JsonSchema)]
#[serde(untagged)]
pub enum MyEnum {
Unit,
pub enum MyEnum {
StringNewType(String),
StructVariant {
floats: Vec<f32>,
}
}
fn main() {

View 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"
}
}
}
}
]
}
}
}

View file

@ -16,9 +16,11 @@ pub struct MyStruct {
}
#[derive(JsonSchema)]
pub enum MyEnum {
Unit,
StringNewType(String)
pub enum MyEnum {
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"
}
}
}
}
}
}
]
}
@ -105,16 +125,17 @@ pub struct MyStruct {
#[derive(Deserialize, Serialize, JsonSchema)]
#[serde(untagged)]
pub enum MyEnum {
Unit,
StringNewType(String)
pub enum MyEnum {
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
View 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