Skip to content

Commit

Permalink
Improve output formatting (#131)
Browse files Browse the repository at this point in the history
It enhances console output by adjusting indentation and string padding,
making the results easier to read and understand.
  • Loading branch information
SmetDenis committed Apr 2, 2024
1 parent 2d3dde4 commit 9c50b12
Show file tree
Hide file tree
Showing 13 changed files with 428 additions and 457 deletions.
70 changes: 34 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -878,10 +878,11 @@ Options:
You can specify path in which CSV files will be searched (max depth is 10).
Feel free to use glob pattrens. Usage examples:
/full/path/file.csv, p/file.csv, p/*.csv, p/**/*.csv, p/**/name-*.csv, **/*.csv, etc. (multiple values allowed)
-s, --schema=SCHEMA Path(s) to schema file(s).
It can be a YAML, JSON or PHP. See examples on GitHub.Also, you can specify path in which schema files will be searched (max depth is 10).
-s, --schema=SCHEMA Path(s) to schema file(s). It can be a YAML, JSON or PHP. See examples on GitHub.
Also, you can specify path in which schema files will be searched (max depth is 10).
Feel free to use glob pattrens. Usage examples:
/full/path/file.yml, p/file.yml, p/*.yml, p/**/*.yml, p/**/name-*.json, **/*.php, etc. (multiple values allowed)
/full/path/file.yml, p/file.yml, p/*.yml, p/**/*.yml, p/**/name-*.json, **/*.php, etc.
(multiple values allowed)
-S, --skip-schema[=SKIP-SCHEMA] Skip schema validation.
If you are sure that the schema is correct, you can skip this check.
Empty value or "yes" will be treated as "true". [default: "no"]
Expand Down Expand Up @@ -932,10 +933,11 @@ Usage:
validate:schema [options]
Options:
-s, --schema=SCHEMA Path(s) to schema file(s).
It can be a YAML, JSON or PHP. See examples on GitHub.Also, you can specify path in which schema files will be searched (max depth is 10).
-s, --schema=SCHEMA Path(s) to schema file(s). It can be a YAML, JSON or PHP. See examples on GitHub.
Also, you can specify path in which schema files will be searched (max depth is 10).
Feel free to use glob pattrens. Usage examples:
/full/path/file.yml, p/file.yml, p/*.yml, p/**/*.yml, p/**/name-*.json, **/*.php, etc. (multiple values allowed)
/full/path/file.yml, p/file.yml, p/*.yml, p/**/*.yml, p/**/name-*.json, **/*.php, etc.
(multiple values allowed)
-r, --report=REPORT Report output format. Available options:
["text", "table", "github", "gitlab", "teamcity", "junit"] [default: "table"]
-Q, --quick[=QUICK] Immediately terminate the check at the first error found.
Expand Down Expand Up @@ -985,38 +987,34 @@ Found CSV files : 1
Pairs by pattern: 1
Check schema syntax: 1
(1/1) Schema: ./tests/schemas/demo_invalid.yml
(1/1) Issues: 2
+-------+------------------+------------- tests/schemas/demo_invalid.yml ----------------------------------------+
| Line | id:Column | Rule | Message |
+-------+------------------+--------------+----------------------------------------------------------------------+
| undef | 2:Float | is_float | Value "Qwerty" is not a float number |
| undef | 4:Favorite color | allow_values | Value "123" is not allowed. Allowed values: ["red", "green", "Blue"] |
+-------+------------------+------------- tests/schemas/demo_invalid.yml ----------------------------------------+
2 issues in ./tests/schemas/demo_invalid.yml
+-------+------------------+--------------+----------------------------------------------------------------------+
| Line | id:Column | Rule | Message |
+-------+------------------+--------------+----------------------------------------------------------------------+
| undef | 2:Float | is_float | Value "Qwerty" is not a float number |
| undef | 4:Favorite color | allow_values | Value "123" is not allowed. Allowed values: ["red", "green", "Blue"] |
+-------+------------------+--------------+----------------------------------------------------------------------+
CSV file validation: 1
(1/1) Schema: ./tests/schemas/demo_invalid.yml
(1/1) CSV : ./tests/fixtures/demo.csv; Size: 123.34 MB
(1/1) Issues: 10
+------+------------------+---------------------+-------------- tests/fixtures/demo.csv ---------------------------------------------------------------+
| Line | id:Column | Rule | Message |
+------+------------------+---------------------+------------------------------------------------------------------------------------------------------+
| 1 | | allow_extra_columns | Column(s) not found in CSV: "wrong_column_name" |
| 6 | 0:Name | length_min | The length of the value "Carl" is 4, which is less than the expected "5" |
| 11 | 0:Name | length_min | The length of the value "Lois" is 4, which is less than the expected "5" |
| 1 | 1:City | ag:is_unique | Column has non-unique values. Unique: 9, total: 10 |
| 2 | 2:Float | num_max | The value "4825.185" is greater than the expected "4825.184" |
| 1 | 2:Float | ag:nth_num | The N-th value in the column is "74", which is not equal than the expected "0.001" |
| 6 | 3:Birthday | date_min | The date of the value "1955-05-14" is parsed as "1955-05-14 00:00:00 +00:00", which is less than the |
| | | | expected "1955-05-15 00:00:00 +00:00 (1955-05-15)" |
| 8 | 3:Birthday | date_min | The date of the value "1955-05-14" is parsed as "1955-05-14 00:00:00 +00:00", which is less than the |
| | | | expected "1955-05-15 00:00:00 +00:00 (1955-05-15)" |
| 9 | 3:Birthday | date_max | The date of the value "2010-07-20" is parsed as "2010-07-20 00:00:00 +00:00", which is greater than |
| | | | the expected "2009-01-01 00:00:00 +00:00 (2009-01-01)" |
| 5 | 4:Favorite color | allow_values | Value "blue" is not allowed. Allowed values: ["red", "green", "Blue"] |
+------+------------------+---------------------+-------------- tests/fixtures/demo.csv ---------------------------------------------------------------+
Schema: ./tests/schemas/demo_invalid.yml
10 issues in ./tests/fixtures/demo.csv; Size: 123.34 MB
+------+------------------+---------------------+------------------------------------------------------------------------------------------------------+
| Line | id:Column | Rule | Message |
+------+------------------+---------------------+------------------------------------------------------------------------------------------------------+
| 1 | | allow_extra_columns | Column(s) not found in CSV: "wrong_column_name" |
| 6 | 0:Name | length_min | The length of the value "Carl" is 4, which is less than the expected "5" |
| 11 | 0:Name | length_min | The length of the value "Lois" is 4, which is less than the expected "5" |
| 1 | 1:City | ag:is_unique | Column has non-unique values. Unique: 9, total: 10 |
| 2 | 2:Float | num_max | The value "4825.185" is greater than the expected "4825.184" |
| 1 | 2:Float | ag:nth_num | The N-th value in the column is "74", which is not equal than the expected "0.001" |
| 6 | 3:Birthday | date_min | The date of the value "1955-05-14" is parsed as "1955-05-14 00:00:00 +00:00", which is less than the |
| | | | expected "1955-05-15 00:00:00 +00:00 (1955-05-15)" |
| 8 | 3:Birthday | date_min | The date of the value "1955-05-14" is parsed as "1955-05-14 00:00:00 +00:00", which is less than the |
| | | | expected "1955-05-15 00:00:00 +00:00 (1955-05-15)" |
| 9 | 3:Birthday | date_max | The date of the value "2010-07-20" is parsed as "2010-07-20 00:00:00 +00:00", which is greater than |
| | | | the expected "2009-01-01 00:00:00 +00:00 (2009-01-01)" |
| 5 | 4:Favorite color | allow_values | Value "blue" is not allowed. Allowed values: ["red", "green", "Blue"] |
+------+------------------+---------------------+------------------------------------------------------------------------------------------------------+
Summary:
1 pairs (schema to csv) were found based on `filename_pattern`.
Expand Down
55 changes: 48 additions & 7 deletions src/Commands/AbstractValidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,6 @@ protected function isQuickMode(): bool
return $value === '' || bool($value);
}

protected function out(null|array|string $messge): void
{
if ($this->isHumanReadableMode()) {
$this->_($messge);
}
}

/**
* @return SplFileInfo[]
*/
Expand All @@ -111,4 +104,52 @@ protected function findFiles(string $option, bool $throwException = true): array

return $filenames;
}

