From 1dca8e384207a903e21596934704b56c8a65891c Mon Sep 17 00:00:00 2001 From: Denis Smet Date: Tue, 19 Mar 2024 23:31:40 +0400 Subject: [PATCH] Update SchemaValidator to cast example to string This commit modifies the SchemaValidator code, ensuring that values used for examples are cast as strings before validation. This change addresses potential issues with non-string values, improving the robustness and accuracy of the schema validation process. Updates have also been made to the associated tests and documentation. --- README.md | 13 ++++----- src/Validators/SchemaValidator.php | 2 +- tests/Commands/ValidateCsvTest.php | 43 ++++++++++++++++++------------ tests/schemas/demo_invalid.yml | 1 + 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index c778776c..c945fc07 100644 --- a/README.md +++ b/README.md @@ -517,11 +517,12 @@ Schema: ./tests/schemas/demo_invalid.yml Found CSV files: 1 Schema is invalid: ./tests/schemas/demo_invalid.yml -+-------+-----------+----- demo_invalid.yml --------------------------+ -| Line | id:Column | Rule | Message | -+-------+-----------+----------+--------------------------------------+ -| undef | 2:Float | is_float | Value "Qwerty" is not a float number | -+-------+-----------+----- demo_invalid.yml --------------------------+ ++-------+------------------+--------------+----- 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"] | ++-------+------------------+--------------+----- demo_invalid.yml -----------------------------------------------+ (1/1) Invalid file: ./tests/fixtures/demo.csv +------+------------------+------------------+----------------------- demo.csv ---------------------------------------------------------------------+ @@ -543,7 +544,7 @@ Schema is invalid: ./tests/schemas/demo_invalid.yml Found 9 issues in CSV file. -Found 1 issues in schema. +Found 2 issues in schema. ``` diff --git a/src/Validators/SchemaValidator.php b/src/Validators/SchemaValidator.php index 58a567c0..4a4e372e 100644 --- a/src/Validators/SchemaValidator.php +++ b/src/Validators/SchemaValidator.php @@ -107,7 +107,7 @@ private static function validateColumnExample(array $actualColumn, int $columnKe ]; if (isset($actualColumn['example']) && !\in_array($actualColumn['example'], $exclude, true)) { - return (new Column($columnKey, $actualColumn))->validateCell($actualColumn['example']); + return (new Column($columnKey, $actualColumn))->validateCell((string)$actualColumn['example']); } return null; diff --git a/tests/Commands/ValidateCsvTest.php b/tests/Commands/ValidateCsvTest.php index ea41d7bb..c38898dd 100644 --- a/tests/Commands/ValidateCsvTest.php +++ b/tests/Commands/ValidateCsvTest.php @@ -63,11 +63,12 @@ public function testValidateOneFileNegativeTable(): void Found CSV files: 1 Schema is invalid: ./tests/schemas/demo_invalid.yml - +-------+-----------+----- demo_invalid.yml --------------------------+ - | Line | id:Column | Rule | Message | - +-------+-----------+----------+--------------------------------------+ - | undef | 2:Float | is_float | Value "Qwerty" is not a float number | - +-------+-----------+----- demo_invalid.yml --------------------------+ + +-------+------------------+--------------+----- 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"] | + +-------+------------------+--------------+----- demo_invalid.yml -----------------------------------------------+ (1/1) Invalid file: ./tests/fixtures/demo.csv +------+------------------+------------------+----------------------- demo.csv ---------------------------------------------------------------------+ @@ -89,7 +90,7 @@ public function testValidateOneFileNegativeTable(): void Found 9 issues in CSV file. - Found 1 issues in schema. + Found 2 issues in schema. TXT; @@ -111,11 +112,12 @@ public function testValidateManyFileNegativeTable(): void Found CSV files: 3 Schema is invalid: ./tests/schemas/demo_invalid.yml - +-------+-----------+----- demo_invalid.yml --------------------------+ - | Line | id:Column | Rule | Message | - +-------+-----------+----------+--------------------------------------+ - | undef | 2:Float | is_float | Value "Qwerty" is not a float number | - +-------+-----------+----- demo_invalid.yml --------------------------+ + +-------+------------------+--------------+----- 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"] | + +-------+------------------+--------------+----- demo_invalid.yml -----------------------------------------------+ (1/3) Invalid file: ./tests/fixtures/batch/demo-1.csv +------+------------------+--------------+--------- demo-1.csv --------------------------------------------------+ @@ -148,7 +150,7 @@ public function testValidateManyFileNegativeTable(): void Found 8 issues in 3 out of 3 CSV files. - Found 1 issues in schema. + Found 2 issues in schema. TXT; @@ -170,6 +172,7 @@ public function testValidateOneFileNegativeText(): void Schema is invalid: ./tests/schemas/demo_invalid.yml "is_float", column "2:Float". Value "Qwerty" is not a float number. + "allow_values", column "4:Favorite color". Value "123" is not allowed. Allowed values: ["red", "green", "Blue"]. (1/1) Invalid file: ./tests/fixtures/demo.csv "filename_pattern" at line 1, column "". Filename "./tests/fixtures/demo.csv" does not match pattern: "/demo-[12].csv$/i". @@ -184,7 +187,7 @@ public function testValidateOneFileNegativeText(): void Found 9 issues in CSV file. - Found 1 issues in schema. + Found 2 issues in schema. TXT; @@ -200,6 +203,7 @@ public function testValidateManyFilesNegativeTextQuick(): void Schema is invalid: ./tests/schemas/demo_invalid.yml "is_float", column "2:Float". Value "Qwerty" is not a float number. + "allow_values", column "4:Favorite color". Value "123" is not allowed. Allowed values: ["red", "green", "Blue"]. (1/3) Invalid file: ./tests/fixtures/batch/demo-1.csv "ag:is_unique" at line 1, column "1:City". Column has non-unique values. Unique: 1, total: 2. @@ -208,7 +212,7 @@ public function testValidateManyFilesNegativeTextQuick(): void (3/3) Skipped: ./tests/fixtures/batch/sub/demo-3.csv Found 1 issues in 1 out of 3 CSV files. - Found 1 issues in schema. + Found 2 issues in schema. TXT; @@ -267,6 +271,7 @@ public function testValidateManyFilesNegativeTextQuick(): void Schema is invalid: ./tests/schemas/demo_invalid.yml "is_float", column "2:Float". Value "Qwerty" is not a float number. + "allow_values", column "4:Favorite color". Value "123" is not allowed. Allowed values: ["red", "green", "Blue"]. (1/3) Invalid file: ./tests/fixtures/batch/demo-1.csv "ag:is_unique" at line 1, column "1:City". Column has non-unique values. Unique: 1, total: 2. @@ -284,7 +289,7 @@ public function testValidateManyFilesNegativeTextQuick(): void Found 8 issues in 3 out of 3 CSV files. - Found 1 issues in schema. + Found 2 issues in schema. TXT; @@ -308,7 +313,7 @@ public function testCreateValidateNegativeTeamcity(): void Schema is invalid: ./tests/schemas/demo_invalid.yml - ##teamcity[testCount count='1' flowId='42'] + ##teamcity[testCount count='2' flowId='42'] ##teamcity[testSuiteStarted name='demo_invalid.yml' flowId='42'] @@ -316,6 +321,10 @@ public function testCreateValidateNegativeTeamcity(): void "is_float", column "2:Float". Value "Qwerty" is not a float number. ##teamcity[testFinished name='is_float at column 2:Float' flowId='42'] + ##teamcity[testStarted name='allow_values at column 4:Favorite color' locationHint='php_qn://./tests/schemas/demo_invalid.yml' flowId='42'] + "allow_values", column "4:Favorite color". Value "123" is not allowed. Allowed values: ["red", "green", "Blue"]. + ##teamcity[testFinished name='allow_values at column 4:Favorite color' flowId='42'] + ##teamcity[testSuiteFinished name='demo_invalid.yml' flowId='42'] (1/3) Invalid file: ./tests/fixtures/batch/demo-1.csv @@ -376,7 +385,7 @@ public function testCreateValidateNegativeTeamcity(): void Found 8 issues in 3 out of 3 CSV files. - Found 1 issues in schema. + Found 2 issues in schema. TXT; diff --git a/tests/schemas/demo_invalid.yml b/tests/schemas/demo_invalid.yml index 8bfba212..0dff325a 100644 --- a/tests/schemas/demo_invalid.yml +++ b/tests/schemas/demo_invalid.yml @@ -44,6 +44,7 @@ columns: date_max: "2009-01-01" - name: Favorite color + example: 123 rules: not_empty: true allow_values: [ red, green, Blue ]