Add #[schemars(inner(...)] attribute to specify schema for array items (#234)
This commit is contained in:
parent
30e513ac14
commit
a5e51b22b3
11 changed files with 231 additions and 60 deletions
|
@ -10,6 +10,8 @@ pub struct MyStruct {
|
|||
pub my_bool: bool,
|
||||
#[schemars(default)]
|
||||
pub my_nullable_enum: Option<MyEnum>,
|
||||
#[schemars(inner(regex(pattern = "^x$")))]
|
||||
pub my_vec_str: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, JsonSchema)]
|
||||
|
|
|
@ -4,7 +4,8 @@
|
|||
"type": "object",
|
||||
"required": [
|
||||
"myBool",
|
||||
"myNumber"
|
||||
"myNumber",
|
||||
"myVecStr"
|
||||
],
|
||||
"properties": {
|
||||
"myBool": {
|
||||
|
@ -26,6 +27,13 @@
|
|||
"format": "int32",
|
||||
"maximum": 10.0,
|
||||
"minimum": 1.0
|
||||
},
|
||||
"myVecStr": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string",
|
||||
"pattern": "^x$"
|
||||
}
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
|
|
74
schemars/tests/expected/validate_inner.json
Normal file
74
schemars/tests/expected/validate_inner.json
Normal file
|
@ -0,0 +1,74 @@
|
|||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "Struct",
|
||||
"type": "object",
|
||||
"required": [
|
||||
"array_str_length",
|
||||
"slice_str_contains",
|
||||
"vec_i32_range",
|
||||
"vec_str_length",
|
||||
"vec_str_length2",
|
||||
"vec_str_regex",
|
||||
"vec_str_url"
|
||||
],
|
||||
"properties": {
|
||||
"array_str_length": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string",
|
||||
"maxLength": 100,
|
||||
"minLength": 5
|
||||
},
|
||||
"maxItems": 2,
|
||||
"minItems": 2
|
||||
},
|
||||
"slice_str_contains": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string",
|
||||
"pattern": "substring\\.\\.\\."
|
||||
}
|
||||
},
|
||||
"vec_i32_range": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "integer",
|
||||
"format": "int32",
|
||||
"maximum": 10.0,
|
||||
"minimum": -10.0
|
||||
}
|
||||
},
|
||||
"vec_str_length": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string",
|
||||
"maxLength": 100,
|
||||
"minLength": 1
|
||||
}
|
||||
},
|
||||
"vec_str_length2": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string",
|
||||
"maxLength": 100,
|
||||
"minLength": 1
|
||||
},
|
||||
"maxItems": 3,
|
||||
"minItems": 1
|
||||
},
|
||||
"vec_str_regex": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string",
|
||||
"pattern": "^[Hh]ello\\b"
|
||||
}
|
||||
},
|
||||
"vec_str_url": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string",
|
||||
"format": "uri"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "Struct",
|
||||
"title": "Struct2",
|
||||
"type": "object",
|
||||
"required": [
|
||||
"contains_str1",
|
||||
|
|
|
@ -101,7 +101,7 @@ pub struct Struct2 {
|
|||
|
||||
#[test]
|
||||
fn validate_schemars_attrs() -> TestResult {
|
||||
test_default_generated_schema::<Struct>("validate_schemars_attrs")
|
||||
test_default_generated_schema::<Struct2>("validate_schemars_attrs")
|
||||
}
|
||||
|
||||
#[derive(JsonSchema)]
|
||||
|
|
31
schemars/tests/validate_inner.rs
Normal file
31
schemars/tests/validate_inner.rs
Normal file
|
@ -0,0 +1,31 @@
|
|||
mod util;
|
||||
|
||||
use schemars::JsonSchema;
|
||||
use util::*;
|
||||
|
||||
// In real code, this would typically be a Regex, potentially created in a `lazy_static!`.
|
||||
static STARTS_WITH_HELLO: &str = r"^[Hh]ello\b";
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(JsonSchema)]
|
||||
pub struct Struct<'a> {
|
||||
#[schemars(inner(length(min = 5, max = 100)))]
|
||||
array_str_length: [&'a str; 2],
|
||||
#[schemars(inner(contains(pattern = "substring...")))]
|
||||
slice_str_contains: &'a[&'a str],
|
||||
#[schemars(inner(regex = "STARTS_WITH_HELLO"))]
|
||||
vec_str_regex: Vec<String>,
|
||||
#[schemars(inner(length(min = 1, max = 100)))]
|
||||
vec_str_length: Vec<&'a str>,
|
||||
#[schemars(length(min = 1, max = 3), inner(length(min = 1, max = 100)))]
|
||||
vec_str_length2: Vec<String>,
|
||||
#[schemars(inner(url))]
|
||||
vec_str_url: Vec<String>,
|
||||
#[schemars(inner(range(min = -10, max = 10)))]
|
||||
vec_i32_range: Vec<i32>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_inner() -> TestResult {
|
||||
test_default_generated_schema::<Struct>("validate_inner")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue