|
| 1 | +use fortifier::{Validate, error_code}; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | + |
| 4 | +#[derive(Deserialize, Serialize, Validate)] |
| 5 | +struct CustomData<'a> { |
| 6 | + #[validate(custom(function = custom, error = CustomError))] |
| 7 | + zero_options: &'a str, |
| 8 | + |
| 9 | + #[validate(custom(function = custom, error = CustomError))] |
| 10 | + strip_one_option: Option<&'a str>, |
| 11 | + #[validate(custom(function = custom, error = CustomError))] |
| 12 | + strip_two_options: Option<Option<&'a str>>, |
| 13 | + #[validate(custom(function = custom, error = CustomError))] |
| 14 | + strip_three_options: Option<Option<Option<&'a str>>>, |
| 15 | + |
| 16 | + #[validate(custom(function = custom_one_option, error = CustomError, options))] |
| 17 | + strip_no_options_from_one: Option<&'a str>, |
| 18 | + #[validate(custom(function = custom_two_options, error = CustomError, options))] |
| 19 | + strip_no_options_from_two: Option<Option<&'a str>>, |
| 20 | + #[validate(custom(function = custom_three_options, error = CustomError, options))] |
| 21 | + strip_no_options_from_three: Option<Option<Option<&'a str>>>, |
| 22 | + |
| 23 | + #[validate(custom(function = custom_one_option, error = CustomError, options = 1))] |
| 24 | + strip_to_one_option_from_one: Option<&'a str>, |
| 25 | + #[validate(custom(function = custom_one_option, error = CustomError, options = 1))] |
| 26 | + strip_to_one_option_from_two: Option<Option<&'a str>>, |
| 27 | + #[validate(custom(function = custom_one_option, error = CustomError, options = 1))] |
| 28 | + strip_to_one_option_from_three: Option<Option<Option<&'a str>>>, |
| 29 | + |
| 30 | + #[validate(custom(function = custom_one_option, error = CustomError, options = 2))] |
| 31 | + strip_to_two_options_from_one: Option<&'a str>, |
| 32 | + #[validate(custom(function = custom_two_options, error = CustomError, options = 2))] |
| 33 | + strip_to_two_options_from_two: Option<Option<&'a str>>, |
| 34 | + #[validate(custom(function = custom_two_options, error = CustomError, options = 2))] |
| 35 | + strip_to_two_options_from_three: Option<Option<Option<&'a str>>>, |
| 36 | +} |
| 37 | + |
| 38 | +error_code!(CustomErrorCode, "custom"); |
| 39 | + |
| 40 | +#[derive(Debug, Deserialize, PartialEq, Serialize)] |
| 41 | +#[serde(rename_all = "camelCase")] |
| 42 | +struct CustomError { |
| 43 | + code: CustomErrorCode, |
| 44 | +} |
| 45 | + |
| 46 | +fn custom(value: &str) -> Result<(), CustomError> { |
| 47 | + if value == "" { |
| 48 | + Ok(()) |
| 49 | + } else { |
| 50 | + Err(CustomError { |
| 51 | + code: CustomErrorCode, |
| 52 | + }) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +fn custom_one_option(value: &Option<&str>) -> Result<(), CustomError> { |
| 57 | + if let Some(value) = value |
| 58 | + && *value == "" |
| 59 | + { |
| 60 | + Ok(()) |
| 61 | + } else { |
| 62 | + Err(CustomError { |
| 63 | + code: CustomErrorCode, |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +fn custom_two_options(value: &Option<Option<&str>>) -> Result<(), CustomError> { |
| 69 | + if let Some(Some(value)) = value |
| 70 | + && *value == "" |
| 71 | + { |
| 72 | + Ok(()) |
| 73 | + } else { |
| 74 | + Err(CustomError { |
| 75 | + code: CustomErrorCode, |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +fn custom_three_options(value: &Option<Option<Option<&str>>>) -> Result<(), CustomError> { |
| 81 | + if let Some(Some(Some(value))) = value |
| 82 | + && *value == "" |
| 83 | + { |
| 84 | + Ok(()) |
| 85 | + } else { |
| 86 | + Err(CustomError { |
| 87 | + code: CustomErrorCode, |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +fn main() { |
| 93 | + let data = CustomData { |
| 94 | + zero_options: "", |
| 95 | + |
| 96 | + strip_one_option: Some(""), |
| 97 | + strip_two_options: Some(Some("")), |
| 98 | + strip_three_options: Some(Some(Some(""))), |
| 99 | + |
| 100 | + strip_no_options_from_one: Some(""), |
| 101 | + strip_no_options_from_two: Some(Some("")), |
| 102 | + strip_no_options_from_three: Some(Some(Some(""))), |
| 103 | + |
| 104 | + strip_to_one_option_from_one: Some(""), |
| 105 | + strip_to_one_option_from_two: Some(Some("")), |
| 106 | + strip_to_one_option_from_three: Some(Some(Some(""))), |
| 107 | + |
| 108 | + strip_to_two_options_from_one: Some(""), |
| 109 | + strip_to_two_options_from_two: Some(Some("")), |
| 110 | + strip_to_two_options_from_three: Some(Some(Some(""))), |
| 111 | + }; |
| 112 | + |
| 113 | + assert_eq!(data.validate_sync(), Ok(())); |
| 114 | +} |
0 commit comments