1
0
Fork 0

Merge pull request #9938 from adlacruzes/fix_custom_json_schema

Fix JsonFile when using custom json schema with no "name" and "descri…
pull/9942/head
Jordi Boggiano 2021-06-04 08:10:46 +02:00 committed by GitHub
commit d45e3a98e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 3 deletions

View File

@ -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) {

View File

@ -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 = '{