Allow setting validation attributes via #[schemars(...)]
This commit is contained in:
parent
c013052f59
commit
7914593d89
17 changed files with 607 additions and 99 deletions
24
docs/_includes/examples/validate.rs
Normal file
24
docs/_includes/examples/validate.rs
Normal file
|
@ -0,0 +1,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(phone)] 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());
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue