1
1
<?php
2
2
3
3
use Webhkp \Pvalidate \Rules \Length ;
4
+ use Webhkp \Pvalidate \Rules \MaxLength ;
5
+ use Webhkp \Pvalidate \Rules \MinLength ;
4
6
use Webhkp \Pvalidate \ValidationBuilder ;
5
7
use Webhkp \Pvalidate \Validator ;
6
8
@@ -20,6 +22,12 @@ public function __construct() {
20
22
21
23
#[Length(min: 20 , max: 50 )]
22
24
public string $ fourthString = 'my fourth string ' ;
25
+
26
+ #[MinLength(20 )]
27
+ public string $ minLengthTestString = 'abc ' ;
28
+
29
+ #[MaxLength(10 )]
30
+ public string $ maxLengthTestString = 'abc def ghi jkl mno ' ;
23
31
};
24
32
});
25
33
@@ -30,6 +38,8 @@ public function __construct() {
30
38
$ this ->testObj ->secondString = "abc def ghi jkl mno pqr stu vwx yz " ;
31
39
$ this ->testObj ->thirdString = "abc def " ;
32
40
$ this ->testObj ->fourthString = "abc def ghi jkl mno pqr stu vwx yz " ;
41
+ $ this ->testObj ->minLengthTestString = "abc def ghi jkl mno pqr stu vwx yz " ;
42
+ $ this ->testObj ->maxLengthTestString = "abc def " ;
33
43
34
44
$ validationResponse = Validator::validate ($ this ->testObj );
35
45
@@ -41,7 +51,7 @@ public function __construct() {
41
51
$ validationResponse = Validator::validate ($ this ->testObj );
42
52
43
53
expect ($ validationResponse ->isValid ())->toBeFalse ();
44
- expect ($ validationResponse ->getErrors ())->toHaveKeys (['firstString ' , 'secondString ' , 'thirdString ' , 'fourthString ' ]);
54
+ expect ($ validationResponse ->getErrors ())->toHaveKeys (['firstString ' , 'secondString ' , 'thirdString ' , 'fourthString ' , ' minLengthTestString ' , ' maxLengthTestString ' ]);
45
55
});
46
56
});
47
57
@@ -52,6 +62,34 @@ public function __construct() {
52
62
expect ($ validation ->isValid ())->toBeFalse ();
53
63
expect ($ validation ->getErrors ())->toHaveKeys (['length.errors.maxLength ' ]);
54
64
});
65
+
66
+ it ('Should return error for minLength violation ' , function () {
67
+ $ validation = ValidationBuilder::minLength (20 )->safeParse ('abc def ' );
68
+
69
+ expect ($ validation ->isValid ())->toBeFalse ();
70
+ expect ($ validation ->getErrors ())->toHaveKeys (['minLength.errors.minLength ' ]);
71
+ });
72
+
73
+ it ('Should return error for maxLength violation ' , function () {
74
+ $ validation = ValidationBuilder::maxLength (20 )->safeParse ('abc def ghi jkl mno pqr stu vwx yz ' );
75
+
76
+ expect ($ validation ->isValid ())->toBeFalse ();
77
+ expect ($ validation ->getErrors ())->toHaveKeys (['maxLength.errors.maxLength ' ]);
78
+ });
79
+
80
+ it ('Should return error for length(min or max) violation ' , function () {
81
+ $ validation = ValidationBuilder::minLength (10 )->maxLength (20 );
82
+
83
+ $ validationResult = $ validation ->safeParse ('abc ' );
84
+
85
+ expect ($ validationResult ->isValid ())->toBeFalse ();
86
+ expect ($ validationResult ->getErrors ())->toHaveKeys (['minLength.errors.minLength ' ]);
87
+
88
+ $ validationResult = $ validation ->safeParse ('abc def ghi jkl mno pqr stu vwx yz ' );
89
+
90
+ expect ($ validationResult ->isValid ())->toBeFalse ();
91
+ expect ($ validationResult ->getErrors ())->toHaveKeys (['maxLength.errors.maxLength ' ]);
92
+ });
55
93
});
56
94
});
57
95
0 commit comments