Skip to content

Commit

Permalink
Add 'is_isbn' validation rule (#142)
Browse files Browse the repository at this point in the history
Introduced a new cell validation rule named `is_isbn` to validate if a
value is a valid International Standard Book Number (ISBN). The rule has
been added to the schema-examples and implemented in a new class file.
The README.md file and relevant test files have been updated to reflect
this change.
  • Loading branch information
SmetDenis committed Apr 3, 2024
1 parent 0bb2820 commit 7b447c9
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 5 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-330-green?label=Total%20number%20of%20rules&labelColor=darkgreen&color=gray)](schema-examples/full.yml)
[![Static Badge](https://img.shields.io/badge/Rules-116-green?label=Cell%20rules&labelColor=blue&color=gray)](src/Rules/Cell)
[![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-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-18/54/9-green?label=Plan%20to%20add&labelColor=gray&color=gray)](tests/schemas/todo.yml)
[![Static Badge](https://img.shields.io/badge/Rules-17/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 @@ -529,6 +529,7 @@ columns:
is_bic: true # Validates a Bank Identifier Code (BIC) according to ISO 9362 standards. See: https://en.wikipedia.org/wiki/ISO_9362
postal_code: US # Validate postal code by country code (alpha-2). Example: "02179". Extracted from https://www.geonames.org
is_imei: true # Validates an International Mobile Equipment Identity (IMEI). See: https://en.wikipedia.org/wiki/International_Mobile_Station_Equipment_Identity
is_isbn: true # Validates an International Standard Book Number (ISBN). See: https://www.isbn-international.org/content/what-isbn

# Misc
is_version: true # Validates the string as version numbers using Semantic Versioning. Example: "1.2.3".
Expand Down
1 change: 1 addition & 0 deletions schema-examples/full.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
"is_bic" : true,
"postal_code" : "US",
"is_imei" : true,
"is_isbn" : true,

"is_version" : true,
"is_punct" : 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 @@ -171,6 +171,7 @@
'is_bic' => true,
'postal_code' => 'US',
'is_imei' => true,
'is_isbn' => true,

'is_version' => true,
'is_punct' => true,
Expand Down
1 change: 1 addition & 0 deletions schema-examples/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ columns:
is_bic: true # Validates a Bank Identifier Code (BIC) according to ISO 9362 standards. See: https://en.wikipedia.org/wiki/ISO_9362
postal_code: US # Validate postal code by country code (alpha-2). Example: "02179". Extracted from https://www.geonames.org
is_imei: true # Validates an International Mobile Equipment Identity (IMEI). See: https://en.wikipedia.org/wiki/International_Mobile_Station_Equipment_Identity
is_isbn: true # Validates an International Standard Book Number (ISBN). See: https://www.isbn-international.org/content/what-isbn

# Misc
is_version: true # Validates the string as version numbers using Semantic Versioning. Example: "1.2.3".
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 @@ -179,6 +179,7 @@ columns:
is_bic: true
postal_code: US
is_imei: true
is_isbn: true

is_version: true
is_punct: true
Expand Down
49 changes: 49 additions & 0 deletions src/Rules/Cell/IsIsbn.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 IsIsbn extends AbstractCellRule
{
public function getHelpMeta(): array
{
return [
[],
[
self::DEFAULT => [
'true',
'Validates an International Standard Book Number (ISBN). ' .
'See: https://www.isbn-international.org/content/what-isbn',
],
],
];
}

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

if (!Validator::isbn()->validate($cellValue)) {
return "Value \"<c>{$cellValue}</c>\" is not a valid ISBN number.";
}

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

use function JBZoo\PHPUnit\isSame;

final class IsIsbnTest extends TestAbstractCellRule
{
protected string $ruleClass = IsIsbn::class;

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

$valid = [
'',
'ISBN-13: 978-0-596-52068-7',
'978 0 596 52068 7',
'9780596520687',
'0-596-52068-9',
'0 512 52068 9',
'ISBN-10 0-596-52068-9',
'ISBN-10: 0-596-52068-9',
];

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 = [
';',
'!@#$%^&*()',
'ISBN 11978-0-596-52068-7',
'ISBN-12: 978-0-596-52068-7',
'978 10 596 52068 7',
'119780596520687',
'0-5961-52068-9',
'11 5122 52068 9',
'ISBN-11 0-596-52068-9',
'ISBN-10- 0-596-52068-9',
'Defiatly no ISBN',
'Neither ISBN-13: 978-0-596-52068-7',
];

foreach ($invalid as $value) {
isSame(
"Value \"{$value}\" is not a valid ISBN number.",
$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 @@ -30,6 +30,7 @@ structural_rules:
columns_count: 5 # Exact number of columns in the file. By default, it is equal to the number of columns in the schema.
columns_count_max: 5 # Minimum number of columns in the file. By default, it is equal to the number of columns in the schema.
ignore_duplicate_rows: false # If true, then duplicate rows will be ignored. Duplicate rows are rows that have the same values in all columns - 100% match.
ends_with_newline: false # If true, then the file must end with a newline character (\n).


columns:
Expand All @@ -53,8 +54,6 @@ columns:
subdivision_code: [ ] # https://github.com/Respect/Validation/blob/main/docs/rules/SubdivisionCode.md

# ids
is_isbn: true # Validates an International Standard Book Number (ISBN).

is_bsn: true # Validates a Dutch citizen service number (BSN).
is_cnh: true # Validates a Brazilian national health card number (CNH).
is_cnpj: true # Validates a Brazilian company identifier (CNPJ).
Expand Down

0 comments on commit 7b447c9

Please sign in to comment.