From cc55b56dd70f260b14035e7c7f91078d5e7674dd Mon Sep 17 00:00:00 2001 From: BoShurik Date: Tue, 16 Mar 2021 15:33:49 +0300 Subject: [PATCH] Allow optional name and description fields for project type --- res/composer-schema.json | 21 ++++++++++++++++++- src/Composer/Json/JsonFile.php | 2 +- .../Composer/Test/Json/ComposerSchemaTest.php | 21 +++++++++++++++++-- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/res/composer-schema.json b/res/composer-schema.json index 7aa8fbd1a..35d5f75cf 100644 --- a/res/composer-schema.json +++ b/res/composer-schema.json @@ -3,7 +3,26 @@ "name": "Package", "type": "object", "additionalProperties": false, - "required": [ "name", "description" ], + "oneOf": [ + { + "properties": { + "type": { + "not": { + "enum": ["project"] + } + } + }, + "required": ["name", "description"] + }, + { + "properties": { + "type": { + "enum": ["project"] + } + }, + "required": ["type"] + } + ], "properties": { "name": { "type": "string", diff --git a/src/Composer/Json/JsonFile.php b/src/Composer/Json/JsonFile.php index b15098d56..ff4dcc8c8 100644 --- a/src/Composer/Json/JsonFile.php +++ b/src/Composer/Json/JsonFile.php @@ -198,7 +198,7 @@ class JsonFile if ($schema === self::LAX_SCHEMA) { $schemaData->additionalProperties = true; - $schemaData->required = array(); + $schemaData->oneOf = null; } $validator = new Validator(); diff --git a/tests/Composer/Test/Json/ComposerSchemaTest.php b/tests/Composer/Test/Json/ComposerSchemaTest.php index 687c1137a..d6880348a 100644 --- a/tests/Composer/Test/Json/ComposerSchemaTest.php +++ b/tests/Composer/Test/Json/ComposerSchemaTest.php @@ -41,18 +41,35 @@ class ComposerSchemaTest extends TestCase { $json = '{ }'; $result = $this->check($json); - $this->assertContains(array('property' => 'name', 'message' => 'The property name is required', 'constraint' => 'required'), $result); - $this->assertContains(array('property' => 'description', 'message' => 'The property description is required', 'constraint' => 'required'), $result); + $this->assertContains(array('property' => 'type', 'message' => 'The property type is required', 'constraint' => 'required'), $result); $json = '{ "name": "vendor/package" }'; $this->assertEquals(array( + array('property' => 'type', 'message' => 'The property type is required', 'constraint' => 'required'), array('property' => 'description', 'message' => 'The property description is required', 'constraint' => 'required'), + array('property' => '', 'message' => 'Failed to match exactly one schema', 'constraint' => 'oneOf'), ), $this->check($json)); $json = '{ "description": "generic description" }'; $this->assertEquals(array( + array('property' => 'type', 'message' => 'The property type is required', 'constraint' => 'required'), array('property' => 'name', 'message' => 'The property name is required', 'constraint' => 'required'), + array('property' => '', 'message' => 'Failed to match exactly one schema', 'constraint' => 'oneOf'), ), $this->check($json)); + + $json = '{ "type": "library" }'; + $this->assertEquals(array( + array('property' => 'type', 'message' => 'Does not have a value in the enumeration ["project"]', 'constraint' => 'enum', 'enum' => array('project')), + array('property' => 'name', 'message' => 'The property name is required', 'constraint' => 'required'), + array('property' => 'description', 'message' => 'The property description is required', 'constraint' => 'required'), + array('property' => '', 'message' => 'Failed to match exactly one schema', 'constraint' => 'oneOf'), + ), $this->check($json)); + + $json = '{ "type": "project" }'; + $this->assertTrue($this->check($json)); + + $json = '{ "name": "vendor/package", "description": "description" }'; + $this->assertTrue($this->check($json)); } public function testOptionalAbandonedProperty()