-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The commit introduces a `is_json` rule to validate if a given input is a valid JSON. This rule has been added to the json, php, and yml schema examples. Accompanying unit tests have also been added. The rule count in the README.md file is also updated to reflect the new addition.
- Loading branch information
Showing
7 changed files
with
107 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/** | ||
* JBZoo Toolbox - Csv-Blueprint. | ||
* | ||
* This file is part of the JBZoo Toolbox project. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT | ||
* @copyright Copyright (C) JBZoo.com, All rights reserved. | ||
* @see https://github.com/JBZoo/Csv-Blueprint | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JBZoo\CsvBlueprint\Rules\Cell; | ||
|
||
use Respect\Validation\Validator; | ||
|
||
final class IsJson extends AbstractCellRule | ||
{ | ||
protected const HELP_TOP = [ | ||
'Validates if the given input is a valid JSON.', | ||
'This is possible if you escape all special characters correctly and use a special CSV format.', | ||
]; | ||
|
||
protected const HELP_OPTIONS = [ | ||
self::DEFAULT => [ | ||
'true', | ||
'Example: {"foo":"bar"}', | ||
], | ||
]; | ||
|
||
public function validateRule(string $cellValue): ?string | ||
{ | ||
if (!Validator::json()->validate($cellValue)) { | ||
return "Value \"<c>{$cellValue}</c>\" is not a valid JSON"; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
/** | ||
* JBZoo Toolbox - Csv-Blueprint. | ||
* | ||
* This file is part of the JBZoo Toolbox project. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT | ||
* @copyright Copyright (C) JBZoo.com, All rights reserved. | ||
* @see https://github.com/JBZoo/Csv-Blueprint | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JBZoo\PHPUnit\Rules\Cell; | ||
|
||
use JBZoo\CsvBlueprint\Rules\Cell\IsJson; | ||
use JBZoo\PHPUnit\Rules\AbstractCellRule; | ||
|
||
use function JBZoo\PHPUnit\isSame; | ||
|
||
final class IsJsonTest extends AbstractCellRule | ||
{ | ||
protected string $ruleClass = IsJson::class; | ||
|
||
public function testPositive(): void | ||
{ | ||
$rule = $this->create(true); | ||
isSame('', $rule->test('{"foo":"bar"}')); | ||
} | ||
|
||
public function testNegative(): void | ||
{ | ||
$rule = $this->create(true); | ||
isSame( | ||
'Value "Hello world!" is not a valid JSON', | ||
$rule->test('Hello world!'), | ||
); | ||
} | ||
} |