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
commit
d45e3a98e4
|
@ -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) {
|
||||
|
|
|
@ -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 = '{
|
||||
|
|
Loading…
Reference in New Issue