function JsonFile::validateJsonSchema
Validates the schema of the current json file according to composer-schema.json rules
@phpstan-param self::*_SCHEMA $schema
Parameters
mixed $data Decoded JSON data to validate:
int $schema a JsonFile::*_SCHEMA constant:
string|null $schemaFile a path to the schema file:
Return value
true true on success
Throws
2 calls to JsonFile::validateJsonSchema()
- Factory::validateJsonSchema in vendor/
composer/ composer/ src/ Composer/ Factory.php - JsonFile::validateSchema in vendor/
composer/ composer/ src/ Composer/ Json/ JsonFile.php - Validates the schema of the current json file according to composer-schema.json rules
File
-
vendor/
composer/ composer/ src/ Composer/ Json/ JsonFile.php, line 229
Class
- JsonFile
- Reads/writes json files.
Namespace
Composer\JsonCode
public static function validateJsonSchema(string $source, $data, int $schema, ?string $schemaFile = null) : bool {
$isComposerSchemaFile = false;
if (null === $schemaFile) {
if ($schema === self::LOCK_SCHEMA) {
$schemaFile = self::LOCK_SCHEMA_PATH;
}
else {
$isComposerSchemaFile = true;
$schemaFile = self::COMPOSER_SCHEMA_PATH;
}
}
// Prepend with file:// only when not using a special schema already (e.g. in the phar)
if (false === strpos($schemaFile, '://')) {
$schemaFile = 'file://' . $schemaFile;
}
$schemaData = (object) [
'$ref' => $schemaFile,
];
if ($schema === self::LAX_SCHEMA) {
$schemaData->additionalProperties = true;
$schemaData->required = [];
}
elseif ($schema === self::STRICT_SCHEMA && $isComposerSchemaFile) {
$schemaData->additionalProperties = false;
$schemaData->required = [
'name',
'description',
];
}
elseif ($schema === self::AUTH_SCHEMA && $isComposerSchemaFile) {
$schemaData = (object) [
'$ref' => $schemaFile . '#/properties/config',
'$schema' => "https://json-schema.org/draft-04/schema#",
];
}
$validator = new Validator();
$validator->check($data, $schemaData);
if (!$validator->isValid()) {
$errors = [];
foreach ((array) $validator->getErrors() as $error) {
$errors[] = ($error['property'] ? $error['property'] . ' : ' : '') . $error['message'];
}
throw new JsonValidationException('"' . $source . '" does not match the expected JSON schema', $errors);
}
return true;
}