protected function out(null|array|string $messge, int $indent = 0): void
{
if ($messge === null) {
return;
}

if ($this->isHumanReadableMode()) {
$indent = \str_repeat(' ', $indent);
$messges = \is_string($messge) ? \explode("\n", $messge) : $messge;

foreach ($messges as $line) {
$this->_($indent . $line);
}
}
}

protected function outReport(ErrorSuite $errorSuite, int $indet = 2): void
{
if ($this->getReportType() === ErrorSuite::REPORT_GITHUB) {
$indet = 0;
}

if ($this->isHumanReadableMode()) {
$this->out($errorSuite->render($this->getReportType()), $indet);
} else {
$this->_($errorSuite->render($this->getReportType()));
}
}

protected function renderIssues(string $prefix, int $number, string $filepath, int $indent = 0): void
{
$issues = $number === 1 ? 'issue' : 'issues';
$this->out("{$prefix}<yellow>{$number} {$issues}</yellow> in {$filepath}", $indent);
}

protected static function renderPrefix(int $index, int $totalFiles): string
{
if ($totalFiles <= 1) {
return '';
}

return \sprintf(
'(%s/%s) ',
\str_pad((string)$index, \strlen((string)$totalFiles), ' ', \STR_PAD_LEFT),
(string)$totalFiles,
);
}
}

0 comments on commit 9c50b12

Please sign in to comment.