| 1 | #![allow(clippy::incompatible_msrv)] |
| 2 | use crate::prelude::*; |
| 3 | use garde::Validate; |
| 4 | |
| 5 | const ONE: usize = 1; |
| 6 | const HUNDRED: usize = 10; |
| 7 | |
| 8 | #[derive(JsonSchema, Deserialize, Serialize, Validate)] |
| 9 | pub struct GardeAttrStruct { |
| 10 | #[garde(range(min = 1.0, max = 100.0))] |
| 11 | min_max: f32, |
| 12 | #[garde(range(min = ONE as f32, max = HUNDRED as f32))] |
| 13 | min_max2: f32, |
| 14 | #[garde(pattern(r"^[Hh]ello"))] |
| 15 | regex_str: String, |
| 16 | #[garde(contains(concat!("sub","string...")))] |
| 17 | contains_str: String, |
| 18 | #[garde(email)] |
| 19 | email_address: String, |
| 20 | #[garde(url)] |
| 21 | homepage: String, |
| 22 | #[garde(length(min = ONE, max = HUNDRED))] |
| 23 | non_empty_str: String, |
| 24 | #[garde(length(equal = 2), inner(length(min = 1)))] |
| 25 | pair: Vec<String>, |
| 26 | #[garde(required)] |
| 27 | required_option: Option<bool>, |
| 28 | #[garde(required, dive)] |
| 29 | #[serde(flatten)] |
| 30 | required_flattened: Option<GardeAttrInner>, |
| 31 | } |
| 32 | |
| 33 | #[derive(JsonSchema, Deserialize, Serialize, Validate)] |
| 34 | pub struct GardeAttrInner { |
| 35 | #[garde(range(min = -100, max = 100))] |
| 36 | x: i32, |
| 37 | } |
| 38 | |
| 39 | impl Default for GardeAttrStruct { |
| 40 | fn default() -> Self { |
| 41 | Self { |
| 42 | min_max: 1.0, |
| 43 | min_max2: 1.0, |
| 44 | regex_str: "Hello world".to_owned(), |
| 45 | contains_str: "Contains substring...".to_owned(), |
| 46 | email_address: "[email protected]".to_owned(), |
| 47 | homepage: "http://test.test".to_owned(), |
| 48 | non_empty_str: "test".to_owned(), |
| 49 | pair: vec!["a".to_owned(), "b".to_owned()], |
| 50 | required_option: Some(true), |
| 51 | required_flattened: Some(GardeAttrInner { x: 0 }), |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | impl GardeAttrStruct { |
| 57 | pub fn invalid_values() -> impl IntoIterator<Item = Self> { |
| 58 | static MUTATORS: &[fn(&mut GardeAttrStruct)] = &[ |
| 59 | |v| v.min_max = 0.9, |
| 60 | |v| v.min_max = 100.1, |
| 61 | |v| v.min_max2 = 0.9, |
| 62 | |v| v.min_max2 = 100.1, |
| 63 | |v| v.regex_str = "fail".to_owned(), |
| 64 | |v| v.contains_str = "fail".to_owned(), |
| 65 | |v| v.email_address = "fail".to_owned(), |
| 66 | |v| v.homepage = "fail".to_owned(), |
| 67 | |v| v.non_empty_str = String::new(), |
| 68 | |v| v.pair = Vec::new(), |
| 69 | |v| v.pair = vec!["a".to_owned(), "b".to_owned(), "c".to_owned()], |
| 70 | |v| v.pair = vec!["".to_owned(), "b".to_owned()], |
| 71 | |v| v.required_option = None, |
| 72 | |v| v.required_flattened = None, |
| 73 | |v| v.required_flattened = Some(GardeAttrInner { x: -101 }), |
| 74 | |v| v.required_flattened = Some(GardeAttrInner { x: 101 }), |
| 75 | ]; |
| 76 | MUTATORS.iter().map(|f| { |
| 77 | let mut result = GardeAttrStruct::default(); |
| 78 | f(&mut result); |
| 79 | result |
| 80 | }) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | #[test] |
| 85 | fn garde_attrs() { |
| 86 | test!(GardeAttrStruct) |
| 87 | .with_validator(|v| v.validate().is_ok()) |
| 88 | .assert_snapshot() |
| 89 | .assert_allows_ser_roundtrip_default() |
| 90 | .assert_rejects_invalid(GardeAttrStruct::invalid_values()) |
| 91 | .assert_matches_de_roundtrip(arbitrary_values()); |
| 92 | } |
| 93 | |
| 94 | #[allow(dead_code)] |
| 95 | #[derive(JsonSchema)] |
| 96 | #[schemars(rename = "GardeAttrStruct")] |
| 97 | pub struct SchemarsAttrStruct { |
| 98 | #[schemars(range(min = 1.0, max = 100.0))] |
| 99 | min_max: f32, |
| 100 | #[schemars(range(min = ONE as f32, max = HUNDRED as f32))] |
| 101 | min_max2: f32, |
| 102 | #[schemars(pattern(r"^[Hh]ello"))] |
| 103 | regex_str: String, |
| 104 | #[schemars(contains(concat!("sub","string...")))] |
| 105 | contains_str: String, |
| 106 | #[schemars(email)] |
| 107 | email_address: String, |
| 108 | #[schemars(url)] |
| 109 | homepage: String, |
| 110 | #[schemars(length(min = ONE, max = HUNDRED))] |
| 111 | non_empty_str: String, |
| 112 | #[schemars(length(equal = 2), inner(length(min = 1)))] |
| 113 | pair: Vec<String>, |
| 114 | #[schemars(required)] |
| 115 | required_option: Option<bool>, |
| 116 | #[schemars(required)] |
| 117 | #[serde(flatten)] |
| 118 | required_flattened: Option<SchemarsAttrInner>, |
| 119 | } |
| 120 | |
| 121 | #[allow(dead_code)] |
| 122 | #[derive(JsonSchema)] |
| 123 | pub struct SchemarsAttrInner { |
| 124 | #[schemars(range(min = -100, max = 100))] |
| 125 | x: i32, |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn schemars_attrs() { |
| 130 | test!(SchemarsAttrStruct).assert_identical::<GardeAttrStruct>(); |
| 131 | } |
| 132 | |
| 133 | #[derive(JsonSchema, Deserialize, Serialize, Validate)] |
| 134 | pub struct GardeAttrTuple( |
| 135 | #[garde(range(max = 10))] u8, |
| 136 | #[garde(required)] Option<bool>, |
| 137 | ); |
| 138 | |
| 139 | #[test] |
| 140 | fn garde_attrs_tuple() { |
| 141 | test!(GardeAttrTuple) |
| 142 | .with_validator(|v| v.validate().is_ok()) |
| 143 | .assert_snapshot() |
| 144 | .assert_allows_ser_roundtrip([GardeAttrTuple(10, Some(false))]) |
| 145 | .assert_rejects_invalid([GardeAttrTuple(11, Some(false)), GardeAttrTuple(10, None)]) |
| 146 | .assert_matches_de_roundtrip(arbitrary_values()); |
| 147 | } |
| 148 | |
| 149 | #[derive(JsonSchema, Deserialize, Serialize, Validate)] |
| 150 | pub struct GardeAttrNewType(#[garde(range(max = 10))] u8); |
| 151 | |
| 152 | #[test] |
| 153 | fn garde_attrs_newtype() { |
| 154 | test!(GardeAttrNewType) |
| 155 | .with_validator(|v| v.validate().is_ok()) |
| 156 | .assert_snapshot() |
| 157 | .assert_allows_ser_roundtrip([GardeAttrNewType(10)]) |
| 158 | .assert_rejects_invalid([GardeAttrNewType(11)]) |
| 159 | .assert_matches_de_roundtrip(arbitrary_values()); |
| 160 | } |