Read #[garde(...)]
attributes in addition to #[validate(...)]
(#331)
This commit is contained in:
parent
56cdd45c5a
commit
9770301218
16 changed files with 421 additions and 87 deletions
74
schemars/tests/expected/garde.json
Normal file
74
schemars/tests/expected/garde.json
Normal file
|
@ -0,0 +1,74 @@
|
|||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"title": "Struct",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"min_max": {
|
||||
"type": "number",
|
||||
"format": "float",
|
||||
"minimum": 0.01,
|
||||
"maximum": 100
|
||||
},
|
||||
"min_max2": {
|
||||
"type": "number",
|
||||
"format": "float",
|
||||
"minimum": 1,
|
||||
"maximum": 1000
|
||||
},
|
||||
"regex_str1": {
|
||||
"type": "string",
|
||||
"pattern": "^[Hh]ello\\b"
|
||||
},
|
||||
"contains_str1": {
|
||||
"type": "string",
|
||||
"pattern": "substring\\.\\.\\."
|
||||
},
|
||||
"email_address": {
|
||||
"type": "string",
|
||||
"format": "email"
|
||||
},
|
||||
"homepage": {
|
||||
"type": "string",
|
||||
"format": "uri"
|
||||
},
|
||||
"non_empty_str": {
|
||||
"type": "string",
|
||||
"minLength": 1,
|
||||
"maxLength": 100
|
||||
},
|
||||
"non_empty_str2": {
|
||||
"type": "string",
|
||||
"minLength": 1,
|
||||
"maxLength": 1000
|
||||
},
|
||||
"pair": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
},
|
||||
"minItems": 2,
|
||||
"maxItems": 2
|
||||
},
|
||||
"required_option": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"x": {
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"min_max",
|
||||
"min_max2",
|
||||
"regex_str1",
|
||||
"contains_str1",
|
||||
"email_address",
|
||||
"homepage",
|
||||
"non_empty_str",
|
||||
"non_empty_str2",
|
||||
"pair",
|
||||
"required_option",
|
||||
"x"
|
||||
]
|
||||
}
|
8
schemars/tests/expected/garde_newtype.json
Normal file
8
schemars/tests/expected/garde_newtype.json
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"title": "NewType",
|
||||
"type": "integer",
|
||||
"format": "uint8",
|
||||
"minimum": 0,
|
||||
"maximum": 10
|
||||
}
|
74
schemars/tests/expected/garde_schemars_attrs.json
Normal file
74
schemars/tests/expected/garde_schemars_attrs.json
Normal file
|
@ -0,0 +1,74 @@
|
|||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"title": "Struct2",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"min_max": {
|
||||
"type": "number",
|
||||
"format": "float",
|
||||
"minimum": 0.01,
|
||||
"maximum": 100
|
||||
},
|
||||
"min_max2": {
|
||||
"type": "number",
|
||||
"format": "float",
|
||||
"minimum": 1,
|
||||
"maximum": 1000
|
||||
},
|
||||
"regex_str1": {
|
||||
"type": "string",
|
||||
"pattern": "^[Hh]ello\\b"
|
||||
},
|
||||
"contains_str1": {
|
||||
"type": "string",
|
||||
"pattern": "substring\\.\\.\\."
|
||||
},
|
||||
"email_address": {
|
||||
"type": "string",
|
||||
"format": "email"
|
||||
},
|
||||
"homepage": {
|
||||
"type": "string",
|
||||
"format": "uri"
|
||||
},
|
||||
"non_empty_str": {
|
||||
"type": "string",
|
||||
"minLength": 1,
|
||||
"maxLength": 100
|
||||
},
|
||||
"non_empty_str2": {
|
||||
"type": "string",
|
||||
"minLength": 1,
|
||||
"maxLength": 1000
|
||||
},
|
||||
"pair": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
},
|
||||
"minItems": 2,
|
||||
"maxItems": 2
|
||||
},
|
||||
"required_option": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"x": {
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"min_max",
|
||||
"min_max2",
|
||||
"regex_str1",
|
||||
"contains_str1",
|
||||
"email_address",
|
||||
"homepage",
|
||||
"non_empty_str",
|
||||
"non_empty_str2",
|
||||
"pair",
|
||||
"required_option",
|
||||
"x"
|
||||
]
|
||||
}
|
18
schemars/tests/expected/garde_tuple.json
Normal file
18
schemars/tests/expected/garde_tuple.json
Normal file
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"title": "Tuple",
|
||||
"type": "array",
|
||||
"prefixItems": [
|
||||
{
|
||||
"type": "integer",
|
||||
"format": "uint8",
|
||||
"minimum": 0,
|
||||
"maximum": 10
|
||||
},
|
||||
{
|
||||
"type": "boolean"
|
||||
}
|
||||
],
|
||||
"minItems": 2,
|
||||
"maxItems": 2
|
||||
}
|
|
@ -58,15 +58,6 @@
|
|||
"minItems": 2,
|
||||
"maxItems": 2
|
||||
},
|
||||
"map_contains": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "null"
|
||||
},
|
||||
"required": [
|
||||
"map_key"
|
||||
]
|
||||
},
|
||||
"required_option": {
|
||||
"type": "boolean"
|
||||
},
|
||||
|
@ -87,7 +78,6 @@
|
|||
"non_empty_str",
|
||||
"non_empty_str2",
|
||||
"pair",
|
||||
"map_contains",
|
||||
"required_option",
|
||||
"x"
|
||||
]
|
||||
|
|
|
@ -58,15 +58,6 @@
|
|||
"minItems": 2,
|
||||
"maxItems": 2
|
||||
},
|
||||
"map_contains": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "null"
|
||||
},
|
||||
"required": [
|
||||
"map_key"
|
||||
]
|
||||
},
|
||||
"required_option": {
|
||||
"type": "boolean"
|
||||
},
|
||||
|
@ -87,7 +78,6 @@
|
|||
"non_empty_str",
|
||||
"non_empty_str2",
|
||||
"pair",
|
||||
"map_contains",
|
||||
"required_option",
|
||||
"x"
|
||||
]
|
||||
|
|
99
schemars/tests/garde.rs
Normal file
99
schemars/tests/garde.rs
Normal file
|
@ -0,0 +1,99 @@
|
|||
mod util;
|
||||
use schemars::JsonSchema;
|
||||
use util::*;
|
||||
|
||||
const MIN: u32 = 1;
|
||||
const MAX: u32 = 1000;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(JsonSchema)]
|
||||
pub struct Struct {
|
||||
#[garde(range(min = 0.01, max = 100))]
|
||||
min_max: f32,
|
||||
#[garde(range(min = MIN, max = MAX))]
|
||||
min_max2: f32,
|
||||
#[garde(pattern(r"^[Hh]ello\b"))]
|
||||
regex_str1: String,
|
||||
#[garde(contains(concat!("sub","string...")))]
|
||||
contains_str1: String,
|
||||
#[garde(email)]
|
||||
email_address: String,
|
||||
#[garde(url)]
|
||||
homepage: String,
|
||||
#[garde(length(min = 1, max = 100))]
|
||||
non_empty_str: String,
|
||||
#[garde(length(min = MIN, max = MAX))]
|
||||
non_empty_str2: String,
|
||||
#[garde(length(equal = 2))]
|
||||
pair: Vec<i32>,
|
||||
#[garde(required)]
|
||||
required_option: Option<bool>,
|
||||
#[garde(required)]
|
||||
#[serde(flatten)]
|
||||
required_flattened: Option<Inner>,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(JsonSchema)]
|
||||
pub struct Inner {
|
||||
x: i32,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn garde() -> TestResult {
|
||||
test_default_generated_schema::<Struct>("garde")
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(JsonSchema)]
|
||||
pub struct Struct2 {
|
||||
#[schemars(range(min = 0.01, max = 100))]
|
||||
min_max: f32,
|
||||
#[schemars(range(min = MIN, max = MAX))]
|
||||
min_max2: f32,
|
||||
#[schemars(pattern(r"^[Hh]ello\b"))]
|
||||
regex_str1: String,
|
||||
#[schemars(contains(concat!("sub","string...")))]
|
||||
contains_str1: String,
|
||||
#[schemars(email)]
|
||||
email_address: String,
|
||||
#[schemars(url)]
|
||||
homepage: String,
|
||||
#[schemars(length(min = 1, max = 100))]
|
||||
non_empty_str: String,
|
||||
#[schemars(length(min = MIN, max = MAX))]
|
||||
non_empty_str2: String,
|
||||
#[schemars(length(equal = 2))]
|
||||
pair: Vec<i32>,
|
||||
#[schemars(required)]
|
||||
required_option: Option<bool>,
|
||||
#[schemars(required)]
|
||||
#[serde(flatten)]
|
||||
required_flattened: Option<Inner>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn garde_schemars_attrs() -> TestResult {
|
||||
test_default_generated_schema::<Struct2>("garde_schemars_attrs")
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(JsonSchema)]
|
||||
pub struct Tuple(
|
||||
#[garde(range(max = 10))] u8,
|
||||
#[garde(required)] Option<bool>,
|
||||
);
|
||||
|
||||
#[test]
|
||||
fn garde_tuple() -> TestResult {
|
||||
test_default_generated_schema::<Tuple>("garde_tuple")
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(JsonSchema)]
|
||||
pub struct NewType(#[garde(range(max = 10))] u8);
|
||||
|
||||
#[test]
|
||||
fn garde_newtype() -> TestResult {
|
||||
test_default_generated_schema::<NewType>("garde_newtype")
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
mod util;
|
||||
use schemars::JsonSchema;
|
||||
use std::collections::BTreeMap;
|
||||
use util::*;
|
||||
|
||||
struct FakeRegex(&'static str);
|
||||
|
@ -42,8 +41,6 @@ pub struct Struct {
|
|||
non_empty_str2: String,
|
||||
#[validate(length(equal = 2))]
|
||||
pair: Vec<i32>,
|
||||
#[validate(contains(pattern = "map_key"))]
|
||||
map_contains: BTreeMap<String, ()>,
|
||||
#[validate(required)]
|
||||
required_option: Option<bool>,
|
||||
#[validate(required)]
|
||||
|
@ -90,8 +87,6 @@ pub struct Struct2 {
|
|||
non_empty_str2: String,
|
||||
#[schemars(length(equal = 2))]
|
||||
pair: Vec<i32>,
|
||||
#[schemars(contains(pattern = "map_key"))]
|
||||
map_contains: BTreeMap<String, ()>,
|
||||
#[schemars(required)]
|
||||
required_option: Option<bool>,
|
||||
#[schemars(required)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue