Skip to content

Commit

Permalink
Add 'is_hex_rgb_color' validation rule (#143)
Browse files Browse the repository at this point in the history
A new validation rule called 'is_hex_rgb_color' has been added to the
CSV Blueprint parsing tool. This update allows for RGB color codes in
hexadecimal format to be validated. Changes have also been made to the
README tutorial and enforcement logic for this validation rule. Tests
have been created to ensure its proper functionality.
  • Loading branch information
SmetDenis committed Apr 3, 2024
1 parent 7b447c9 commit ef6f379
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 11 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
<!-- auto-update:/top-badges -->

<!-- auto-update:rules-counter -->
[![Static Badge](https://img.shields.io/badge/Rules-331-green?label=Total%20number%20of%20rules&labelColor=darkgreen&color=gray)](schema-examples/full.yml)
[![Static Badge](https://img.shields.io/badge/Rules-117-green?label=Cell%20rules&labelColor=blue&color=gray)](src/Rules/Cell)
[![Static Badge](https://img.shields.io/badge/Rules-332-green?label=Total%20number%20of%20rules&labelColor=darkgreen&color=gray)](schema-examples/full.yml)
[![Static Badge](https://img.shields.io/badge/Rules-118-green?label=Cell%20rules&labelColor=blue&color=gray)](src/Rules/Cell)
[![Static Badge](https://img.shields.io/badge/Rules-206-green?label=Aggregate%20rules&labelColor=blue&color=gray)](src/Rules/Aggregate)
[![Static Badge](https://img.shields.io/badge/Rules-8-green?label=Extra%20checks&labelColor=blue&color=gray)](#extra-checks)
[![Static Badge](https://img.shields.io/badge/Rules-17/54/10-green?label=Plan%20to%20add&labelColor=gray&color=gray)](tests/schemas/todo.yml)
[![Static Badge](https://img.shields.io/badge/Rules-16/54/10-green?label=Plan%20to%20add&labelColor=gray&color=gray)](tests/schemas/todo.yml)
<!-- auto-update:/rules-counter -->

A console utility designed for validating CSV files against a strictly defined schema and validation rules outlined
Expand Down Expand Up @@ -538,6 +538,7 @@ columns:
is_consonant: true # Validates if the input contains only consonants. Example: "bcd".
is_alnum: true # Validates whether the input is only alphanumeric. Example: "aBc123".
is_alpha: true # This is similar to `is_alnum`, but it does not allow numbers. Example: "aBc".
is_hex_rgb_color: true # Validates weather the input is a hex RGB color or not. Examples: "#FF0000", "#123", "ffffff", "fff".

# Check if the value is a valid hash. Supported algorithms:
# - md2, md4, md5, sha1, sha224, sha256, sha384, sha512/224, sha512/256, sha512
Expand Down
2 changes: 2 additions & 0 deletions schema-examples/full.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@
"is_consonant" : true,
"is_alnum" : true,
"is_alpha" : true,
"is_hex_rgb_color" : true,

"hash" : "set_algo",
"charset" : "charset_code",
"credit_card" : "Any"
Expand Down
13 changes: 7 additions & 6 deletions schema-examples/full.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,13 @@
'is_imei' => true,
'is_isbn' => true,

'is_version' => true,
'is_punct' => true,
'is_vowel' => true,
'is_consonant' => true,
'is_alnum' => true,
'is_alpha' => true,
'is_version' => true,
'is_punct' => true,
'is_vowel' => true,
'is_consonant' => true,
'is_alnum' => true,
'is_alpha' => true,
'is_hex_rgb_color' => true,

'hash' => 'set_algo',
'charset' => 'charset_code',
Expand Down
1 change: 1 addition & 0 deletions schema-examples/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ columns:
is_consonant: true # Validates if the input contains only consonants. Example: "bcd".
is_alnum: true # Validates whether the input is only alphanumeric. Example: "aBc123".
is_alpha: true # This is similar to `is_alnum`, but it does not allow numbers. Example: "aBc".
is_hex_rgb_color: true # Validates weather the input is a hex RGB color or not. Examples: "#FF0000", "#123", "ffffff", "fff".

# Check if the value is a valid hash. Supported algorithms:
# - md2, md4, md5, sha1, sha224, sha256, sha384, sha512/224, sha512/256, sha512
Expand Down
1 change: 1 addition & 0 deletions schema-examples/full_clean.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ columns:
is_consonant: true
is_alnum: true
is_alpha: true
is_hex_rgb_color: true

hash: set_algo

Expand Down
49 changes: 49 additions & 0 deletions src/Rules/Cell/IsHexRgbColor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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 IsHexRgbColor extends AbstractCellRule
{
public function getHelpMeta(): array
{
return [
[],
[
self::DEFAULT => [
'true',
'Validates weather the input is a hex RGB color or not. ' .
'Examples: "#FF0000", "#123", "ffffff", "fff".',
],
],
];
}

public function validateRule(string $cellValue): ?string
{
if ($cellValue === '') {
return null;
}

if (!Validator::hexRgbColor()->validate($cellValue)) {
return "Value \"<c>{$cellValue}</c>\" is not a valid hex RGB color.";
}

return null;
}
}
81 changes: 81 additions & 0 deletions tests/Rules/Cell/IsHexRgbColorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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\IsHexRgbColor;
use JBZoo\PHPUnit\Rules\TestAbstractCellRule;

use function JBZoo\PHPUnit\isSame;

final class IsHexRgbColorTest extends TestAbstractCellRule
{
protected string $ruleClass = IsHexRgbColor::class;

public function testPositive(): void
{
$rule = $this->create(true);
isSame(null, $rule->validate(''));

$valid = [
'',
'#000',
'#00000F',
'#00000f',
'#123',
'#123456',
'#FFFFFF',
'#ffffff',
'123123',
'FFFFFF',
'ffffff',
'443',
];

foreach ($valid as $value) {
isSame('', $rule->test($value), "\"{$value}\"");
}

$rule = $this->create(false);
isSame(null, $rule->validate(' 1'));
}

public function testNegative(): void
{
$rule = $this->create(true);

$invalid = [
';',
'!@#$%^&*()',
'#0',
'#0000G0',
'#0FG',
'#1234',
'#AAAAAA1',
'#S',
'1234',
'foo',
];

foreach ($invalid as $value) {
isSame(
"Value \"{$value}\" is not a valid hex RGB color.",
$rule->test($value),
$value,
);
}
}
}
3 changes: 1 addition & 2 deletions tests/schemas/todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ columns:
is_portuguese_nif: true # Validates a Portuguese taxpayer number (NIF).

# Strings
is_hex_rgb_color: true
no_whitespace: true
no_whitespace: t rue

custom_func: callbak function

Expand Down

0 comments on commit ef6f379

Please sign in to comment.