Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'credit_card' validation #136

Merged
merged 1 commit into from
Apr 3, 2024
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
12 changes: 8 additions & 4 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-324-green?label=Total%20number%20of%20rules&labelColor=darkgreen&color=gray)](schema-examples/full.yml)
[![Static Badge](https://img.shields.io/badge/Rules-110-green?label=Cell%20rules&labelColor=blue&color=gray)](src/Rules/Cell)
[![Static Badge](https://img.shields.io/badge/Rules-325-green?label=Total%20number%20of%20rules&labelColor=darkgreen&color=gray)](schema-examples/full.yml)
[![Static Badge](https://img.shields.io/badge/Rules-111-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-26/54/9-green?label=Plan%20to%20add&labelColor=gray&color=gray)](tests/schemas/todo.yml)
[![Static Badge](https://img.shields.io/badge/Rules-24/54/9-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 @@ -553,6 +553,10 @@ columns:
# - UTF-32BE, UTF-32LE, UTF7-IMAP, UUENCODE, Windows-1251, Windows-1252, Windows-1254, eucJP-win
charset: charset_code # Validates if a string is in a specific charset. Example: "UTF-8".

# Validates whether the input is a credit card number.
# Available credit card brands: "Any", "American Express", "Diners Club", "Discover", "JCB", "MasterCard", "Visa", "RuPay".
credit_card: Any # Example: "5376-7473-9720-8720"

####################################################################################################################
# Data validation for the entire(!) column using different data aggregation methods.
# Every rule is optional.
Expand Down Expand Up @@ -932,7 +936,7 @@ Usage:
Options:
-c, --csv=CSV Specify the path(s) to the CSV files you want to validate.
This can include a direct path to a file or a directory to search with a maximum depth of 10 levels.
Examples: :/full/path/name.csv; p/file.csv; p/*.csv; p/**/*.csv; p/**/name-*.csv; **/*.csv
Examples: /full/path/name.csv; p/file.csv; p/*.csv; p/**/*.csv; p/**/name-*.csv; **/*.csv
(multiple values allowed)
-s, --schema=SCHEMA Specify the path(s) to the schema file(s), supporting YAML, JSON, or PHP formats.
Similar to CSV paths, you can direct to specific files or search directories with glob patterns.
Expand Down
3 changes: 2 additions & 1 deletion schema-examples/full.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@
"is_alnum" : true,
"is_alpha" : true,
"hash" : "set_algo",
"charset" : "charset_code"
"charset" : "charset_code",
"credit_card" : "Any"
},
"aggregate_rules" : {
"is_unique" : true,
Expand Down
2 changes: 2 additions & 0 deletions schema-examples/full.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@

'hash' => 'set_algo',
'charset' => 'charset_code',

'credit_card' => 'Any',
],

'aggregate_rules' => [
Expand Down
4 changes: 4 additions & 0 deletions schema-examples/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ columns:
# - UTF-32BE, UTF-32LE, UTF7-IMAP, UUENCODE, Windows-1251, Windows-1252, Windows-1254, eucJP-win
charset: charset_code # Validates if a string is in a specific charset. Example: "UTF-8".

# Validates whether the input is a credit card number.
# Available credit card brands: "Any", "American Express", "Diners Club", "Discover", "JCB", "MasterCard", "Visa", "RuPay".
credit_card: Any # Example: "5376-7473-9720-8720"

####################################################################################################################
# Data validation for the entire(!) column using different data aggregation methods.
# Every rule is optional.
Expand Down
2 changes: 2 additions & 0 deletions schema-examples/full_clean.yml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ columns:

charset: charset_code

credit_card: Any

aggregate_rules:
is_unique: true
sorted: [ asc, natural ]
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/ValidateCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function configure(): void
'Specify the path(s) to the CSV files you want to validate.',
'This can include a direct path to a file or a directory to search with a maximum depth of ' .
Utils::MAX_DIRECTORY_DEPTH . ' levels.',
'Examples: :<info>' . \implode('</info>; <info>', [
'Examples: <info>' . \implode('</info>; <info>', [
'/full/path/name.csv',
'p/file.csv',
'p/*.csv',
Expand Down
63 changes: 63 additions & 0 deletions src/Rules/Cell/CreditCard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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\CreditCard as RespectCreditCard;
use Respect\Validation\Validator;

final class CreditCard extends AbstractCellRule
{
private const BRANDS = [
RespectCreditCard::ANY,
RespectCreditCard::AMERICAN_EXPRESS,
RespectCreditCard::DINERS_CLUB,
RespectCreditCard::DISCOVER,
RespectCreditCard::JCB,
RespectCreditCard::MASTERCARD,
RespectCreditCard::VISA,
RespectCreditCard::RUPAY,
];

public function getHelpMeta(): array
{
return [
[
'Validates whether the input is a credit card number.',
'Available credit card brands: "' . \implode('", "', self::BRANDS) . '".',
],
[self::DEFAULT => [RespectCreditCard::ANY, 'Example: "5376-7473-9720-8720"']],
];
}

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

$brand = $this->getOptionAsString();
if ($brand === '') {
return 'The brand is required. Example: "Any", "Visa", "MasterCard"';
}

if (!Validator::creditCard($brand)->validate($cellValue)) {
return "The value \"<c>{$cellValue}</c>\" has invalid credit card format for brand \"{$brand}\".";
}

return null;
}
}
2 changes: 1 addition & 1 deletion tests/ReadmeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class ReadmeTest extends TestCase
'CSV file, ensuring schema conformity.',
];

public function testCalidateCsvHelp(): void
public function testValidateCsvHelp(): void
{
$text = \implode("\n", [
'```text',
Expand Down
52 changes: 52 additions & 0 deletions tests/Rules/Cell/CreditCardTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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\CreditCard;
use JBZoo\PHPUnit\Rules\TestAbstractCellRule;

use function JBZoo\PHPUnit\isSame;

final class CreditCardTest extends TestAbstractCellRule
{
protected string $ruleClass = CreditCard::class;

public function testPositive(): void
{
$rule = $this->create('Any');
isSame('', $rule->test(''));
isSame('', $rule->test('5376-7473-9720-8720'));
isSame('', $rule->test('5376747397208720'));
}

public function testNegative(): void
{
$rule = $this->create('Any');
isSame(
'The value "qwerty" has invalid credit card format for brand "Any".',
$rule->test('qwerty'),
);

$rule = $this->create('Qwerty');
isSame(
'"credit_card" at line <red>1</red>, column "prop". ' .
'Unexpected error: "Qwerty" is not a valid credit card brand ' .
'(Available: Any, American Express, Diners Club, Discover, JCB, MasterCard, Visa, RuPay).',
(string)$rule->validate('qwerty'),
);
}
}
78 changes: 0 additions & 78 deletions tests/Rules/Cell/IsPhoneTest.php

This file was deleted.

2 changes: 0 additions & 2 deletions tests/schemas/todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ columns:
subdivision_code: [ ] # https://github.com/Respect/Validation/blob/main/docs/rules/SubdivisionCode.md

# ids
is_credit_card: brands[] # https://github.com/Respect/Validation/blob/main/docs/rules/CreditCard.md
is_postal_code: country code # https://github.com/Respect/Validation/blob/main/docs/rules/PostalCode.md
is_bsn: true
is_cnh: true
Expand All @@ -72,7 +71,6 @@ columns:
is_portuguese_nif: true
is_bic: true
is_iban: true
is_card_number: true

# Strings
is_hex_rgb_color: true
Expand Down