From d2e941e96db77bc37d5181251cdfda3c647d4669 Mon Sep 17 00:00:00 2001 From: SmetDenis Date: Sat, 6 Apr 2024 03:29:55 +0400 Subject: [PATCH] Remove inheritance functionality in src/SchemaDataPrep.php 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. --- README.md | 4 ++-- src/SchemaDataPrep.php | 51 ++++++++++++++++-------------------------- tests/ReadmeTest.php | 7 +----- tests/schemas/todo.yml | 13 ++--------- 4 files changed, 24 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index e1051432..0cdc1540 100644 --- a/README.md +++ b/README.md @@ -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) A console utility designed for validating CSV files against a strictly defined schema and validation rules outlined @@ -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? diff --git a/src/SchemaDataPrep.php b/src/SchemaDataPrep.php index 357712da..698adaae 100644 --- a/src/SchemaDataPrep.php +++ b/src/SchemaDataPrep.php @@ -31,7 +31,6 @@ final class SchemaDataPrep 'inlcudes' => [], 'csv' => [ - 'inherit' => null, 'header' => true, 'delimiter' => ',', 'quote_char' => '\\', @@ -46,7 +45,6 @@ final class SchemaDataPrep ], 'column' => [ - 'inherit' => '', 'name' => '', 'description' => '', 'example' => null, @@ -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(), @@ -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 @@ -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(); @@ -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'] ?? ''; diff --git a/tests/ReadmeTest.php b/tests/ReadmeTest.php index a1080e44..a1b9505e 100644 --- a/tests/ReadmeTest.php +++ b/tests/ReadmeTest.php @@ -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')), ]); diff --git a/tests/schemas/todo.yml b/tests/schemas/todo.yml index 83312538..1ac62c24 100644 --- a/tests/schemas/todo.yml +++ b/tests/schemas/todo.yml @@ -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 @@ -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