Skip to content

Commit

Permalink
Add is_alias validation rule to schema examples
Browse files Browse the repository at this point in the history
A new validation rule is_alias has been added to the schema examples in the full.json, full.php and full.yml files. Due to this addition, a new IsAlias.php file is created under src/Rules, which will validate the aliases present in these files. This function is also tested in Blueprint/RulesTest.php.
  • Loading branch information
Denis Smet committed Mar 13, 2024
1 parent 2757bb1 commit 861cafa
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ columns:
str_ends_with: " suffix" # Case-sensitive. Example: "Hello World suffix"
str_starts_with: "prefix " # Case-sensitive. Example: "prefix Hello World"


# Decimal and integer numbers
min: 10 # Can be integer or float, negative and positive
max: 100.50 # Can be integer or float, negative and positive
Expand All @@ -357,6 +356,7 @@ columns:
is_uuid4: true # Only UUID4 format. Example: "550e8400-e29b-41d4-a716-446655440000"
is_latitude: true # Can be integer or float. Example: 50.123456
is_longitude: true # Can be integer or float. Example: -89.123456
is_alias: true # Only alias format. Example: "my-alias-123"
cardinal_direction: true # Valid cardinal direction. Examples: "N", "S", "NE", "SE", "none", ""
usa_market_name: true # Check if the value is a valid USA market name. Example: "New York, NY"

Expand Down
1 change: 1 addition & 0 deletions schema-examples/full.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"is_uuid4" : true,
"is_latitude" : true,
"is_longitude" : true,
"is_alias" : true,
"cardinal_direction" : true,
"usa_market_name" : 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 @@ -66,6 +66,7 @@
'is_uuid4' => true,
'is_latitude' => true,
'is_longitude' => true,
'is_alias' => true,
'cardinal_direction' => true,
'usa_market_name' => true,
],
Expand Down
2 changes: 1 addition & 1 deletion schema-examples/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ columns:
str_ends_with: " suffix" # Case-sensitive. Example: "Hello World suffix"
str_starts_with: "prefix " # Case-sensitive. Example: "prefix Hello World"


# Decimal and integer numbers
min: 10 # Can be integer or float, negative and positive
max: 100.50 # Can be integer or float, negative and positive
Expand All @@ -79,6 +78,7 @@ columns:
is_uuid4: true # Only UUID4 format. Example: "550e8400-e29b-41d4-a716-446655440000"
is_latitude: true # Can be integer or float. Example: 50.123456
is_longitude: true # Can be integer or float. Example: -89.123456
is_alias: true # Only alias format. Example: "my-alias-123"
cardinal_direction: true # Valid cardinal direction. Examples: "N", "S", "NE", "SE", "none", ""
usa_market_name: true # Check if the value is a valid USA market name. Example: "New York, NY"

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

use JBZoo\Utils\Filter;

final class IsAlias extends AbstarctRule
{
public function validateRule(string $cellValue): ?string
{
if (!$this->getOptionAsBool()) {
return null;
}

$alias = Filter::alias($cellValue);
if ($cellValue !== $alias) {
return "Value \"<c>{$cellValue}</c>\" is not a valid alias. Expected \"<green>{$alias}</green>\".";
}

return null;
}
}
15 changes: 15 additions & 0 deletions tests/Blueprint/RulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use JBZoo\CsvBlueprint\Rules\CardinalDirection;
use JBZoo\CsvBlueprint\Rules\DateFormat;
use JBZoo\CsvBlueprint\Rules\ExactValue;
use JBZoo\CsvBlueprint\Rules\IsAlias;
use JBZoo\CsvBlueprint\Rules\IsBool;
use JBZoo\CsvBlueprint\Rules\IsDomain;
use JBZoo\CsvBlueprint\Rules\IsEmail;
Expand Down Expand Up @@ -849,4 +850,18 @@ public function testMaxWordCount(): void
\strip_tags((string)$rule->validate('asd, asdasd asd 1232 asdas')),
);
}

public function testIsAlias(): void
{
$rule = new IsAlias('prop', true);
isSame(null, $rule->validate(''));
isSame(null, $rule->validate('123'));

$rule = new IsAlias('prop', true);
isSame(
'"is_alias" at line 0, column "prop". ' .
'Value "Qwerty, asd 123" is not a valid alias. Expected "qwerty-asd-123".',
\strip_tags((string)$rule->validate('Qwerty, asd 123')),
);
}
}

0 comments on commit 861cafa

Please sign in to comment.