Skip to content

Commit

Permalink
Refactor schema validation and dump, update README
Browse files Browse the repository at this point in the history
Adjusted printDumpOfSchema to handle null schema and improve display of dumped schema in XML format. Moved calling of printDumpOfSchema method after possible exceptions in ValidateCsv. Also, clarified instructions for troubleshooting faulty presets in the README documentation.
  • Loading branch information
SmetDenis committed Apr 6, 2024
1 parent cfd0b50 commit 0460bb7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -962,8 +962,9 @@ In fact, this can be considered as partial inheritance.
Otherwise, it might break the syntax.

**If something went wrong**
If you're having trouble working with presets and don't understand how the CSV Blueprint under the hood understands
it, just add `--dump-schema` to see it. Also, there is a separate CLI command for validating schema:

If you're having trouble working with presets and don't understand how the CSV Blueprint under the hood understands it,
just add `--dump-schema` to see it. Also, there is a separate CLI command for validating schema:

```shell
./csv-blueprint validate:schema --dump-schema --schema=./your/schema.yml
Expand Down
15 changes: 11 additions & 4 deletions src/Commands/AbstractValidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,19 @@ protected function renderIssues(string $prefix, int $number, string $filepath, i
$this->out("{$prefix}<yellow>{$number} {$issues}</yellow> in {$filepath}", $indent);
}

protected function printDumpOfSchema(Schema $schema): void
protected function printDumpOfSchema(?Schema $schema): void
{
if ($schema === null) {
return;
}
$dump = $schema->dumpAsYamlString();
$dump = \preg_replace('/^([ \t]*)([^:\n]+:)/m', '$1<c>$2</c>', $dump);

if ($this->getOptBool('dump-schema')) {
$this->_('```yaml');
$this->_($schema->dumpAsYamlString());
$this->_('```');
$this->_('<blue>```yaml</blue>');
$this->_("# File: <blue>{$schema->getFilename()}</blue>");
$this->_($dump);
$this->_('<blue>```</blue>');
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/Commands/ValidateCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ private function validateSchemas(array $schemaFilenames): int
continue;
}

$schema = null;

try {
$schema = new Schema($schemaFilename->getPathname());
$this->printDumpOfSchema($schema);

$schemaErrors = $schema->validate($quickCheck);
if ($schemaErrors->count() > 0) {
$this->renderIssues($prefix, $schemaErrors->count(), $schemaPath, 2);
Expand All @@ -161,6 +161,7 @@ private function validateSchemas(array $schemaFilenames): int
"{$prefix}Exception: <yellow>{$e->getMessage()}</yellow>",
], 2);
}
$this->printDumpOfSchema($schema);
}

$this->out('');
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/ValidateSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected function executeAction(): int
try {
$schema = new Schema($filename);
$schemaErrors = $schema->validate($this->isQuickMode());
$this->printDumpOfSchema($schema);
$this->printDumpOfSchema(new Schema($filename));
} catch (ParseException $e) {
$schemaErrors->addError(new Error('schema.syntax', $e->getMessage(), '', $e->getParsedLine()));
} catch (\Throwable $e) {
Expand Down

0 comments on commit 0460bb7

Please sign in to comment.