1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use c3p0::Model;
use lightspeed_core::error::{ErrorDetails, LightSpeedError};
use lightspeed_core::service::validator::order::validate_ge;
use lightspeed_core::service::validator::Validable;
use serde::{Deserialize, Serialize};
pub type ProjectModel = Model<ProjectData>;
#[derive(Clone, Serialize, Deserialize)]
pub struct ProjectData {
pub name: String,
}
impl Validable for ProjectData {
fn validate(&self, error_details: &mut ErrorDetails) -> Result<(), LightSpeedError> {
validate_ge(error_details, "name", 3, self.name.len());
Ok(())
}
}
#[cfg(test)]
pub mod test {
use super::*;
use lightspeed_core::error::ErrorDetail;
use lightspeed_core::service::validator::order::MUST_BE_GREATER_OR_EQUAL;
use lightspeed_core::service::validator::Validator;
#[test]
pub fn validation_should_fail_if_name_too_short() {
let project_data = ProjectData {
name: "".to_owned(),
};
let result = Validator::validate(&project_data);
match result {
Err(LightSpeedError::ValidationError { details }) => {
assert_eq!(details.details.len(), 1);
assert_eq!(
details.details.get("name"),
Some(&vec![ErrorDetail::new(
MUST_BE_GREATER_OR_EQUAL,
vec!["3".to_owned()]
)])
);
}
_ => assert!(false),
}
}
#[test]
pub fn should_validate() {
let project_data = ProjectData {
name: "good name".to_owned(),
};
let result = Validator::validate(&project_data);
assert!(result.is_ok());
}
}