@@ -33,7 +33,66 @@ public void shouldValidateExample() {
33
33
);
34
34
testEntity = validator .validate (testEntity );
35
35
assertThat (email ).isNotEqualTo (testEntity .getEmail ());
36
+ assertThat (testEntity .getEmail ()).isEqualTo (email .trim ());
37
+ }
38
+
39
+ @ Test
40
+ public void shouldSkipOptionalPropertyIfNotSet () {
41
+ TestEntity testEntity =
new TestEntity (
"Mr" ,
"[email protected] " ,
null ,
3 ,
null );
42
+ validator = Validator .fromRules (
43
+ string ("title" ).required ().trim ().max (3 ),
44
+ string ("email" ).required ().trim ().email (),
45
+ string ("firstName" ).optional ().trim ().alphanum ().max (80 ),
46
+ number ("count" ).max (5 )
47
+ );
48
+ testEntity = validator .validate (testEntity );
49
+ assertThat (testEntity .getFirstName ()).isNull ();
50
+ }
51
+
52
+ @ Test
53
+ public void shouldValidateOptionalPropertyIfSet () {
54
+ TestEntity testEntity =
new TestEntity (
"Mr" ,
"[email protected] " ,
"Max " ,
3 ,
null );
55
+ validator = Validator .fromRules (
56
+ string ("title" ).required ().trim ().max (3 ),
57
+ string ("email" ).required ().trim ().email (),
58
+ string ("firstName" ).optional ().trim ().alphanum ().max (80 ),
59
+ number ("count" ).max (5 )
60
+ );
61
+ testEntity = validator .validate (testEntity );
62
+ assertThat (testEntity .getFirstName ().length ()).isEqualTo (3 );
63
+ }
36
64
65
+ @ Test
66
+ public void shouldSkipOptionalNestedPropertyIfNotSet () {
67
+ TestEntity testEntity =
new TestEntity (
"Mr" ,
"[email protected] " ,
null ,
3 ,
null );
68
+ validator = Validator .fromRules (
69
+ string ("title" ).required ().trim ().max (3 ),
70
+ string ("email" ).required ().trim ().email (),
71
+ string ("firstName" ).optional ().trim ().alphanum ().max (80 ),
72
+ number ("count" ).max (5 ),
73
+ object ("nestedEntity" ).optional ().fields (
74
+ string ("name" ).required ().trim ().min (1 )
75
+ )
76
+ );
77
+ testEntity = validator .validate (testEntity );
78
+ assertThat (testEntity .getNestedEntity ()).isNull ();
79
+ }
80
+
81
+ @ Test
82
+ public void shouldValidateOptionalNestedPropertyIfSet () {
83
+ final String name = " Test " ;
84
+ TestEntity testEntity =
new TestEntity (
"Mr" ,
"[email protected] " ,
null ,
3 ,
new NestedEntity (
name ));
85
+ validator = Validator .fromRules (
86
+ string ("title" ).required ().trim ().max (3 ),
87
+ string ("email" ).required ().trim ().email (),
88
+ string ("firstName" ).optional ().trim ().alphanum ().max (80 ),
89
+ number ("count" ).max (5 ),
90
+ object ("nestedEntity" ).optional ().fields (
91
+ string ("name" ).required ().trim ().min (1 )
92
+ )
93
+ );
94
+ testEntity = validator .validate (testEntity );
95
+ assertThat (testEntity .getNestedEntity ().getName ()).isNotEqualTo (name );
37
96
}
38
97
39
98
}
0 commit comments