-
-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhancement: Apply lax validation to composer.json #416
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"$ref": "schema.json", | ||
"additionalProperties": true, | ||
"required": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
|
||
use Composer\Command; | ||
use Composer\Factory; | ||
use Composer\IO; | ||
use Ergebnis\Composer\Normalize\Exception; | ||
use Ergebnis\Json\Normalizer; | ||
use Localheinz\Diff; | ||
|
@@ -121,19 +122,10 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O | |
$composerFile = Factory::getComposerFile(); | ||
} | ||
|
||
try { | ||
$composer = $this->factory->createComposer( | ||
$io, | ||
$composerFile | ||
); | ||
} catch (\Exception $exception) { | ||
$io->writeError(\sprintf( | ||
'<error>%s</error>', | ||
$exception->getMessage() | ||
)); | ||
|
||
return 1; | ||
} | ||
$composer = $this->factory->createComposer( | ||
$io, | ||
$composerFile | ||
); | ||
|
||
if (false === $input->getOption('dry-run') && !\is_writable($composerFile)) { | ||
$io->writeError(\sprintf( | ||
|
@@ -159,19 +151,22 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O | |
|
||
try { | ||
$normalized = $this->normalizer->normalize($json); | ||
} catch (Normalizer\Exception\OriginalInvalidAccordingToSchemaException | Normalizer\Exception\NormalizedInvalidAccordingToSchemaException $exception) { | ||
$io->writeError(\sprintf( | ||
'<error>%s</error>', | ||
$exception->getMessage() | ||
)); | ||
} catch (Normalizer\Exception\OriginalInvalidAccordingToSchemaException $exception) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll never get here either, keeping it only for consistency. |
||
$io->writeError('<error>Original composer.json does not match the expected JSON schema:</error>'); | ||
|
||
$errors = $exception->errors(); | ||
$this->showValidationErrors( | ||
$io, | ||
...$exception->errors() | ||
); | ||
|
||
\array_walk($errors, static function (string $error) use ($io): void { | ||
$io->writeError($error); | ||
}); | ||
return 1; | ||
} catch (Normalizer\Exception\NormalizedInvalidAccordingToSchemaException $exception) { | ||
$io->writeError('<error>Normalized composer.json does not match the expected JSON schema:</error>'); | ||
|
||
$io->writeError('<warning>See https://getcomposer.org/doc/04-schema.md for details on the schema</warning>'); | ||
$this->showValidationErrors( | ||
$io, | ||
...$exception->errors() | ||
); | ||
|
||
return 1; | ||
} catch (\RuntimeException $exception) { | ||
|
@@ -309,6 +304,18 @@ private function indentFrom(Console\Input\InputInterface $input): ?Normalizer\Fo | |
return $indent; | ||
} | ||
|
||
private function showValidationErrors(IO\IOInterface $io, string ...$errors): void | ||
{ | ||
foreach ($errors as $error) { | ||
$io->writeError(\sprintf( | ||
'<error>- %s</error>', | ||
$error | ||
)); | ||
} | ||
|
||
$io->writeError('<warning>See https://getcomposer.org/doc/04-schema.md for details on the schema</warning>'); | ||
} | ||
|
||
/** | ||
* @param string $before | ||
* @param string $after | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
{ | ||
"description": "This composer.json is not valid as it is missing required fields." | ||
"_comment": "This composer.json is not valid according to a lax validation, as existing fields have unexpected types", | ||
"name": [ | ||
"foo", | ||
"bar" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
test/Fixture/json/valid/lock/not-present/json/not-yet-normalized/composer.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
test/Fixture/json/valid/lock/present/lock/fresh-before/json/already-normalized/composer.lock
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
test/Fixture/json/valid/lock/present/lock/not-fresh-before/composer.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This useless because
Factory::createComposer()
is run earlier already - we never even get here.