Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ Below is list of all available validation rules
* [email](#rule-email)
* [uppercase](#rule-uppercase)
* [lowercase](#rule-lowercase)
* [json](#rule-json)
* [alpha](#rule-alpha)
* [numeric](#rule-numeric)
* [alpha_num](#rule-alpha_num)
Expand Down Expand Up @@ -404,6 +405,11 @@ The field under this validation must be valid uppercase.

The field under this validation must be valid lowercase.

<a id="rule-json"></a>
#### json

The field under this validation must be valid JSON string.

<a id="rule-alpha"></a>
#### alpha

Expand Down
27 changes: 27 additions & 0 deletions src/Rules/Json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rakit\Validation\Rules;

use Rakit\Validation\Rule;

class Json extends Rule
{

protected $message = "The :attribute must be a valid JSON string";

public function check($value)
{
if (! is_string($value) || empty($value)) {
return false;
}

json_decode($value);

if (json_last_error() !== JSON_ERROR_NONE) {
return false;
}

return true;
}

}
1 change: 1 addition & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ protected function registerBaseValidators()
'after' => new Rules\After,
'lowercase' => new Rules\Lowercase,
'uppercase' => new Rules\Uppercase,
'json' => new Rules\Json,
'defaults' => new Rules\Defaults,
'default' => new Rules\Defaults, // alias of defaults
];
Expand Down
18 changes: 18 additions & 0 deletions tests/Fixtures/Even.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php


class Even extends \Rakit\Validation\Rule
{

protected $message = "The :attribute must be even";

public function check($value)
{
if ( ! is_numeric($value)) {
return false;
}

return $value % 2 === 0;
}

}
20 changes: 0 additions & 20 deletions tests/Fixtures/Json.php

This file was deleted.

33 changes: 33 additions & 0 deletions tests/Rules/JsonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Rakit\Validation\Rules\Json;

class JsonTest extends PHPUnit_Framework_TestCase
{

public function setUp()
{
$this->rule = new Json;
}

public function testValids()
{
$this->assertTrue($this->rule->check('{}'));
$this->assertTrue($this->rule->check('[]'));
$this->assertTrue($this->rule->check('false'));
$this->assertTrue($this->rule->check('null'));
$this->assertTrue($this->rule->check('{"username": "John Doe"}'));
$this->assertTrue($this->rule->check('{"number": 12345678}'));
}

public function testInvalids()
{
$this->assertFalse($this->rule->check(''));
$this->assertFalse($this->rule->check(123));
$this->assertFalse($this->rule->check(false));
$this->assertFalse($this->rule->check('{"username": John Doe}'));
$this->assertFalse($this->rule->check('{number: 12345678}'));
}

}

8 changes: 4 additions & 4 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Rakit\Validation\Validator;

require_once 'Fixtures/Json.php';
require_once 'Fixtures/Even.php';
require_once 'Fixtures/Required.php';

class ValidatorTest extends PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -339,11 +339,11 @@ public function testAfterRule()
public function testNewValidationRuleCanBeAdded()
{

$this->validator->addValidator('json', new Json());
$this->validator->addValidator('even', new Even());

$data = ['s' => json_encode(['name' => 'space x', 'human' => false])];
$data = [4, 6, 8, 10 ];

$validation = $this->validator->make($data, ['s' => 'json'], []);
$validation = $this->validator->make($data, ['s' => 'even'], []);

$validation->validate();

Expand Down