File tree Expand file tree Collapse file tree 7 files changed +89
-24
lines changed Expand file tree Collapse file tree 7 files changed +89
-24
lines changed Original file line number Diff line number Diff line change @@ -274,6 +274,7 @@ Below is list of all available validation rules
274
274
* [ email] ( #rule-email )
275
275
* [ uppercase] ( #rule-uppercase )
276
276
* [ lowercase] ( #rule-lowercase )
277
+ * [ json] ( #rule-json )
277
278
* [ alpha] ( #rule-alpha )
278
279
* [ numeric] ( #rule-numeric )
279
280
* [ alpha_num] ( #rule-alpha_num )
@@ -404,6 +405,11 @@ The field under this validation must be valid uppercase.
404
405
405
406
The field under this validation must be valid lowercase.
406
407
408
+ <a id =" rule-json " ></a >
409
+ #### json
410
+
411
+ The field under this validation must be valid JSON string.
412
+
407
413
<a id =" rule-alpha " ></a >
408
414
#### alpha
409
415
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Rakit \Validation \Rules ;
4
+
5
+ use Rakit \Validation \Rule ;
6
+
7
+ class Json extends Rule
8
+ {
9
+
10
+ protected $ message = "The :attribute must be a valid JSON string " ;
11
+
12
+ public function check ($ value )
13
+ {
14
+ if (! is_string ($ value ) || empty ($ value )) {
15
+ return false ;
16
+ }
17
+
18
+ json_decode ($ value );
19
+
20
+ if (json_last_error () !== JSON_ERROR_NONE ) {
21
+ return false ;
22
+ }
23
+
24
+ return true ;
25
+ }
26
+
27
+ }
Original file line number Diff line number Diff line change @@ -106,6 +106,7 @@ protected function registerBaseValidators()
106
106
'after ' => new Rules \After ,
107
107
'lowercase ' => new Rules \Lowercase ,
108
108
'uppercase ' => new Rules \Uppercase ,
109
+ 'json ' => new Rules \Json ,
109
110
'defaults ' => new Rules \Defaults ,
110
111
'default ' => new Rules \Defaults , // alias of defaults
111
112
];
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+
4
+ class Even extends \Rakit \Validation \Rule
5
+ {
6
+
7
+ protected $ message = "The :attribute must be even " ;
8
+
9
+ public function check ($ value )
10
+ {
11
+ if ( ! is_numeric ($ value )) {
12
+ return false ;
13
+ }
14
+
15
+ return $ value % 2 === 0 ;
16
+ }
17
+
18
+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ use Rakit \Validation \Rules \Json ;
4
+
5
+ class JsonTest extends PHPUnit_Framework_TestCase
6
+ {
7
+
8
+ public function setUp ()
9
+ {
10
+ $ this ->rule = new Json ;
11
+ }
12
+
13
+ public function testValids ()
14
+ {
15
+ $ this ->assertTrue ($ this ->rule ->check ('{} ' ));
16
+ $ this ->assertTrue ($ this ->rule ->check ('[] ' ));
17
+ $ this ->assertTrue ($ this ->rule ->check ('false ' ));
18
+ $ this ->assertTrue ($ this ->rule ->check ('null ' ));
19
+ $ this ->assertTrue ($ this ->rule ->check ('{"username": "John Doe"} ' ));
20
+ $ this ->assertTrue ($ this ->rule ->check ('{"number": 12345678} ' ));
21
+ }
22
+
23
+ public function testInvalids ()
24
+ {
25
+ $ this ->assertFalse ($ this ->rule ->check ('' ));
26
+ $ this ->assertFalse ($ this ->rule ->check (123 ));
27
+ $ this ->assertFalse ($ this ->rule ->check (false ));
28
+ $ this ->assertFalse ($ this ->rule ->check ('{"username": John Doe} ' ));
29
+ $ this ->assertFalse ($ this ->rule ->check ('{number: 12345678} ' ));
30
+ }
31
+
32
+ }
33
+
Original file line number Diff line number Diff line change 2
2
3
3
use Rakit \Validation \Validator ;
4
4
5
- require_once 'Fixtures/Json .php ' ;
5
+ require_once 'Fixtures/Even .php ' ;
6
6
require_once 'Fixtures/Required.php ' ;
7
7
8
8
class ValidatorTest extends PHPUnit_Framework_TestCase
@@ -339,11 +339,11 @@ public function testAfterRule()
339
339
public function testNewValidationRuleCanBeAdded ()
340
340
{
341
341
342
- $ this ->validator ->addValidator ('json ' , new Json ());
342
+ $ this ->validator ->addValidator ('even ' , new Even ());
343
343
344
- $ data = [' s ' => json_encode ([ ' name ' => ' space x ' , ' human ' => false ]) ];
344
+ $ data = [4 , 6 , 8 , 10 ];
345
345
346
- $ validation = $ this ->validator ->make ($ data , ['s ' => 'json ' ], []);
346
+ $ validation = $ this ->validator ->make ($ data , ['s ' => 'even ' ], []);
347
347
348
348
$ validation ->validate ();
349
349
You can’t perform that action at this time.
0 commit comments