diff --git a/Cargo.lock b/Cargo.lock index 51b1d3b..5bb9677 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -328,6 +328,7 @@ dependencies = [ name = "fortifier" version = "0.2.8" dependencies = [ + "cfg-if", "chrono", "email_address", "fortifier-macros", diff --git a/packages/fortifier/Cargo.toml b/packages/fortifier/Cargo.toml index 054ada9..d06a1d1 100644 --- a/packages/fortifier/Cargo.toml +++ b/packages/fortifier/Cargo.toml @@ -48,6 +48,7 @@ utoipa = { workspace = true, optional = true } uuid = { workspace = true, optional = true } [dev-dependencies] +cfg-if = "1.0.4" pretty_assertions.workspace = true rust_decimal = { workspace = true, features = ["macros"] } serde_json.workspace = true diff --git a/packages/fortifier/tests/serde.rs b/packages/fortifier/tests/serde.rs index 9efaadc..8fafcda 100644 --- a/packages/fortifier/tests/serde.rs +++ b/packages/fortifier/tests/serde.rs @@ -1,5 +1,6 @@ #![cfg(feature = "serde")] +use cfg_if::cfg_if; use fortifier::{ EmailAddressError, LengthError, LengthErrorCode, RegexError, UrlError, ValidationErrors, }; @@ -18,66 +19,74 @@ enum TestError { } fn setup() -> (ValidationErrors, Value) { - ( - ValidationErrors::from_iter([ - TestError::EmailAddress(EmailAddressError::from( - email_address::Error::MissingSeparator, - )), - TestError::Length(LengthError::Equal { - equal: 1, - value: 2, - code: LengthErrorCode, - #[cfg(feature = "message")] - message: "length 2 is not equal to required length 1".to_owned(), - }), - TestError::Regex(RegexError::default()), - TestError::Url(UrlError::from(ParseError::EmptyHost)), - ]), - #[cfg(not(feature = "message"))] - json!([ - { - "code": "emailAddress", - "subcode": "missingSeparator", - }, - { - "code": "length", - "subcode": "equal", - "equal": 1, - "value": 2, - }, - { - "code": "regex", - }, - { - "code": "url", - "subcode": "emptyHost", - } - ]), - #[cfg(feature = "message")] - json!([ - { - "code": "emailAddress", - "subcode": "missingSeparator", - "message": "", - }, - { - "code": "length", - "subcode": "equal", - "equal": 1, - "value": 2, - "message": "length 2 is not equal to required length 1", - }, - { - "code": "regex", - "message": "value does not match regular expression", - }, - { - "code": "url", - "subcode": "emptyHost", - "message": "empty host", - } - ]), - ) + let errors = ValidationErrors::from_iter([ + TestError::EmailAddress(EmailAddressError::from( + email_address::Error::MissingSeparator, + )), + TestError::Length(LengthError::Equal { + equal: 1, + value: 2, + code: LengthErrorCode, + #[cfg(feature = "message")] + message: "length 2 is not equal to required length 1".to_owned(), + }), + TestError::Regex(RegexError::default()), + TestError::Url(UrlError::from(ParseError::EmptyHost)), + ]); + + cfg_if! { + if #[cfg(feature = "message")] { + ( + errors, + json!([ + { + "code": "emailAddress", + "subcode": "missingSeparator", + "message": "", + }, + { + "code": "length", + "subcode": "equal", + "equal": 1, + "value": 2, + "message": "length 2 is not equal to required length 1", + }, + { + "code": "regex", + "message": "value does not match regular expression", + }, + { + "code": "url", + "subcode": "emptyHost", + "message": "empty host", + } + ]), + ) + } else { + ( + errors, + json!([ + { + "code": "emailAddress", + "subcode": "missingSeparator", + }, + { + "code": "length", + "subcode": "equal", + "equal": 1, + "value": 2, + }, + { + "code": "regex", + }, + { + "code": "url", + "subcode": "emptyHost", + } + ]), + ) + } + } } #[test]