Skip to content

Commit

Permalink
Merge f6e90b8 into 9fd89b7
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Mar 11, 2024
2 parents 9fd89b7 + f6e90b8 commit 9cd1226
Show file tree
Hide file tree
Showing 32 changed files with 210 additions and 188 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ demo-invalid: ##@Project Run demo invalid CSV
$(call title,"Demo - Invalid CSV")
@${PHP_BIN} ./csv-blueprint validate:csv \
--csv=./tests/fixtures/demo.csv \
--schema=./tests/schemas/demo_invalid.yml
--schema=./tests/schemas/demo_invalid.yml \
--output=$(OUTPUT)


demo-github: ##@Project Run demo invalid CSV
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/ValidateCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function executeAction(): int
if ($this->isTextMode()) {
$this->_(
'<yellow>CSV file is not valid!</yellow> ' .
'Found <yellow>' . $errorSuite->count() . '<yellow> errors.',
'Found <yellow>' . $errorSuite->count() . '</yellow> errors.',
OutLvl::E,
);
}
Expand Down Expand Up @@ -113,6 +113,7 @@ private function getSchemaFilepath(): string

if ($this->isTextMode()) {
$this->_('<blue>Schema :</blue> ' . \realpath($schemaFilename));
$this->_('');
}

return $schemaFilename;
Expand Down
2 changes: 1 addition & 1 deletion src/Csv/CsvFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private function validateHeader(): ErrorSuite
if ($column->getName() === '') {
$error = new Error(
'csv.header',
"Property \"name\" is not defined in schema: \"{$this->schema->getFilename()}\"",
"Property \"<c>name</c>\" is not defined in schema: \"<c>{$this->schema->getFilename()}</c>\"",
$column->getHumanName(),
1,
);
Expand Down
13 changes: 11 additions & 2 deletions src/Validators/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(

public function __toString(): string
{
return "\"{$this->getRuleCode()}\" at line {$this->getLine()}, " .
return "\"{$this->getRuleCode()}\" at line <red>{$this->getLine()}</red>, " .
"column \"{$this->getColumnName()}\". {$this->getMessage()}.";
}

Expand All @@ -37,8 +37,12 @@ public function getRuleCode(): string
return $this->ruleCode;
}

public function getMessage(): string
public function getMessage(bool $noTags = false): string
{
if ($noTags) {
return \strip_tags(\rtrim($this->message));
}

return \rtrim($this->message, '.');
}

Expand All @@ -52,6 +56,11 @@ public function getLine(): int
return $this->line;
}

public function toCleanString(): string
{
return \strip_tags((string)$this);
}

public function toArray(): array
{
return [
Expand Down
9 changes: 7 additions & 2 deletions src/Validators/ErrorSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,12 @@ private function renderTable(): string
->setColumnMaxWidth(3, 60);

foreach ($this->errors as $error) {
$table->addRow([$error->getLine(), $error->getColumnName(), $error->getRuleCode(), $error->getMessage()]);
$table->addRow([
$error->getLine(),
$error->getColumnName(),
$error->getRuleCode(),
$error->getMessage(true),
]);
}

$table->render();
Expand All @@ -161,7 +166,7 @@ private function prepareSourceSuite(): SourceSuite
$case = $suite->addTestCase($caseName);
$case->line = $error->getLine();
$case->file = $this->csvFilename;
$case->errOut = (string)$error;
$case->errOut = $error->toCleanString();
}

return $suite;
Expand Down
4 changes: 2 additions & 2 deletions src/Validators/Rules/AllowValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public function validateRule(?string $cellValue): ?string
}

if (!\in_array($cellValue, $allowedValues, true)) {
return "Value \"{$cellValue}\" is not allowed. " .
'Allowed values: ["' . \implode('", "', $allowedValues) . '"]';
return "Value \"<c>{$cellValue}</c>\" is not allowed. " .
'Allowed values: <green>["' . \implode('", "', $allowedValues) . '"]</green>';
}

return null;
Expand Down
3 changes: 2 additions & 1 deletion src/Validators/Rules/DateFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function validateRule(?string $cellValue): ?string

