Add more info to README
This commit is contained in:
parent
5c307b92fb
commit
792fbbb86e
7 changed files with 182 additions and 91 deletions
19
schemars/examples/main.rs
Normal file
19
schemars/examples/main.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
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 {
|
||||
Unit,
|
||||
StringNewType(String)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let schema = schema_for!(MyStruct);
|
||||
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
||||
}
|
24
schemars/examples/serde_attrs.rs
Normal file
24
schemars/examples/serde_attrs.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
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 {
|
||||
Unit,
|
||||
StringNewType(String)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let schema = schema_for!(MyStruct);
|
||||
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
use schemars::{schema_for, JsonSchema};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Deserialize, Serialize, JsonSchema)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct MyStruct {
|
||||
my_int: i32,
|
||||
my_nullable: Option<bool>,
|
||||
my_nested_struct: Nested,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, JsonSchema)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct Nested {
|
||||
#[serde(default)]
|
||||
my_string: String,
|
||||
#[serde(rename = "myArray")]
|
||||
my_float_vec: Vec<f32>,
|
||||
my_recursive_struct: Option<Box<Nested>>,
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let schema = schema_for!(MyStruct);
|
||||
println!("{}", serde_json::to_string_pretty(&schema)?);
|
||||
Ok(())
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
use crate::schema::*;
|
||||
use crate::{JsonSchema, Map};
|
||||
|
||||
/// Settings to customize how a Schema is generated.
|
||||
/// Settings to customize how Schemas are generated.
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct SchemaSettings {
|
||||
/// If `true`, schemas for [`Option<T>`](Option) will include a `nullable` property.
|
||||
|
@ -37,16 +37,11 @@ pub enum BoolSchemas {
|
|||
|
||||
impl Default for SchemaSettings {
|
||||
fn default() -> SchemaSettings {
|
||||
SchemaSettings::new()
|
||||
SchemaSettings::draft07()
|
||||
}
|
||||
}
|
||||
|
||||
impl SchemaSettings {
|
||||
/// Creates `SchemaSettings` that conform to [JSON Schema Draft 7](https://json-schema.org/specification-links.html#draft-7).
|
||||
pub fn new() -> SchemaSettings {
|
||||
Self::draft07()
|
||||
}
|
||||
|
||||
/// Creates `SchemaSettings` that conform to [JSON Schema Draft 7](https://json-schema.org/specification-links.html#draft-7).
|
||||
pub fn draft07() -> SchemaSettings {
|
||||
SchemaSettings {
|
||||
|
@ -57,7 +52,7 @@ impl SchemaSettings {
|
|||
}
|
||||
}
|
||||
|
||||
/// Creates `SchemaSettings` that conform to [OpenAPI 3](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#schemaObject).
|
||||
/// Creates `SchemaSettings` that conform to [OpenAPI 3.0](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#schemaObject).
|
||||
pub fn openapi3() -> SchemaSettings {
|
||||
SchemaSettings {
|
||||
option_nullable: true,
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#[cfg(feature = "derive_json_schema")]
|
||||
use crate as schemars;
|
||||
#[cfg(feature = "derive_json_schema")]
|
||||
use crate::JsonSchema;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue