summaryrefslogtreecommitdiff
path: root/vendor/schemars/examples/validate.rs
blob: 70fa4a2b4ca5aec56dececd7b0634cfac4452f14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use schemars::{schema_for, JsonSchema};

#[derive(JsonSchema)]
pub struct MyStruct {
    #[validate(range(min = 1, max = 10))]
    pub my_int: i32,
    pub my_bool: bool,
    #[validate(required)]
    pub my_nullable_enum: Option<MyEnum>,
}

#[derive(JsonSchema)]
pub enum MyEnum {
    StringNewType(#[validate(email)] String),
    StructVariant {
        #[validate(length(min = 1, max = 100))]
        floats: Vec<f32>,
    },
}

fn main() {
    let schema = schema_for!(MyStruct);
    println!("{}", serde_json::to_string_pretty(&schema).unwrap());
}