Skip to content

Commit

Permalink
Add country_code validation rule (#45)
Browse files Browse the repository at this point in the history
Implemented a new CountryCode rule which validates the input against the
ISO 3166-1 standard. This new rule is applied in full.json, full.php,
and full.yml files, and is supported by a new source code file,
CountryCode.php, and its corresponding test file, CountryCodeTest.php.
Composer dependencies were updated to accommodate this enhancement.
  • Loading branch information
SmetDenis committed Mar 17, 2024
1 parent e474a5a commit c55358c
Show file tree
Hide file tree
Showing 9 changed files with 281 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .phan.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
'vendor/jbzoo/data/src',
'vendor/jbzoo/utils/src',
'vendor/league/csv/src',
'vendor/markrogoyski/math-php/src',
'vendor/symfony/console',
'vendor/symfony/finder',
'vendor/markrogoyski/math-php/src',
'vendor/respect/validation',
],
]);
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ columns:
is_cardinal_direction: true # Valid cardinal direction. Examples: "N", "S", "NE", "SE", "none", "".
is_usa_market_name: true # Check if the value is a valid USA market name. Example: "New York, NY".

# Validates whether the input is a country code in ISO 3166-1 standard.
# Available options: "alpha-2" (Ex: "US"), "alpha-3" (Ex: "USA"), "numeric" (Ex: "840").
# The rule uses data from iso-codes: https://salsa.debian.org/iso-codes-team/iso-codes.
country_code: alpha-2 # Country code in ISO 3166-1 standard. Examples: "US", "USA", "840".


####################################################################################################################
# Data validation for the entire(!) column using different data aggregation methods.
Expand Down
25 changes: 11 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,21 @@
"require" : {
"php" : "^8.1",
"ext-mbstring" : "*",

"league/csv" : "^9.15",

"jbzoo/data" : "^7.1",
"jbzoo/cli" : "^7.1",
"jbzoo/utils" : "^7.1",
"jbzoo/ci-report-converter" : "^7.2",

"symfony/yaml" : ">=6.4",
"symfony/filesystem" : ">=6.4",
"symfony/finder" : ">=6.4",

"markrogoyski/math-php" : "^2.9"
"league/csv" : "^9.15.0",
"jbzoo/data" : "^7.1.1",
"jbzoo/cli" : "^7.1.8",
"jbzoo/utils" : "^7.1.2",
"jbzoo/ci-report-converter" : "^7.2.1",
"symfony/yaml" : ">=6.4.3",
"symfony/filesystem" : ">=6.4.3",
"symfony/finder" : ">=6.4.0",
"markrogoyski/math-php" : "^2.9.0",
"respect/validation" : "^2.3.5"
},

"require-dev" : {
"roave/security-advisories" : "dev-latest",
"jbzoo/toolbox-dev" : "^7.1"
"jbzoo/toolbox-dev" : "^7.1.0"
},

"bin" : ["csv-blueprint"],
Expand Down
124 changes: 123 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion schema-examples/full.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@
"is_longitude" : true,
"is_geohash" : true,
"is_cardinal_direction" : true,
"is_usa_market_name" : true
"is_usa_market_name" : true,

"country_code" : "alpha-2"
},

"aggregate_rules" : {
Expand Down
2 changes: 2 additions & 0 deletions schema-examples/full.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
'is_geohash' => true,
'is_cardinal_direction' => true,
'is_usa_market_name' => true,

'country_code' => 'alpha-2',
],

'aggregate_rules' => [
Expand Down
5 changes: 5 additions & 0 deletions schema-examples/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ columns:
is_cardinal_direction: true # Valid cardinal direction. Examples: "N", "S", "NE", "SE", "none", "".
is_usa_market_name: true # Check if the value is a valid USA market name. Example: "New York, NY".

# Validates whether the input is a country code in ISO 3166-1 standard.
# Available options: "alpha-2" (Ex: "US"), "alpha-3" (Ex: "USA"), "numeric" (Ex: "840").
# The rule uses data from iso-codes: https://salsa.debian.org/iso-codes-team/iso-codes.
country_code: alpha-2 # Country code in ISO 3166-1 standard. Examples: "US", "USA", "840".


####################################################################################################################
# Data validation for the entire(!) column using different data aggregation methods.
Expand Down
59 changes: 59 additions & 0 deletions src/Rules/Cell/CountryCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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\Rules\CountryCode as RespectCountryCode;
use Respect\Validation\Validator;

class CountryCode extends AbstractCellRule
{
protected const HELP_TOP = [
'Validates whether the input is a country code in ISO 3166-1 standard.',
'Available options: "alpha-2" (Ex: "US"), "alpha-3" (Ex: "USA"), "numeric" (Ex: "840").',
'The rule uses data from iso-codes: https://salsa.debian.org/iso-codes-team/iso-codes.',
];

protected const HELP_OPTIONS = [
self::DEFAULT => ['alpha-2', 'Country code in ISO 3166-1 standard. Examples: "US", "USA", "840"'],
];

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

$validSets = [
RespectCountryCode::ALPHA2,
RespectCountryCode::ALPHA3,
RespectCountryCode::NUMERIC,
];

$set = $this->getOptionAsString();

if (!\in_array($set, $validSets, true)) {
return "Unknown country set: \"<c>{$set}</c>\". " .
'Available options: [<green>' . \implode(', ', $validSets) . '</green>]';
}

if (!Validator::countryCode($set)->validate($cellValue)) {
return "Value \"<c>{$cellValue}</c>\" is not a valid {$set} country code.";
}

return null;
}
}
71 changes: 71 additions & 0 deletions tests/Rules/Cell/CountryCodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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\CountryCode;
use JBZoo\PHPUnit\Rules\AbstractCellRule;
use Respect\Validation\Rules\CountryCode as RespectCountryCode;

use function JBZoo\PHPUnit\isSame;

final class CountryCodeTest extends AbstractCellRule
{
protected string $ruleClass = CountryCode::class;

public function testPositive(): void
{
$rule = $this->create(RespectCountryCode::ALPHA2);
isSame('', $rule->test(''));
isSame('', $rule->test('US'));

$rule = $this->create(RespectCountryCode::ALPHA3);
isSame('', $rule->test('USA'));

$rule = $this->create(RespectCountryCode::NUMERIC);
isSame('', $rule->test('840'));
}

public function testNegative(): void
{
$rule = $this->create(RespectCountryCode::ALPHA2);
isSame(
'Value "qq" is not a valid alpha-2 country code.',
$rule->test('qq'),
);

$rule = $this->create(RespectCountryCode::ALPHA3);
isSame(
'Value "QQQ" is not a valid alpha-3 country code.',
$rule->test('QQQ'),
);

$rule = $this->create(RespectCountryCode::NUMERIC);
isSame(
'Value "101010101" is not a valid numeric country code.',
$rule->test('101010101'),
);
}

public function testInvalidOption(): void
{
$rule = $this->create('qwerty');
isSame(
'Unknown country set: "qwerty". Available options: [alpha-2, alpha-3, numeric]',
$rule->test('US'),
);
}
}

0 comments on commit c55358c

Please sign in to comment.