Skip to content

Commit

Permalink
Improve error handling in CsvFile
Browse files Browse the repository at this point in the history
This update introduces the use of the Utils::cutPath function in the error reporting for filename pattern mismatch in CsvFile.php. The error message, now, only displays the relative file path instead of the absolute one. The changes help decrease verbosity and improve clarity in error messages raised during CSV file validation.
  • Loading branch information
Denis Smet committed Mar 13, 2024
1 parent bcd9d11 commit 00bcb66
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/Csv/CsvFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace JBZoo\CsvBlueprint\Csv;

use JBZoo\CsvBlueprint\Schema;
use JBZoo\CsvBlueprint\Utils;
use JBZoo\CsvBlueprint\Validators\Error;
use JBZoo\CsvBlueprint\Validators\ErrorSuite;
use League\Csv\Reader as LeagueReader;
Expand Down Expand Up @@ -170,7 +171,8 @@ private function validateFile(bool $quickStop = false): ErrorSuite
) {
$error = new Error(
'filename_pattern',
"Filename \"<c>{$this->csvFilename}</c>\" does not match pattern: \"<c>{$filenamePattern}</c>\"",
'Filename "<c>' . Utils::cutPath($this->csvFilename) .
"</c>\" does not match pattern: \"<c>{$filenamePattern}</c>\"",
'',
0,
);
Expand Down
5 changes: 2 additions & 3 deletions tests/Blueprint/ValidateCsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ public function testValidateOneFileNegativeTable(): void
+------+------------------+------------------+--- demo.csv -------------------------------------------------+
| Line | id:Column | Rule | Message |
+------+------------------+------------------+--------------------------------------------------------------+
| 0 | | filename_pattern | Filename |
| | | | "/Users/smetdenis/Work/projects/jbzoo-csv-validator/tests/fi |
| | | | xtures/demo.csv" does not match pattern: "/demo-[12].csv$/i" |
| 0 | | filename_pattern | Filename "./tests/fixtures/demo.csv" does not match pattern: |
| | | | "/demo-[12].csv$/i" |
| 5 | 2:Float | max | Value "74605.944" is greater than "74605" |
| 5 | 4:Favorite color | allow_values | Value "blue" is not allowed. Allowed values: ["red", |
| | | | "green", "Blue"] |
Expand Down

0 comments on commit 00bcb66

Please sign in to comment.