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