Skip to content

Commit

Permalink
Validate schema before writing the file, fixes #9986
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Jun 27, 2021
1 parent 420d9bf commit a5efbbc
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Composer/Command/InitCommand.php
Expand Up @@ -14,6 +14,7 @@

use Composer\Factory;
use Composer\Json\JsonFile;
use Composer\Json\JsonValidationException;
use Composer\Package\BasePackage;
use Composer\Package\CompletePackageInterface;
use Composer\Package\Package;
Expand All @@ -27,6 +28,7 @@
use Composer\Util\Filesystem;
use Composer\Util\ProcessExecutor;
use Composer\Semver\Constraint\Constraint;
use Composer\Util\Silencer;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -162,6 +164,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$file->write($options);
try {
$file->validateSchema(JsonFile::LAX_SCHEMA);
} catch (JsonValidationException $e) {
$io->writeError('<error>Schema validation error, aborting</error>');
$errors = ' - ' . implode(PHP_EOL . ' - ', $e->getErrors());
$io->writeError($e->getMessage() . ':' . PHP_EOL . $errors);
Silencer::call('unlink', $file->getPath());

return 1;
}

// --autoload - Create src folder
if ($autoloadPath) {
Expand Down

0 comments on commit a5efbbc

Please sign in to comment.