Skip to content

Commit

Permalink
Implement apply-all option for global schema application without gl…
Browse files Browse the repository at this point in the history
…ob patterns (#150)

Renamed the `apply-global` command option to `apply-all` in CSV
validation, and adjusted all related usage within the documentation,
test, and action files. This change aims to improve clarity and
simplicity of command usage.

The 'apply-all' option now automates the application of a single schema
as global rules if no glob pattern is used. This update refines the
command's functionality and improves user experience. Changes have been
reflected in the documentation, test, and action files.
  • Loading branch information
SmetDenis authored Apr 7, 2024
1 parent aa45904 commit 1d231f6
Show file tree
Hide file tree
Showing 10 changed files with 354 additions and 122 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,14 @@ You can find launch examples in the [workflow demo](https://github.com/JBZoo/Csv
# Required: true
report: 'table'

# Apply all schemas (also without `filename_pattern`) to all CSV files found.
# Default value: 'no'
# Apply all schemas (also without `filename_pattern`) to all CSV files found as global rules.
# Available options:
# auto: If no glob pattern (*) is used for --schema, the schema is applied to all found CSV files.
# yes: Apply all schemas to all CSV files, Schemas without `filename_pattern` are applied as a global rule.
# no: Apply only schemas with not empty `filename_pattern` and match the CSV files.
# Default value: 'auto'
# Required: true
apply-all: 'no'
apply-all: 'auto'

# Quick mode. It will not validate all rows. It will stop after the first error.
# Default value: 'no'
Expand Down Expand Up @@ -1446,7 +1450,13 @@ Options:
-S, --skip-schema[=SKIP-SCHEMA] Skips schema validation for quicker checks when the schema's correctness is certain.
Use any non-empty value or "yes" to activate
[default: "no"]
-a, --apply-all[=APPLY-ALL] Apply global schemas (also without `filename_pattern`) to all CSV files found. [default: "no"]
-a, --apply-all[=APPLY-ALL] Apply all schemas (also without `filename_pattern`) to all CSV files found as global rules.
Available options:
- auto: If no glob pattern (*) is used for --schema, the schema is applied to all found CSV files.
- yes|y|1: Apply all schemas to all CSV files, Schemas without `filename_pattern` are applied as a global rule.
- no|n|0: Apply only schemas with not empty `filename_pattern` and match the CSV files.
Note. If specify the option `--apply-all` without value, it will be treated as "yes".
[default: "auto"]
-r, --report=REPORT Determines the report's output format.
Available options: text, table, github, gitlab, teamcity, junit
[default: "table"]
Expand Down Expand Up @@ -1557,7 +1567,7 @@ discovered errors. This format is ideal for quick reviews and sharing with team


CSV Blueprint: vX.Y.Z
Found Schemas : 1
Found Schemas : 1 (Apply All)
Found CSV files : 1
Pairs by pattern: 1

Expand Down
9 changes: 7 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ inputs:
default: table
required: true
apply-all:
description: 'Apply all schemas (also without `filename_pattern`) to all CSV files found.'
default: no
description: |
Apply all schemas (also without `filename_pattern`) to all CSV files found as global rules.
Available options:
auto: If no glob pattern (*) is used for --schema, the schema is applied to all found CSV files.
yes: Apply all schemas to all CSV files, Schemas without `filename_pattern` are applied as a global rule.
no: Apply only schemas with not empty `filename_pattern` and match the CSV files.
default: auto
required: true
quick:
description: 'Quick mode. It will not validate all rows. It will stop after the first error.'
Expand Down
63 changes: 47 additions & 16 deletions src/Commands/ValidateCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,19 @@ protected function configure(): void
'apply-all',
'a',
InputOption::VALUE_OPTIONAL,
'Apply global schemas (also without `filename_pattern`) to all CSV files found.',
'no',
\implode("\n", [
"Apply all schemas (also without `filename_pattern`) to all CSV files found as global rules.\n",
'Available options:',
' - <info>auto</info>: If no glob pattern (*) is used for --schema, the schema is applied to all ' .
'found CSV files.',
' - <info>yes|y|1</info>: Apply all schemas to all CSV files, Schemas without `filename_pattern` ' .
'are applied as a global rule.',
' - <info>no|n|0</info>: Apply only schemas with not empty `filename_pattern` and ' .
'match the CSV files.',
'Note. If specify the option `--apply-all` without value, it will be treated as "yes".',
'',
]),
'auto',
);

parent::configure();
Expand All @@ -101,7 +112,7 @@ protected function executeAction(): int

$csvFilenames = $this->findFiles('csv', false);
$schemaFilenames = $this->findFiles('schema', false);
$matchedFiles = Utils::matchSchemaAndCsvFiles($csvFilenames, $schemaFilenames, $this->isApplyGlobal());
$matchedFiles = Utils::matchSchemaAndCsvFiles($csvFilenames, $schemaFilenames, $this->isApplyAll());

$this->printHeaderInfo($csvFilenames, $schemaFilenames, $matchedFiles);

Expand All @@ -119,18 +130,6 @@ protected function executeAction(): int
);
}

protected function isApplyGlobal(): bool
{
$value = $this->getOptString('apply-all');
return $value === '' || bool($value);
}

private function isCheckingSchema(): bool
{
$value = $this->getOptString('skip-schema');
return !($value === '' || bool($value));
}

/**
* @param SplFileInfo[] $schemaFilenames
*/
Expand Down Expand Up @@ -318,15 +317,47 @@ private function printSummary(
return $exitCode;
}

private function isApplyAll(): bool
{
$applyAll = \strtolower(\trim($this->getOptString('apply-all')));
if (\in_array($applyAll, ['', 'yes', 'y', '1'], true)) {
return true;
}

if (\in_array($applyAll, ['no', 'n', '0'], true)) {
return false;
}

if ($applyAll === 'auto') {
$schemaPatterns = $this->getOptArray('schema');
foreach ($schemaPatterns as $schema) {
if (\str_contains($schema, '*')) { // glob pattern found.
return false;
}
}

return \count($schemaPatterns) === 1; // Only one schema file found without glob pattern.
}

throw new Exception('Invalid value for --apply-all option: ' . $applyAll);
}

private function isCheckingSchema(): bool
{
$value = $this->getOptString('skip-schema');
return !($value === '' || bool($value));
}

/**
* @param SplFileInfo[] $csvFilenames
* @param SplFileInfo[] $schemaFilenames
*/
private function printHeaderInfo(array $csvFilenames, array $schemaFilenames, array $matchedFiles): void
{
$validationFlag = $this->isCheckingSchema() ? '' : ' (<c>Validation skipped</c>)';
$applyAllFlag = $this->isApplyAll() ? ' (<c>Apply All</c>)' : '';
$this->out([
'Found Schemas : ' . \count($schemaFilenames) . $validationFlag,
'Found Schemas : ' . \count($schemaFilenames) . $validationFlag . $applyAllFlag,
'Found CSV files : ' . \count($csvFilenames),
'Pairs by pattern: ' . $matchedFiles['count_pairs'],
]);
Expand Down
Loading

0 comments on commit 1d231f6

Please sign in to comment.