Update examples

This commit is contained in:
Graham Esau 2024-08-10 17:57:02 +01:00
parent 7bcd200a21
commit c61b26091e
6 changed files with 24 additions and 12 deletions

View file

@ -1,7 +1,7 @@
{ {
"$schema": "https://json-schema.org/draft/2020-12/schema", "$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "My Amazing Struct", "title": "My Amazing Struct",
"description": "This struct shows off generating a schema with\na custom title and description.", "description": "This struct shows off generating a schema with\n a custom title and description.",
"type": "object", "type": "object",
"properties": { "properties": {
"my_bool": { "my_bool": {
@ -48,7 +48,7 @@
] ]
}, },
{ {
"description": "A struct-like enum variant which contains\nsome floats", "description": "A struct-like enum variant which contains\n some floats",
"type": "object", "type": "object",
"properties": { "properties": {
"StructVariant": { "StructVariant": {

View file

@ -1,11 +1,11 @@
use schemars::{schema_for, JsonSchema}; use schemars::{schema_for, JsonSchema, Schema};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, JsonSchema)] #[derive(Deserialize, Serialize, JsonSchema)]
#[schemars(rename_all = "camelCase", deny_unknown_fields)] #[schemars(rename_all = "camelCase", deny_unknown_fields, extend("x-customProperty" = "example"))]
pub struct MyStruct { pub struct MyStruct {
#[serde(rename = "thisIsOverridden")] #[serde(rename = "thisIsOverridden")]
#[schemars(rename = "myNumber", range(min = 1, max = 10))] #[schemars(rename = "myNumber", range(min = 1, max = 10), transform = remove_format)]
pub my_int: i32, pub my_int: i32,
pub my_bool: bool, pub my_bool: bool,
#[schemars(default)] #[schemars(default)]
@ -24,6 +24,12 @@ pub enum MyEnum {
}, },
} }
fn remove_format(schema: &mut Schema) {
if let Some(obj) = schema.as_object_mut() {
obj.remove("format");
}
}
fn main() { fn main() {
let schema = schema_for!(MyStruct); let schema = schema_for!(MyStruct);
println!("{}", serde_json::to_string_pretty(&schema).unwrap()); println!("{}", serde_json::to_string_pretty(&schema).unwrap());

View file

@ -19,7 +19,6 @@
}, },
"myNumber": { "myNumber": {
"type": "integer", "type": "integer",
"format": "int32",
"maximum": 10, "maximum": 10,
"minimum": 1 "minimum": 1
}, },
@ -37,6 +36,7 @@
"myBool", "myBool",
"myVecStr" "myVecStr"
], ],
"x-customProperty": "example",
"$defs": { "$defs": {
"MyEnum": { "MyEnum": {
"anyOf": [ "anyOf": [

View file

@ -1,7 +1,7 @@
{ {
"$schema": "https://json-schema.org/draft/2020-12/schema", "$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "My Amazing Struct", "title": "My Amazing Struct",
"description": "This struct shows off generating a schema with\na custom title and description.", "description": "This struct shows off generating a schema with\n a custom title and description.",
"type": "object", "type": "object",
"properties": { "properties": {
"my_bool": { "my_bool": {
@ -48,7 +48,7 @@
] ]
}, },
{ {
"description": "A struct-like enum variant which contains\nsome floats", "description": "A struct-like enum variant which contains\n some floats",
"type": "object", "type": "object",
"properties": { "properties": {
"StructVariant": { "StructVariant": {

View file

@ -1,11 +1,11 @@
use schemars::{schema_for, JsonSchema}; use schemars::{schema_for, JsonSchema, Schema};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, JsonSchema)] #[derive(Deserialize, Serialize, JsonSchema)]
#[schemars(rename_all = "camelCase", deny_unknown_fields)] #[schemars(rename_all = "camelCase", deny_unknown_fields, extend("x-customProperty" = "example"))]
pub struct MyStruct { pub struct MyStruct {
#[serde(rename = "thisIsOverridden")] #[serde(rename = "thisIsOverridden")]
#[schemars(rename = "myNumber", range(min = 1, max = 10))] #[schemars(rename = "myNumber", range(min = 1, max = 10), transform = remove_format)]
pub my_int: i32, pub my_int: i32,
pub my_bool: bool, pub my_bool: bool,
#[schemars(default)] #[schemars(default)]
@ -24,6 +24,12 @@ pub enum MyEnum {
}, },
} }
fn remove_format(schema: &mut Schema) {
if let Some(obj) = schema.as_object_mut() {
obj.remove("format");
}
}
fn main() { fn main() {
let schema = schema_for!(MyStruct); let schema = schema_for!(MyStruct);
println!("{}", serde_json::to_string_pretty(&schema).unwrap()); println!("{}", serde_json::to_string_pretty(&schema).unwrap());

View file

@ -19,7 +19,6 @@
}, },
"myNumber": { "myNumber": {
"type": "integer", "type": "integer",
"format": "int32",
"maximum": 10, "maximum": 10,
"minimum": 1 "minimum": 1
}, },
@ -37,6 +36,7 @@
"myBool", "myBool",
"myVecStr" "myVecStr"
], ],
"x-customProperty": "example",
"$defs": { "$defs": {
"MyEnum": { "MyEnum": {
"anyOf": [ "anyOf": [