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",
"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",
"properties": {
"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",
"properties": {
"StructVariant": {

View file

@ -1,11 +1,11 @@
use schemars::{schema_for, JsonSchema};
use schemars::{schema_for, JsonSchema, Schema};
use serde::{Deserialize, Serialize};
#[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 {
#[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_bool: bool,
#[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() {
let schema = schema_for!(MyStruct);
println!("{}", serde_json::to_string_pretty(&schema).unwrap());

View file

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

View file

@ -1,7 +1,7 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"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",
"properties": {
"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",
"properties": {
"StructVariant": {

View file

@ -1,11 +1,11 @@
use schemars::{schema_for, JsonSchema};
use schemars::{schema_for, JsonSchema, Schema};
use serde::{Deserialize, Serialize};
#[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 {
#[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_bool: bool,
#[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() {
let schema = schema_for!(MyStruct);
println!("{}", serde_json::to_string_pretty(&schema).unwrap());

View file

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