public static function validateJson($json, $errorMessages = false)
    {
        if (is_string($json) && is_readable($json) && !is_dir($json)) {
            $json = json_decode((new File($json))->read(), true);
        }

        $errors = [];
        if (!is_array($json) || empty($json)) {
            $errors[] = __d('cms', 'Corrupt JSON information.');
        } else {
            if (!isset($json['type'])) {
                $errors[] = __d('cms', 'Missing field: "{0}"', 'type');
            }

            if (!isset($json['name'])) {
                $errors[] = __d('cms', 'Missing field: "{0}"', 'name');
            } elseif (!preg_match('/^(.+)\/(.+)+$/', $json['name'])) {
                $errors[] = __d('cms', 'Invalid field: "{0}" ({1}). It should be: {2}', 'name', $json['name'], '{author-name}/{package-name}');
            } elseif (str_ends_with(strtolower($json['name']), 'theme')) {
                if (!isset($json['extra']['regions'])) {
                    $errors[] = __d('cms', 'Missing field: "{0}"', 'extra.regions');
                }
            }
        }

        if ($errorMessages) {
            return $errors;
        }

        return empty($errors);
    }
public static function validateJson($json, $errorMessages = false)
    {
        if (is_string($json) && is_readable($json) && !is_dir($json)) {
            $json = json_decode((new File($json))->read(), true);
        }

        $errors = [];
        if (!is_array($json) || empty($json)) {
            $errors[] = __d('cms', 'Corrupt JSON information.');
        } else {
            if (!isset($json['type'])) {
                $errors[] = __d('cms', 'Missing field: "{0}"', 'type');
            }

            if (!isset($json['name'])) {
                $errors[] = __d('cms', 'Missing field: "{0}"', 'name');
            } elseif (!preg_match('/^(.+)\/(.+)+$/', $json['name'])) {
                $errors[] = __d('cms', 'Invalid field: "{0}" ({1}). It should be: {2}', 'name', $json['name'], '{author-name}/{package-name}');
            } elseif (str_ends_with(strtolower($json['name']), 'theme')) {
                if (!isset($json['extra']['regions'])) {
                    $errors[] = __d('cms', 'Missing field: "{0}"', 'extra.regions');
                }
            }
        }

        if ($errorMessages) {
            return $errors;
        }

        return empty($errors);
    }
public static function validateJson ( $ json , $ errorMessages = false ) { if ( is_string ( $ json ) && is_readable ( $ json ) && ! is_dir ( $ json ) ) { $ json = json_decode ( ( new File ( $ json ) ) -> read ( ) , true ) ; } $ errors = [ ] ; if ( ! is_array ( $ json ) || empty ( $ json ) ) { $ errors [ ] = __d ( 'cms' , 'Corrupt JSON information.' ) ; } else { if ( ! isset ( $ json [ 'type' ] ) ) { $ errors [ ] = __d ( 'cms' , 'Missing field: "{0}"' , 'type' ) ; } if ( ! isset ( $ json [ 'name' ] ) ) { $ errors [ ] = __d ( 'cms' , 'Missing field: "{0}"' , 'name' ) ; } elseif ( ! preg_match ( '/^(.+)\/(.+)+$/' , $ json [ 'name' ] ) ) { $ errors [ ] = __d ( 'cms' , 'Invalid field: "{0}" ({1}). It should be: {2}' , 'name' , $ json [ 'name' ] , '{author-name}/{package-name}' ) ; } elseif ( str_ends_with ( strtolower ( $ json [ 'name' ] ) , 'theme' ) ) { if ( ! isset ( $ json [ 'extra' ] [ 'regions' ] ) ) { $ errors [ ] = __d ( 'cms' , 'Missing field: "{0}"' , 'extra.regions' ) ; } } } if ( $ errorMessages ) { return $ errors ; } return empty ( $ errors ) ; }
Validates a composer.json file.

Below a list of validation rules that are applied:

- must be a valid JSON file.
- key `name` must be present and follow the pattern `author/package`
- key `type` must be present.
- key `extra.regions` must be present if it's a theme (its name ends with
`-theme`, e.g. `quickapps/blue-sky-theme`)

### Usage:

```php
$json = json_decode(file_gets_content('/path/to/composer.json'), true);
Plugin::validateJson($json);

// OR:

Plugin::validateJson('/path/to/composer.json');
```

@param array|string $json JSON given as an array result of
`json_decode(..., true)`, or a string as path to where .json file can be found
@param bool $errorMessages If set to true an array of error messages
will be returned, if set to false boolean result will be returned; true on
success, false on validation failure. Defaults to false (boolean result)
@return array|bool
Validates a composer . json file .