$date = \DateTimeImmutable::createFromFormat($expectedDateFormat, $cellValue);
if ($date === false || $date->format($expectedDateFormat) !== $cellValue) {
return "Date format of value \"{$cellValue}\" is not valid. Expected format: \"{$expectedDateFormat}\"";
return "Date format of value \"<c>{$cellValue}</c>\" is not valid. " .
"Expected format: \"<green>{$expectedDateFormat}<green>\"";
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/ExactValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class ExactValue extends AbstarctRule
public function validateRule(?string $cellValue): ?string
{
if ($this->getOptionAsString() !== (string)$cellValue) {
return "Value \"{$cellValue}\" is not strict equal to \"{$this->getOptionAsString()}\"";
return "Value \"<c>{$cellValue}</c>\" is not strict equal to \"<green>{$this->getOptionAsString()}<green>\"";
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/IsDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function validateRule(?string $cellValue): ?string
$domainPattern = '/^(?!-)[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,})$/';

if (\preg_match($domainPattern, (string)$cellValue) === 0) {
return "Value \"{$cellValue}\" is not a valid domain";
return "Value \"<c>{$cellValue}</c>\" is not a valid domain";
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/IsEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function validateRule(?string $cellValue): ?string
}

if (\filter_var($cellValue, \FILTER_VALIDATE_EMAIL) === false) {
return "Value \"{$cellValue}\" is not a valid email";
return "Value \"<c>{$cellValue}</c>\" is not a valid email";
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/IsFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function validateRule(?string $cellValue): ?string
}

if (\preg_match('/^-?\d+(\.\d+)?$/', (string)$cellValue) === 0) {
return "Value \"{$cellValue}\" is not a float number";
return "Value \"<c>{$cellValue}</c>\" is not a float number";
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/IsInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function validateRule(?string $cellValue): ?string
}

if (\preg_match('/^-?\d+$/', (string)$cellValue) === 0) {
return "Value \"{$cellValue}\" is not an integer";
return "Value \"<c>{$cellValue}</c>\" is not an integer";
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/IsIp.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function validateRule(?string $cellValue): ?string
}

if (\filter_var($cellValue, \FILTER_VALIDATE_IP) === false) {
return "Value \"{$cellValue}\" is not a valid IP";
return "Value \"<c>{$cellValue}</c>\" is not a valid IP";
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/IsLatitude.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function validateRule(?string $cellValue): ?string

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

return null;
Expand Down
3 changes: 2 additions & 1 deletion src/Validators/Rules/IsLongitude.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function validateRule(?string $cellValue): ?string

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

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/IsUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function validateRule(?string $cellValue): ?string
}

if (\filter_var($cellValue, \FILTER_VALIDATE_URL) === false) {
return "Value \"{$cellValue}\" is not a valid URL";
return "Value \"<c>{$cellValue}</c>\" is not a valid URL";
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/Max.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function validateRule(?string $cellValue): ?string
}

if ($this->getOptionAsFloat() < (float)$cellValue) {
return "Value \"{$cellValue}\" is greater than \"{$this->getOptionAsFloat()}\"";
return "Value \"<c>{$cellValue}</c>\" is greater than \"<green>{$this->getOptionAsFloat()}</green>\"";
}

return null;
Expand Down
4 changes: 2 additions & 2 deletions src/Validators/Rules/MaxDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function validateRule(?string $cellValue): ?string
$cellDate = new \DateTimeImmutable((string)$cellValue);

if ($cellDate->getTimestamp() > $minDate->getTimestamp()) {
return "Value \"{$cellValue}\" is more than the maximum " .
"date \"{$minDate->format(\DATE_RFC3339_EXTENDED)}\"";
return "Value \"<c>{$cellValue}</c>\" is more than the maximum " .
"date \"<green>{$minDate->format(\DATE_RFC3339_EXTENDED)}</green>\"";
}

return null;
Expand Down
3 changes: 2 additions & 1 deletion src/Validators/Rules/MaxLength.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function validateRule(?string $cellValue): ?string
$length = \mb_strlen((string)$cellValue);

if ($length > $minLength) {
return "Value \"{$cellValue}\" (legth: {$length}) is too long. Max length is {$minLength}";
return "Value \"<c>{$cellValue}</c>\" (length: {$length}) is too long. " .
"Max length is <green>{$minLength}</green>";
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/Min.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function validateRule(?string $cellValue): ?string
}

if ($this->getOptionAsFloat() > (float)$cellValue) {
return "Value \"{$cellValue}\" is less than \"{$this->getOptionAsFloat()}\"";
return "Value \"<c>{$cellValue}</c>\" is less than \"<green>{$this->getOptionAsFloat()}</green>\"";
}

return null;
Expand Down
4 changes: 2 additions & 2 deletions src/Validators/Rules/MinDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function validateRule(?string $cellValue): ?string
$cellDate = new \DateTimeImmutable((string)$cellValue);

if ($cellDate->getTimestamp() < $minDate->getTimestamp()) {
return "Value \"{$cellValue}\" is less than the minimum " .
"date \"{$minDate->format(\DATE_RFC3339_EXTENDED)}\"";
return "Value \"<c>{$cellValue}</c>\" is less than the minimum " .
"date \"<green>{$minDate->format(\DATE_RFC3339_EXTENDED)}</green>\"";
}

return null;
Expand Down
3 changes: 2 additions & 1 deletion src/Validators/Rules/MinLength.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function validateRule(?string $cellValue): ?string
$length = \mb_strlen((string)$cellValue);

if ($length < $minLength) {
return "Value \"{$cellValue}\" (legth: {$length}) is too short. Min length is {$minLength}";
return "Value \"<c>{$cellValue}</c>\" (length: {$length}) is too short. " .
"Min length is <green>{$minLength}</green>";
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/OnlyCapitalize.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function validateRule(?string $cellValue): ?string
}

if ($cellValue !== null && $cellValue !== \ucfirst($cellValue)) {
return "Value \"{$cellValue}\" should be in capitalize";
return "Value \"<c>{$cellValue}</c>\" should be in capitalize";
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/OnlyLowercase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function validateRule(?string $cellValue): ?string
}

if ($cellValue !== null && $cellValue !== \mb_strtolower($cellValue)) {
return "Value \"{$cellValue}\" should be in lowercase";
return "Value \"<c>{$cellValue}</c>\" should be in lowercase";
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/OnlyTrimed.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function validateRule(?string $cellValue): ?string
}

if (\trim((string)$cellValue) !== (string)$cellValue) {
return "Value \"{$cellValue}\" is not trimmed";
return "Value \"<c>{$cellValue}</c>\" is not trimmed";
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/OnlyUppercase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function validateRule(?string $cellValue): ?string
}

if ($cellValue !== null && \mb_strtoupper($cellValue, 'UTF-8') !== $cellValue) {
return "Value \"{$cellValue}\" is not uppercase";
return "Value \"<c>{$cellValue}</c>\" is not uppercase";
}

return null;
Expand Down
4 changes: 2 additions & 2 deletions src/Validators/Rules/Precision.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public function validateRule(?string $cellValue): ?string
$valuePrecision = self::getFloatPrecision($cellValue);

if ($this->getOptionAsInt() !== $valuePrecision) {
return "Value \"{$cellValue}\" has a precision of {$valuePrecision} " .
"but should have a precision of {$this->getOptionAsInt()}";
return "Value \"<c>{$cellValue}</c>\" has a precision of {$valuePrecision} " .
"but should have a precision of <green>{$this->getOptionAsInt()}</green>";
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function validateRule(?string $cellValue): ?string
}

if (\preg_match($regex, (string)$cellValue) === 0) {
return "Value \"{$cellValue}\" does not match the pattern \"{$regex}\"";
return "Value \"<c>{$cellValue}</c>\" does not match the pattern \"<green>{$regex}</green>\"";
}

return null;
Expand Down
4 changes: 2 additions & 2 deletions src/Validators/Rules/UsaMarketName.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public function validateRule(?string $cellValue): ?string
}

if (\preg_match('/^[A-Za-z0-9\s-]+, [A-Z]{2}$/u', (string)$cellValue) === 0) {
return "Invalid market name format for value \"{$cellValue}\". " .
'Market name must have format "New York, NY"';
return "Invalid market name format for value \"<c>{$cellValue}</c>\". " .
'Market name must have format "<green>New York, NY</green>"';
}

return null;
Expand Down
Loading

0 comments on commit 9cd1226

Please sign in to comment.