Skip to content

Commit

Permalink
Update SchemaValidator to cast example to string (#63)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SmetDenis authored Mar 19, 2024
1 parent 75d77c4 commit fc08765
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ---------------------------------------------------------------------+
Expand All @@ -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.
```
<!-- /output-table -->
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/SchemaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
43 changes: 26 additions & 17 deletions tests/Commands/ValidateCsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ---------------------------------------------------------------------+
Expand All @@ -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;

Expand All @@ -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 --------------------------------------------------+
Expand Down Expand Up @@ -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;

Expand All @@ -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".
Expand All @@ -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;

Expand All @@ -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.
Expand All @@ -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;

Expand Down Expand Up @@ -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.
Expand All @@ -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;

Expand All @@ -308,14 +313,18 @@ 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']
##teamcity[testStarted name='is_float at column 2:Float' locationHint='php_qn://./tests/schemas/demo_invalid.yml' flowId='42']
"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
Expand Down Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions tests/schemas/demo_invalid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ columns:
date_max: "2009-01-01"

- name: Favorite color
example: 123
rules:
not_empty: true
allow_values: [ red, green, Blue ]

0 comments on commit fc08765

Please sign in to comment.