Skip to content

Commit

Permalink
Updated validation rule naming and added 'is_angle' rule (#98)
Browse files Browse the repository at this point in the history
This commit renames 'is_alias' validation rule to 'is_slug' and adds
'is_angle' rule across schema-example files and source code. The
'is_angle' rule checks if the cell value is a valid angle ranging from
0.0 to 360.0. Minor fixes in string format for consistency are also
included in this commit.
  • Loading branch information
SmetDenis committed Mar 26, 2024
1 parent d1771ce commit b43abcd
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 35 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![GitHub Release](https://img.shields.io/github/v/release/jbzoo/csv-blueprint?label=Latest)](https://github.com/jbzoo/csv-blueprint/releases) [![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/tags) [![Docker Image Size](https://img.shields.io/docker/image-size/jbzoo/csv-blueprint)](https://hub.docker.com/r/jbzoo/csv-blueprint/tags)

<!-- rules-counter -->
[![Static Badge](https://img.shields.io/badge/Rules-277-green?label=Total%20number%20of%20rules&labelColor=darkgreen&color=gray)](schema-examples/full.yml) [![Static Badge](https://img.shields.io/badge/Rules-66-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-5-green?label=Extra%20checks&labelColor=blue&color=gray)](#extra-checks) [![Static Badge](https://img.shields.io/badge/Rules-227-green?label=Plan%20to%20add&labelColor=gray&color=gray)](tests/schemas/todo.yml)
[![Static Badge](https://img.shields.io/badge/Rules-278-green?label=Total%20number%20of%20rules&labelColor=darkgreen&color=gray)](schema-examples/full.yml) [![Static Badge](https://img.shields.io/badge/Rules-67-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-5-green?label=Extra%20checks&labelColor=blue&color=gray)](#extra-checks) [![Static Badge](https://img.shields.io/badge/Rules-211-green?label=Plan%20to%20add&labelColor=gray&color=gray)](tests/schemas/todo.yml)
<!-- /rules-counter -->

## Introduction
Expand Down Expand Up @@ -242,9 +242,10 @@ columns:
is_email: true # Only email format. Example: "user@example.com".
is_domain: true # Only domain name. Example: "example.com".
is_uuid: true # Validates whether the input is a valid UUID. It also supports validation of specific versions 1, 3, 4 and 5.
is_alias: true # Only alias format. Example: "my-alias-123". It can contain letters, numbers, and dashes.
is_slug: true # Only slug format. Example: "my-slug-123". It can contain letters, numbers, and dashes.
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=".
is_angle: true # Check if the cell value is a valid angle (0.0 to 360.0).

# 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.
Expand Down Expand Up @@ -898,7 +899,6 @@ It's random ideas and plans. No orderings and deadlines. <u>But batch processing
* Warnings about deprecated options and features.
* Add option `--recomendation` to show a list of recommended rules for the schema or potential issues in the CSV file or schema. It's useful when you are not sure what rules to use.
* Add option `--error=[level]` to show only errors with a specific level. It's useful when you have a lot of warnings and you want to see only errors.
* Move `const:HELP` to PHP annotations. Canonic way to describe the command.
* S3 Storage support. Validate files in the S3 bucket? Hmm... Why not? But...
* More examples and documentation.

Expand Down
3 changes: 2 additions & 1 deletion schema-examples/full.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@
"is_email" : true,
"is_domain" : true,
"is_uuid" : true,
"is_alias" : true,
"is_slug" : true,
"is_currency_code" : true,
"is_base64" : true,
"is_angle" : true,
"is_json" : true,
"is_latitude" : true,
"is_longitude" : true,
Expand Down
3 changes: 2 additions & 1 deletion schema-examples/full.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@
'is_email' => true,
'is_domain' => true,
'is_uuid' => true,
'is_alias' => true,
'is_slug' => true,
'is_currency_code' => true,
'is_base64' => true,
'is_angle' => true,
'is_json' => true,
'is_latitude' => true,
'is_longitude' => true,
Expand Down
3 changes: 2 additions & 1 deletion schema-examples/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,10 @@ columns:
is_email: true # Only email format. Example: "user@example.com".
is_domain: true # Only domain name. Example: "example.com".
is_uuid: true # Validates whether the input is a valid UUID. It also supports validation of specific versions 1, 3, 4 and 5.
is_alias: true # Only alias format. Example: "my-alias-123". It can contain letters, numbers, and dashes.
is_slug: true # Only slug format. Example: "my-slug-123". It can contain letters, numbers, and dashes.
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=".
is_angle: true # Check if the cell value is a valid angle (0.0 to 360.0).

# 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.
Expand Down
3 changes: 2 additions & 1 deletion schema-examples/full_clean.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ columns:
is_email: true
is_domain: true
is_uuid: true
is_alias: true
is_slug: true
is_currency_code: true
is_base64: true
is_angle: true
is_json: true
is_latitude: true
is_longitude: true
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Cell/DateFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function validateRule(string $cellValue): ?string
$date = \DateTimeImmutable::createFromFormat($expectedDateFormat, $cellValue);
if ($date === false || $date->format($expectedDateFormat) !== $cellValue) {
return "Date format of value \"<c>{$cellValue}</c>\" is not valid. " .
"Expected format: \"<green>{$expectedDateFormat}<green>\"";
"Expected format: \"<green>{$expectedDateFormat}</green>\"";
}

return null;
Expand Down
6 changes: 5 additions & 1 deletion src/Rules/Cell/ExactValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ public function getHelpMeta(): array

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

if ($this->getOptionAsString() !== $cellValue) {
return "Value \"<c>{$cellValue}</c>\" is not strict equal to " .
"\"<green>{$this->getOptionAsString()}<green>\"";
"\"<green>{$this->getOptionAsString()}</green>\"";
}

return null;
Expand Down
48 changes: 48 additions & 0 deletions src/Rules/Cell/IsAngle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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;

final class IsAngle extends IsFloat
{
private float $min = 0.0;
private float $max = 360.0;

public function getHelpMeta(): array
{
return [
[],
[
self::DEFAULT => ['true', 'Check if the cell value is a valid angle (0.0 to 360.0).'],
],
];
}

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

$angle = (float)$cellValue;
if ($angle < $this->min || $angle > $this->max) {
return "Value \"<c>{$cellValue}</c>\" is not a valid angle <green>({$this->min} to {$this->max})</green>";
}

return null;
}
}
11 changes: 5 additions & 6 deletions src/Rules/Cell/IsAlias.php → src/Rules/Cell/IsSlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

namespace JBZoo\CsvBlueprint\Rules\Cell;

use JBZoo\Utils\Filter;
use Respect\Validation\Validator;

final class IsAlias extends AbstractCellRule
final class IsSlug extends AbstractCellRule
{
public function getHelpMeta(): array
{
Expand All @@ -27,17 +27,16 @@ public function getHelpMeta(): array
[
self::DEFAULT => [
'true',
'Only alias format. Example: "my-alias-123". It can contain letters, numbers, and dashes.',
'Only slug format. Example: "my-slug-123". It can contain letters, numbers, and dashes.',
],
],
];
}

public function validateRule(string $cellValue): ?string
{
$alias = Filter::alias($cellValue);
if ($cellValue !== $alias) {
return "Value \"<c>{$cellValue}</c>\" is not a valid alias. Expected \"<green>{$alias}</green>\".";
if (!Validator::slug()->validate($cellValue)) {
return "Value \"{$cellValue}\" is not a valid slug. Expected format \"<green>my-slug-123</green>\"";
}

return null;
Expand Down
5 changes: 1 addition & 4 deletions tests/Rules/Cell/ExactValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,13 @@ final class ExactValueTest extends TestAbstractCellRule
public function testPositive(): void
{
$rule = $this->create('123');
isSame('', $rule->test(''));
isSame('', $rule->test('123'));
}

public function testNegative(): void
{
$rule = $this->create('123');
isSame(
'Value "" is not strict equal to "123"',
$rule->test(''),
);
isSame(
'Value "2000-01-02" is not strict equal to "123"',
$rule->test('2000-01-02'),
Expand Down
56 changes: 56 additions & 0 deletions tests/Rules/Cell/IsAngleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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\IsAngle;
use JBZoo\PHPUnit\Rules\TestAbstractCellRule;

use function JBZoo\PHPUnit\isSame;

final class IsAngleTest extends TestAbstractCellRule
{
protected string $ruleClass = IsAngle::class;

public function testPositive(): void
{
$rule = $this->create(true);
isSame('', $rule->test('0'));
isSame('', $rule->test('90'));
isSame('', $rule->test('360.0'));
isSame('', $rule->test('360'));

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

public function testNegative(): void
{
$rule = $this->create(true);
isSame(
'Value "1230" is not a valid angle (0 to 360)',
$rule->test('1230'),
);
isSame(
'Value "-0.1" is not a valid angle (0 to 360)',
$rule->test('-0.1'),
);
isSame(
'Value "90.1.1.1.1" is not a float number',
$rule->test('90.1.1.1.1'),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@

namespace JBZoo\PHPUnit\Rules\Cell;

use JBZoo\CsvBlueprint\Rules\Cell\IsAlias;
use JBZoo\CsvBlueprint\Rules\Cell\IsSlug;
use JBZoo\PHPUnit\Rules\TestAbstractCellRule;

use function JBZoo\PHPUnit\isSame;

final class IsAliasTest extends TestAbstractCellRule
final class IsSlugTest extends TestAbstractCellRule
{
protected string $ruleClass = IsAlias::class;
protected string $ruleClass = IsSlug::class;

public function testPositive(): void
{
$rule = $this->create(true);
isSame('', $rule->test(''));
isSame('Value "" is not a valid slug. Expected format "my-slug-123"', $rule->test(''));
isSame('', $rule->test('123'));

$rule = $this->create(false);
Expand All @@ -39,7 +39,7 @@ public function testNegative(): void
{
$rule = $this->create(true);
isSame(
'Value "Qwerty, asd 123" is not a valid alias. Expected "qwerty-asd-123".',
'Value "Qwerty, asd 123" is not a valid slug. Expected format "my-slug-123"',
$rule->test('Qwerty, asd 123'),
);
}
Expand Down
17 changes: 6 additions & 11 deletions tests/schemas/todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ columns:

rules:
# https://github.com/Respect/Validation/blob/main/docs/08-list-of-rules-by-category.md
is_credit_card: brands[] # https://github.com/Respect/Validation/blob/main/docs/rules/CreditCard.md
is_iban: true
is_bool_value: true # https://github.com/Respect/Validation/blob/main/docs/rules/BoolVal.md

# Dates
Expand All @@ -66,6 +64,8 @@ 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_iban: true
postal_code: country code # https://github.com/Respect/Validation/blob/main/docs/rules/PostalCode.md
is_bsn: true
is_cnh: true
Expand Down Expand Up @@ -194,38 +194,33 @@ columns:
# Math
is_fibonacci: true
is_prime_number: true
is_positive: true
is_negative: true
is_zero: true
is_digit: true
is_digits: true
is_even: true
is_odd: true
is_roman: true
is_perfect_square: true

# Strings
is_alnum: true
is_alpha: true
is_charset: true
is_hex_rgb_color: true
no_whitespace: true
is_phone: true
is_punct: true
is_slug: true
is_space(s): true
is_spaces: true
is_space: true
is_regex: true
is_version: true
is_vowel: true
is_consonant: true
is_xdigit: true
is_angle: # 0.0-360.0

custom_func: callbak function


aggregate_rules:
count_true: 1
count_false: 1

# https://github.com/markrogoyski/math-php#statistics---averages
truncated_mean: [ 1, 25 ] # 25 percent of observations trimmed from each end of distribution
generalized_mean: [ 1, 2 ] # p-power mean
Expand Down

0 comments on commit b43abcd

Please sign in to comment.