Skip to content

Commit f4f7c25

Browse files
authoredDec 28, 2024··
cancel white space removal within attributes value (#14)
1 parent 7376eef commit f4f7c25

File tree

4 files changed

+453
-28
lines changed

4 files changed

+453
-28
lines changed
 

‎Cargo.lock

+421-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
authors = ["James Douglass Lefruit"]
33
categories = ["data-structures", "web-programming", "rust-patterns"]
44
description = "A library to create dynamic DTOs (Data Transfer Object) from a structure"
5-
documentation = "https://github.com/douggynix/dto_mapper/tree/0.3.0"
5+
documentation = "https://github.com/douggynix/dto_mapper/tree/0.3.1"
66
edition = "2021"
77
exclude = [
88
".github/workflows/rust.yml",
@@ -14,7 +14,7 @@ license = "Apache-2.0"
1414
name = "dto_mapper"
1515
readme = "README.md"
1616
repository = "https://github.com/douggynix/dto_mapper"
17-
version = "0.3.0"
17+
version = "0.3.1"
1818
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1919
[lib]
2020
proc-macro = true
@@ -29,3 +29,4 @@ syn = {version = "2.0", features = ["full"]}
2929
derive_builder = "0.20"
3030
serde = {version = "1.0", features = ["serde_derive", "derive"]}
3131
serde_json = {version = "1.0"}
32+
validator = { version = "0.19.0", features = ["derive"] }

‎src/mapper_entry.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ impl MapperEntry {
271271
if let Expr::Lit(content_lit) = elem {
272272
// eprintln!("content_lit={:?}", content_lit);
273273
if let Lit::Str(content) = &content_lit.lit {
274-
vec_array.push(utils::remove_white_space(&content.value()).into());
274+
vec_array.push(content.value().trim().to_string());
275275
}
276276
}
277277
// eprintln!("================>");

‎tests/test_dto_creation.rs

+28-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod test_dto_creation {
55
use serde::{Deserialize, Serialize};
66
#[allow(unused)]
77
use std::str::FromStr;
8+
use validator::{Validate, ValidationErrors};
89
#[allow(unused)]
910

1011
fn concat_str(s1: &str, s2: &str) -> String {
@@ -21,20 +22,31 @@ mod test_dto_creation {
2122
#[mapper(
2223
dto="CustomDtoWithAttribute" ,
2324
no_builder=true ,
24-
map=[ ("email", false, ["#[serde(rename = \"email_address\")]"] ) ],
25-
derive=(Debug, Clone, Serialize, Deserialize),
25+
derive=(Debug, Clone, Serialize, Deserialize, Validate),
26+
map=[( "email",
27+
false,
28+
[
29+
"#[serde(rename = \"email_address\")]",
30+
"#[validate(email(message = \"Invalid Email Address\") ) ]",
31+
]
32+
)],
2633
new_fields=[
2734
(
2835
"name: String",
2936
"concat_str( self.firstname.as_str(), self.lastname.as_str() )",
30-
["#[serde(rename = \"full_name\")]"],
37+
[
38+
"#[serde(rename = \"full_name\")]",
39+
"#[validate( length( max=3, message= \"Too short\" ) )]",
40+
],
3141
),
3242
(
3343
"hidden_password: String",
3444
r#""*".repeat( self.password.len() )"#
3545
),
3646
],
37-
macro_attr=["serde(rename_all = \"UPPERCASE\")"]
47+
macro_attr=[
48+
"#[serde(rename_all = \"UPPERCASE\")]",
49+
]
3850
)]
3951
struct User {
4052
username: String,
@@ -157,18 +169,27 @@ mod test_dto_creation {
157169
let user = User {
158170
firstname: "Dessalines".into(),
159171
lastname: "Jean Jacques".into(),
160-
email: "dessalines@gmail.com".into(),
172+
email: "dessalinesgmail.com".into(),
161173
password: "hello123".into(),
162174
..User::default()
163175
};
176+
164177
let custom_dto: CustomDtoWithAttribute = user.clone().into();
178+
179+
//Check validator
180+
let validation_errors: ValidationErrors = custom_dto.validate().unwrap_err();
181+
182+
assert!(validation_errors.0.contains_key("email"));
183+
assert!(validation_errors.0.contains_key("name"));
184+
println!("Validation error = {}", validation_errors.to_string());
165185
assert_eq!(custom_dto.clone().email.unwrap(), user.email);
166186

167187
// check json serialization
168-
let json_string = serde_json::to_string(&custom_dto).unwrap();
188+
let json_string =
189+
serde_json::to_string(&custom_dto).expect("Failed to Json deserialize custom_dto");
169190
assert_eq!(
170191
json_string,
171-
r#"{"email_address":"dessalines@gmail.com","full_name":"Dessalines Jean Jacques","HIDDEN_PASSWORD":"********"}"#
192+
r#"{"email_address":"dessalinesgmail.com","full_name":"Dessalines Jean Jacques","HIDDEN_PASSWORD":"********"}"#
172193
);
173194
}
174195
}

0 commit comments

Comments
 (0)
Please sign in to comment.