Skip to content

Commit e3473f8

Browse files
authored
Merge pull request #51 from phannaly/master
Add json rule
2 parents d000621 + 2dad32c commit e3473f8

File tree

7 files changed

+89
-24
lines changed

7 files changed

+89
-24
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff 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

405406
The 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

src/Rules/Json.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

src/Validator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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
];

tests/Fixtures/Even.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

tests/Fixtures/Json.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/Rules/JsonTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+

tests/ValidatorTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Rakit\Validation\Validator;
44

5-
require_once 'Fixtures/Json.php';
5+
require_once 'Fixtures/Even.php';
66
require_once 'Fixtures/Required.php';
77

88
class 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

0 commit comments

Comments
 (0)