Skip to content

Commit

Permalink
Remove inheritance functionality in src/SchemaDataPrep.php
Browse files Browse the repository at this point in the history
Removed the 'inherit' attribute from different functions in 'SchemaDataPrep.php'. Both tests and examples have been updated to reflect these changes. This increases code readability and reliability, while reducing complexity.
  • Loading branch information
SmetDenis committed Apr 5, 2024
1 parent 894090f commit d2e941e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 51 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
[![Static Badge](https://img.shields.io/badge/Rules-118-green?label=Cell%20rules&labelColor=blue&color=gray)](src/Rules/Cell)
[![Static Badge](https://img.shields.io/badge/Rules-206-green?label=Aggregate%20rules&labelColor=blue&color=gray)](src/Rules/Aggregate)
[![Static Badge](https://img.shields.io/badge/Rules-8-green?label=Extra%20checks&labelColor=blue&color=gray)](#extra-checks)
[![Static Badge](https://img.shields.io/badge/Rules-17/11/25-green?label=Plan%20to%20add&labelColor=gray&color=gray)](tests/schemas/todo.yml)
[![Static Badge](https://img.shields.io/badge/Rules-17/11/20-green?label=Plan%20to%20add&labelColor=gray&color=gray)](tests/schemas/todo.yml)
<!-- auto-update:/rules-counter -->

A console utility designed for validating CSV files against a strictly defined schema and validation rules outlined
Expand Down Expand Up @@ -1364,12 +1364,12 @@ It's random ideas and plans. No promises and deadlines. Feel free to [help me!](
* Flag to ignore file name pattern. It's useful when you have a lot of files, and you don't want to validate the file name.

* **Validation**
* Multi `filename_pattern`. Support list of regexs.
* Multi values in one cell.
* Custom cell rule as a callback. It's useful when you have a complex rule that can't be described in the schema file.
* Custom agregate rule as a callback. It's useful when you have a complex rule that can't be described in the schema file.
* Configurable keyword for null/empty values. By default, it's an empty string. But you will use `null`, `nil`, `none`, `empty`, etc. Overridable on the column level.
* Handle empty files and files with only a header row, or only with one line of data. One column wthout header is also possible.
* Inheritance of schemas, rules and columns. Define parent schema and override some rules in the child schemas. Make it DRY and easy to maintain.
* If option `--schema` is not specified, then validate only super base level things (like "is it a CSV file?").
* Complex rules (like "if field `A` is not empty, then field `B` should be not empty too").
* Extending with custom rules and custom report formats. Plugins?
Expand Down
51 changes: 19 additions & 32 deletions src/SchemaDataPrep.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ final class SchemaDataPrep
'inlcudes' => [],

'csv' => [
'inherit' => null,
'header' => true,
'delimiter' => ',',
'quote_char' => '\\',
Expand All @@ -46,7 +45,6 @@ final class SchemaDataPrep
],

'column' => [
'inherit' => '',
'name' => '',
'description' => '',
'example' => null,
Expand Down Expand Up @@ -78,7 +76,7 @@ public function buildData(): Data
'name' => $this->buildName(),
'description' => $this->buildDescription(),
'includes' => $this->buildIncludes(),
'filename_pattern' => $this->buildFilenamePattern(),
'filename_pattern' => $this->buildByKey('filename_pattern')[0],
'csv' => $this->buildByKey('csv'),
'structural_rules' => $this->buildByKey('structural_rules'),
'columns' => $this->buildColumns(),
Expand Down Expand Up @@ -146,17 +144,24 @@ private function getParentSchema(string $alias): Schema
throw new \InvalidArgumentException("Unknown included alias: \"{$alias}\"");
}

private function buildFilenamePattern(): string
private function buildIncludes(): array
{
$inherit = $this->data->findString('filename_pattern.inherit');

if ($inherit !== '') {
$inheritParts = self::parseAliasParts($inherit);
$parent = $this->getParentSchema($inheritParts['alias']);
return $parent->getData()->get('filename_pattern');
$result = [];
foreach ($this->aliases as $alias => $schema) {
$result[$alias] = $schema->getFilename();
}

return $this->data->getString('filename_pattern', self::DEFAULTS['filename_pattern']);
return $result;
}

private function buildName(): string
{
return $this->data->getString('name', self::DEFAULTS['name']);
}

private function buildDescription(): string
{
return $this->data->getString('description', self::DEFAULTS['description']);
}

private function buildByKey(string $key = 'structural_rules'): array
Expand Down Expand Up @@ -190,7 +195,9 @@ private function buildColumns(): array
$parent = $this->getParentSchema($inheritParts['alias']);
$parentColumn = $parent->getColumn($inheritParts['column']);
if ($parentColumn === null) {
throw new \InvalidArgumentException("Unknown column: \"{$inheritParts['column']}\"");
throw new \InvalidArgumentException(
"Unknown column: \"{$inheritParts['column']}\" by alias: \"{$inheritParts['alias']}\"",
);
}

$parentConfig = $parentColumn->getData()->getArrayCopy();
Expand All @@ -208,26 +215,6 @@ private function buildColumns(): array
return $columns;
}

private function buildIncludes(): array
{
$result = [];
foreach ($this->aliases as $alias => $schema) {
$result[$alias] = $schema->getFilename();
}

return $result;
}

private function buildName(): string
{
return $this->data->getString('name', self::DEFAULTS['name']);
}

private function buildDescription(): string
{
return $this->data->getString('description', self::DEFAULTS['description']);
}

private function buildRules(array $rules, string $typeOfRules): array
{
$inherit = $rules['inherit'] ?? '';
Expand Down
7 changes: 1 addition & 6 deletions tests/ReadmeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,10 @@ public function testBadgeOfRules(): void
'csv.auto_detect',
'csv.end_of_line',
'csv.null_values',
'filename_pattern - multiple',
'column.faker',
'column.null_values',
'column.multiple + column.multiple_separator',
'inherit.',
'inherit.csv',
'inherit.structural_rules',
'inherit.rules',
'inherit.aggregate_rules',
'inherit.complex_rules',
]) + \count($todoYml->findArray('structural_rules'))
+ \count($todoYml->findArray('complex_rules')),
]);
Expand Down
13 changes: 2 additions & 11 deletions tests/schemas/todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,9 @@

# File contains just ideas. It's invalid!

# Include another schemas
includes: # Alias is always required
- /path/schema_1.yml as alias_1 # Full path to another schema.
- ./path/schema_2.yml as alias_2 # Relative path based on the current schema path.
- ../path/schema_3.yml as alias_3 # Relative path based on the current schema path. Go up one level.


csv: # How to parse file before validation
inherit: alias_1 # Inherited from another schema. Options above will overwrite inherited options.
auto_detect: false # If true, then the cintrol chars will be detected automatically.
end_of_line: LF # End of line character. LF => \n, CRLF => \r\n, CR => \r
auto_detect: false # If true, then the control chars will be detected automatically.
empty_values: # List of values that will be treated as empty
- "" # By default, only empty string is treated as empty (string length = 0).
- null
Expand All @@ -41,8 +33,7 @@ structural_rules:


columns:
- inherit: alias_1\Column Name
empty_values: [''] # Override csv.empty_values. List of values that will be treated as empty.
- empty_values: [''] # Override csv.empty_values. List of values that will be treated as empty.

# Multi prop
multiple: true
Expand Down

0 comments on commit d2e941e

Please sign in to comment.