| 1 | use schemars::{schema_for, JsonSchema, Schema}; |
| 2 | use serde::{Deserialize, Serialize}; |
| 3 | |
| 4 | #[derive(Deserialize, Serialize, JsonSchema)] |
| 5 | #[schemars(rename_all = "camelCase", deny_unknown_fields, extend("x-customProperty" = "example"))] |
| 6 | pub struct MyStruct { |
| 7 | #[serde(rename = "thisIsOverridden")] |
| 8 | #[schemars(rename = "myNumber", range(min = 1, max = 10), transform = remove_format)] |
| 9 | pub my_int: i32, |
| 10 | pub my_bool: bool, |
| 11 | #[schemars(default)] |
| 12 | pub my_nullable_enum: Option<MyEnum>, |
| 13 | #[schemars(inner(regex(pattern = "^x$")))] |
| 14 | pub my_vec_str: Vec<String>, |
| 15 | } |
| 16 | |
| 17 | #[derive(Deserialize, Serialize, JsonSchema)] |
| 18 | #[schemars(untagged)] |
| 19 | pub enum MyEnum { |
| 20 | StringNewType(#[schemars(email)] String), |
| 21 | StructVariant { |
| 22 | #[schemars(length(min = 1, max = 100))] |
| 23 | floats: Vec<f32>, |
| 24 | }, |
| 25 | } |
| 26 | |
| 27 | fn remove_format(schema: &mut Schema) { |
| 28 | schema.remove("format"); |
| 29 | } |
| 30 | |
| 31 | fn main() { |
| 32 | let schema = schema_for!(MyStruct); |
| 33 | println!("{}", serde_json::to_string_pretty(&schema).unwrap()); |
| 34 | } |