Skip to content

Commit

Permalink
Add is_json rule (#50)
Browse files Browse the repository at this point in the history
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
SmetDenis committed Mar 17, 2024
1 parent 7c4d18b commit 268735a
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 10 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Stable Version](https://poser.pugx.org/jbzoo/csv-blueprint/version)](https://packagist.org/packages/jbzoo/csv-blueprint/) [![Total Downloads](https://poser.pugx.org/jbzoo/csv-blueprint/downloads)](https://packagist.org/packages/jbzoo/csv-blueprint/stats) [![Docker Pulls](https://img.shields.io/docker/pulls/jbzoo/csv-blueprint.svg)](https://hub.docker.com/r/jbzoo/csv-blueprint) [![Dependents](https://poser.pugx.org/jbzoo/csv-blueprint/dependents)](https://packagist.org/packages/jbzoo/csv-blueprint/dependents?order_by=downloads) [![GitHub License](https://img.shields.io/github/license/jbzoo/csv-blueprint)](https://github.com/JBZoo/Csv-Blueprint/blob/master/LICENSE)

<!-- rules-counter -->
![Static Badge](https://img.shields.io/badge/Total_Rules-78-green?label=Total%20Rules&color=green) ![Static Badge](https://img.shields.io/badge/Cell_Rules-53-green?label=Cell%20Rules&color=green) ![Static Badge](https://img.shields.io/badge/Aggregate_Rules-25-green?label=Aggregate%20Rules&color=green)
![Static Badge](https://img.shields.io/badge/Rules-79-green?label=Total%20Number%20of%20Rules&labelColor=blue&color=gray) ![Static Badge](https://img.shields.io/badge/Rules-54-green?label=Cell%20Rules&labelColor=blue&color=gray) ![Static Badge](https://img.shields.io/badge/Rules-25-green?label=Aggregate%20Rules&labelColor=blue&color=gray)
<!-- /rules-counter -->

## Introduction
Expand Down Expand Up @@ -208,6 +208,10 @@ columns:
is_currency_code: true # Validates an ISO 4217 currency code like GBP or EUR. Case-sensitive. See: https://en.wikipedia.org/wiki/ISO_4217.
is_base64: true # Validate if a string is Base64-encoded. Example: "cmVzcGVjdCE=".

# 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.
is_json: true # Example: {"foo":"bar"}.

# Geography
is_latitude: true # Can be integer or float. Example: 50.123456.
is_longitude: true # Can be integer or float. Example: -89.123456.
Expand Down
1 change: 1 addition & 0 deletions schema-examples/full.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"is_alias" : true,
"is_currency_code" : true,
"is_base64" : true,
"is_json" : true,

"is_latitude" : true,
"is_longitude" : true,
Expand Down
1 change: 1 addition & 0 deletions schema-examples/full.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
'is_alias' => true,
'is_currency_code' => true,
'is_base64' => true,
'is_json' => true,

'is_latitude' => true,
'is_longitude' => true,
Expand Down
4 changes: 4 additions & 0 deletions schema-examples/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ columns:
is_currency_code: true # Validates an ISO 4217 currency code like GBP or EUR. Case-sensitive. See: https://en.wikipedia.org/wiki/ISO_4217.
is_base64: true # Validate if a string is Base64-encoded. Example: "cmVzcGVjdCE=".

# 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.
is_json: true # Example: {"foo":"bar"}.

# Geography
is_latitude: true # Can be integer or float. Example: 50.123456.
is_longitude: true # Can be integer or float. Example: -89.123456.
Expand Down
43 changes: 43 additions & 0 deletions src/Rules/Cell/IsJson.php
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;
}
}
20 changes: 11 additions & 9 deletions tests/ReadmeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,17 @@ public function testBadgeOfRules(): void
$aggRules = \count(yml(Tools::SCHEMA_FULL)->findArray('columns.0.aggregate_rules'));
$totalRules = $cellRules + $aggRules;

$text = \implode('', [
"![Static Badge](https://img.shields.io/badge/Total_Rules-{$totalRules}-" .
'green?label=Total%20Rules&color=green)',
' ',
"![Static Badge](https://img.shields.io/badge/Cell_Rules-{$cellRules}-" .
'green?label=Cell%20Rules&color=green)',
' ',
"![Static Badge](https://img.shields.io/badge/Aggregate_Rules-{$aggRules}-" .
'green?label=Aggregate%20Rules&color=green)',
$badge = static function (string $label, int $count): string {
$label = \str_replace(' ', '%20', $label);

return "![Static Badge](https://img.shields.io/badge/Rules-{$count}-green" .
"?label={$label}&labelColor=blue&color=gray)";
};

$text = \implode(' ', [
$badge('Total Number of Rules', $totalRules),
$badge('Cell Rules', $cellRules),
$badge('Aggregate Rules', $aggRules),
]);

Tools::insertInReadme('rules-counter', $text);
Expand Down
42 changes: 42 additions & 0 deletions tests/Rules/Cell/IsJsonTest.php
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!'),
);
}
}

0 comments on commit 268735a

Please sign in to comment.