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
274274* [ email] ( #rule-email )
275275* [ uppercase] ( #rule-uppercase )
276276* [ lowercase] ( #rule-lowercase )
277+ * [ json] ( #rule-json )
277278* [ alpha] ( #rule-alpha )
278279* [ numeric] ( #rule-numeric )
279280* [ alpha_num] ( #rule-alpha_num )
@@ -404,6 +405,11 @@ The field under this validation must be valid uppercase.
404405
405406The field under this validation must be valid lowercase.
406407
408+ <a id =" rule-json " ></a >
409+ #### json
410+
411+ The field under this validation must be valid JSON string.
412+
407413<a id =" rule-alpha " ></a >
408414#### alpha
409415
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()
106106 'after ' => new Rules \After ,
107107 'lowercase ' => new Rules \Lowercase ,
108108 'uppercase ' => new Rules \Uppercase ,
109+ 'json ' => new Rules \Json ,
109110 'defaults ' => new Rules \Defaults ,
110111 'default ' => new Rules \Defaults , // alias of defaults
111112 ];
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 22
33use Rakit \Validation \Validator ;
44
5- require_once 'Fixtures/Json .php ' ;
5+ require_once 'Fixtures/Even .php ' ;
66require_once 'Fixtures/Required.php ' ;
77
88class ValidatorTest extends PHPUnit_Framework_TestCase
@@ -339,11 +339,11 @@ public function testAfterRule()
339339 public function testNewValidationRuleCanBeAdded ()
340340 {
341341
342- $ this ->validator ->addValidator ('json ' , new Json ());
342+ $ this ->validator ->addValidator ('even ' , new Even ());
343343
344- $ data = [' s ' => json_encode ([ ' name ' => ' space x ' , ' human ' => false ]) ];
344+ $ data = [4 , 6 , 8 , 10 ];
345345
346- $ validation = $ this ->validator ->make ($ data , ['s ' => 'json ' ], []);
346+ $ validation = $ this ->validator ->make ($ data , ['s ' => 'even ' ], []);
347347
348348 $ validation ->validate ();
349349
You can’t perform that action at this time.
0 commit comments