Fix JsonFile when using custom json schema with no "name" and "description" properties
parent
c686bbd7e2
commit
7366b8e362
|
@ -180,6 +180,7 @@ class JsonFile
|
||||||
{
|
{
|
||||||
$content = file_get_contents($this->path);
|
$content = file_get_contents($this->path);
|
||||||
$data = json_decode($content);
|
$data = json_decode($content);
|
||||||
|
$requiredSchemaData = array();
|
||||||
|
|
||||||
if (null === $data && 'null' !== $content) {
|
if (null === $data && 'null' !== $content) {
|
||||||
self::validateSyntax($content, $this->path);
|
self::validateSyntax($content, $this->path);
|
||||||
|
@ -187,6 +188,7 @@ class JsonFile
|
||||||
|
|
||||||
if (null === $schemaFile) {
|
if (null === $schemaFile) {
|
||||||
$schemaFile = __DIR__ . self::COMPOSER_SCHEMA_PATH;
|
$schemaFile = __DIR__ . self::COMPOSER_SCHEMA_PATH;
|
||||||
|
$requiredSchemaData = array('name', 'description');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepend with file:// only when not using a special schema already (e.g. in the phar)
|
// Prepend with file:// only when not using a special schema already (e.g. in the phar)
|
||||||
|
@ -198,7 +200,7 @@ class JsonFile
|
||||||
|
|
||||||
if ($schema !== self::LAX_SCHEMA) {
|
if ($schema !== self::LAX_SCHEMA) {
|
||||||
$schemaData->additionalProperties = false;
|
$schemaData->additionalProperties = false;
|
||||||
$schemaData->required = array('name', 'description');
|
$schemaData->required = $requiredSchemaData;
|
||||||
}
|
}
|
||||||
|
|
||||||
$validator = new Validator();
|
$validator = new Validator();
|
||||||
|
|
|
@ -207,6 +207,38 @@ class JsonFileTest extends TestCase
|
||||||
unlink($file);
|
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()
|
public function testParseErrorDetectMissingCommaMultiline()
|
||||||
{
|
{
|
||||||
$json = '{
|
$json = '{
|
||||||
|
|
Loading…
Reference in New Issue