diff --git a/src/Composer/Json/JsonFile.php b/src/Composer/Json/JsonFile.php index 07a6f922f..1b662fa91 100644 --- a/src/Composer/Json/JsonFile.php +++ b/src/Composer/Json/JsonFile.php @@ -185,7 +185,9 @@ class JsonFile self::validateSyntax($content, $this->path); } + $isComposerSchemaFile = false; if (null === $schemaFile) { + $isComposerSchemaFile = true; $schemaFile = __DIR__ . self::COMPOSER_SCHEMA_PATH; } @@ -196,7 +198,10 @@ class JsonFile $schemaData = (object) array('$ref' => $schemaFile); - if ($schema !== self::LAX_SCHEMA) { + if ($schema === self::LAX_SCHEMA) { + $schemaData->additionalProperties = true; + $schemaData->required = array(); + } elseif ($schema === self::STRICT_SCHEMA && $isComposerSchemaFile) { $schemaData->additionalProperties = false; $schemaData->required = array('name', 'description'); } @@ -204,8 +209,6 @@ class JsonFile $validator = new Validator(); $validator->check($data, $schemaData); - // TODO add more validation like check version constraints and such, perhaps build that into the arrayloader? - if (!$validator->isValid()) { $errors = array(); foreach ((array) $validator->getErrors() as $error) { diff --git a/tests/Composer/Test/Json/JsonFileTest.php b/tests/Composer/Test/Json/JsonFileTest.php index eb0646ae4..d93b0414d 100644 --- a/tests/Composer/Test/Json/JsonFileTest.php +++ b/tests/Composer/Test/Json/JsonFileTest.php @@ -207,6 +207,38 @@ class JsonFileTest extends TestCase unlink($file); } + public function testCustomSchemaValidationLax() + { + $file = tempnam(sys_get_temp_dir(), 'c'); + file_put_contents($file, '{ "custom": "property", "another custom": "property" }'); + + $schema = tempnam(sys_get_temp_dir(), 'c'); + file_put_contents($schema, '{ "properties": { "custom": { "type": "string" }}}'); + + $json = new JsonFile($file); + + $this->assertTrue($json->validateSchema(JsonFile::LAX_SCHEMA, $schema)); + + unlink($file); + unlink($schema); + } + + public function testCustomSchemaValidationStrict() + { + $file = tempnam(sys_get_temp_dir(), 'c'); + file_put_contents($file, '{ "custom": "property" }'); + + $schema = tempnam(sys_get_temp_dir(), 'c'); + file_put_contents($schema, '{ "properties": { "custom": { "type": "string" }}}'); + + $json = new JsonFile($file); + + $this->assertTrue($json->validateSchema(JsonFile::STRICT_SCHEMA, $schema)); + + unlink($file); + unlink($schema); + } + public function testParseErrorDetectMissingCommaMultiline() { $json = '{