4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
22 use Composer\Spdx\SpdxLicenses
;
23 use JsonSchema\Validator
;
28 class ExtensionJsonValidator
{
33 private $missingDepCallback;
36 * @param callable $missingDepCallback
38 public function __construct( callable
$missingDepCallback ) {
39 $this->missingDepCallback
= $missingDepCallback;
45 public function checkDependencies() {
46 if ( !class_exists( Validator
::class ) ) {
47 call_user_func( $this->missingDepCallback
,
48 'The JsonSchema library cannot be found, please install it through composer.'
51 } elseif ( !class_exists( SpdxLicenses
::class ) ) {
52 call_user_func( $this->missingDepCallback
,
53 'The spdx-licenses library cannot be found, please install it through composer.'
62 * @param string $path file to validate
63 * @return bool true if passes validation
64 * @throws ExtensionJsonValidationError on any failure
66 public function validate( $path ) {
67 $data = json_decode( file_get_contents( $path ) );
68 if ( !is_object( $data ) ) {
69 throw new ExtensionJsonValidationError( "$path is not valid JSON" );
72 if ( !isset( $data->manifest_version
) ) {
73 throw new ExtensionJsonValidationError(
74 "$path does not have manifest_version set." );
77 $version = $data->manifest_version
;
78 $schemaPath = __DIR__
. "/../../docs/extension.schema.v$version.json";
81 if ( $version < ExtensionRegistry
::OLDEST_MANIFEST_VERSION
) {
82 throw new ExtensionJsonValidationError(
83 "$path is using a non-supported schema version"
85 } elseif ( $version > ExtensionRegistry
::MANIFEST_VERSION
) {
86 throw new ExtensionJsonValidationError(
87 "$path is using a non-supported schema version"
91 $licenseError = false;
92 // Check if it's a string, if not, schema validation will display an error
93 if ( isset( $data->{'license-name'} ) && is_string( $data->{'license-name'} ) ) {
94 $licenses = new SpdxLicenses();
95 $valid = $licenses->validate( $data->{'license-name'} );
97 $licenseError = '[license-name] Invalid SPDX license identifier, '
98 . 'see <https://spdx.org/licenses/>';
102 $validator = new Validator
;
103 $validator->check( $data, (object)[ '$ref' => 'file://' . $schemaPath ] );
104 if ( $validator->isValid() && !$licenseError ) {
108 $out = "$path did pass validation.\n";
109 foreach ( $validator->getErrors() as $error ) {
110 $out .= "[{$error['property']}] {$error['message']}\n";
112 if ( $licenseError ) {
113 $out .= "$licenseError\n";
115 throw new ExtensionJsonValidationError( $out );