From 4105bd6f683bd6524b100db7c793f5793fc26d76 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Wed, 21 Feb 2018 19:11:34 +1300 Subject: [PATCH] NEW Add schema:validate command and return command exit code --- readme.md | 7 +++ src/Application.php | 3 ++ src/Commands/Command.php | 2 +- src/Commands/Schema/Validate.php | 66 +++++++++++++++++++++++++++ src/Model/Modules/Library.php | 4 +- src/Utility/Config.php | 6 +-- src/Utility/SchemaValidator.php | 49 ++++++++++++++++++++ tests/Utility/SchemaValidatorTest.php | 59 ++++++++++++++++++++++++ 8 files changed, 190 insertions(+), 6 deletions(-) create mode 100644 src/Commands/Schema/Validate.php create mode 100644 src/Utility/SchemaValidator.php create mode 100644 tests/Utility/SchemaValidatorTest.php diff --git a/readme.md b/readme.md index 30bb018..fa21d6e 100644 --- a/readme.md +++ b/readme.md @@ -108,3 +108,10 @@ each of which could be run separately. After the push step, `release:publish` will automatically wait for this version to be available in packagist.org before continuing. + +## Schema + +The [cow schema file](cow.schema.json) is in the root of this project. + +You can run `cow schema:validate` to check the `.cow.json` configuration file in your project or module to +ensure it matches against the cow schema. diff --git a/src/Application.php b/src/Application.php index 33031c6..bbf7844 100644 --- a/src/Application.php +++ b/src/Application.php @@ -69,6 +69,9 @@ protected function getDefaultCommands() // Module commands $commands[] = new Commands\Module\TranslateBuild(); + // Schema commands + $commands[] = new Commands\Schema\Validate(); + return $commands; } } diff --git a/src/Commands/Command.php b/src/Commands/Command.php index 0e10be7..5609f3a 100644 --- a/src/Commands/Command.php +++ b/src/Commands/Command.php @@ -55,7 +55,7 @@ protected function execute(InputInterface $input, OutputInterface $output) // Configure extra output formats $this->output->getFormatter()->setStyle('bold', new OutputFormatterStyle('blue')); - $this->fire(); + return $this->fire(); } /** diff --git a/src/Commands/Schema/Validate.php b/src/Commands/Schema/Validate.php new file mode 100644 index 0000000..ab63ae6 --- /dev/null +++ b/src/Commands/Schema/Validate.php @@ -0,0 +1,66 @@ +getCowData(); + + /** @var Validator $validator */ + $validator = SchemaValidator::validate($cowData); + + if ($validator->isValid()) { + $this->output->writeln('Cow schema is valid!'); + return 0; + } + + $this->output->writeln('Cow schema failures:'); + foreach ($validator->getErrors() as $error) { + $this->output->writeln('' . $error['property'] . ': ' . $error['message'] . ''); + } + return 1; + } + + protected function configureOptions() + { + // noop + } +} diff --git a/src/Model/Modules/Library.php b/src/Model/Modules/Library.php index 3c3d30c..e41f5bb 100644 --- a/src/Model/Modules/Library.php +++ b/src/Model/Modules/Library.php @@ -15,6 +15,7 @@ use SilverStripe\Cow\Model\Release\Version; use SilverStripe\Cow\Utility\Config; use SilverStripe\Cow\Utility\Format; +use SilverStripe\Cow\Utility\SchemaValidator; use Symfony\Component\Console\Logger\ConsoleLogger; use Symfony\Component\Console\Output\OutputInterface; @@ -628,9 +629,8 @@ public function getArchives() */ public function getCowData() { - // http://json-schema.org/examples.html $path = $this->getDirectory() . '/.cow.json'; - $schemaPath = dirname(dirname(dirname(__DIR__))).'/cow.schema.json'; + $schemaPath = dirname(dirname(dirname(__DIR__))) . '/' . SchemaValidator::SCHEMA_FILENAME; return Config::loadFromFile($path, $schemaPath); } diff --git a/src/Utility/Config.php b/src/Utility/Config.php index 231db92..add4e7a 100644 --- a/src/Utility/Config.php +++ b/src/Utility/Config.php @@ -3,7 +3,6 @@ namespace SilverStripe\Cow\Utility; use Exception; -use JsonSchema\Validator; class Config { @@ -27,9 +26,10 @@ public static function loadFromFile($path, $schemaPath = null) if ($schemaPath) { // Note: Parse as object (assoc = false) for validation $schema = static::loadFromFile($schemaPath); - $validator = new Validator(); + $objectData = self::parseContent($content, false); - $validator->validate($objectData, $schema); + $validator = SchemaValidator::validate($objectData, $schema); + if (!$validator->isValid()) { $errors = []; foreach ($validator->getErrors() as $error) { diff --git a/src/Utility/SchemaValidator.php b/src/Utility/SchemaValidator.php new file mode 100644 index 0000000..9ec4d30 --- /dev/null +++ b/src/Utility/SchemaValidator.php @@ -0,0 +1,49 @@ +validate($objectData, $schema); + + return $validator; + } +} diff --git a/tests/Utility/SchemaValidatorTest.php b/tests/Utility/SchemaValidatorTest.php new file mode 100644 index 0000000..24a93c6 --- /dev/null +++ b/tests/Utility/SchemaValidatorTest.php @@ -0,0 +1,59 @@ +cowSchema = file_get_contents(dirname(dirname(__DIR__)) . '/cow.schema.json'); + } + + public function testValidSchema() + { + $cowConfig = <<assertTrue($validator->isValid()); + } + + public function testInvalidSchema() + { + $cowConfig = <<assertFalse($validator->isValid()); + } +}