Skip to content

Commit

Permalink
[ Feat ] Validate whisky.json (#39)
Browse files Browse the repository at this point in the history
* validates whisky.json with jsonSchema

* adds test cases for validation

---------

Co-authored-by: Faissal Wahabali <contact@faissaloux.com>
Co-authored-by: Gerardo Ibarra <gibarra@upgradeit.com.ar>
Co-authored-by: Len Woodward <Len@ProjektGopher.com>
  • Loading branch information
4 people committed Aug 9, 2023
1 parent 2b80e54 commit fa93dd7
Show file tree
Hide file tree
Showing 5 changed files with 434 additions and 6 deletions.
119 changes: 119 additions & 0 deletions app/FileJson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace ProjektGopher\Whisky;

use Illuminate\Support\Facades\File;
use Swaggest\JsonSchema\Context;
use Swaggest\JsonSchema\Schema;

class FileJson
{
public function __construct(
public string $path,
) {
//
}

public static function make(string $path): FileJson
{
return new FileJson($path);
}

public function read(bool $validate = true): string|array|null
{
$content = json_decode(File::get($this->path), true);

if (json_last_error() !== JSON_ERROR_NONE) {
$msg = 'Invalid JSON';
if (function_exists('json_last_error_msg')) {
$msg .= ': '.json_last_error_msg();
$msg .= ' in '.$this->path;
}
throw new \Exception($msg);
}

if ($validate) {
try {
$contentJson = json_decode(File::get($this->path));
$this->validate($contentJson);
} catch (\Exception $e) {
$msg = 'Invalid JSON schema';
$msg .= ': '.$e->getMessage();
$msg .= ' in '.$this->path;
throw new \Exception($msg);
}
}

return $content;
}

/**
* @param \stdClass|array|string|int|float|bool|null $content
*
* @throws \Exception
*/
protected function validate(mixed $content): void
{
$options = new Context();
// $options->version = 7;

$schema = Schema::import($this->getSchemaValidation(), $options);

$schema->in($content);
}

protected function getSchemaValidation(): ?\stdClass
{
// return json_decode(File::get(Whisky::base_path('resources/schemas/whisky.json')), false);
$schema = [
'$schema' => 'http://json-schema.org/draft-07/schema#',
'type' => 'object',
'properties' => [
'disabled' => [
'type' => 'array',
'items' => [
'type' => 'string',
],
],
'hooks' => [
'type' => 'object',
'properties' => [],
'additionalProperties' => false,
],
],
'required' => ['hooks'],
'additionalProperties' => false,
];

// TODO: Move this into const on the Hooks class.
$availableHooks = [
'pre-commit',
'prepare-commit-msg',
'commit-msg',
'post-commit',
'applypatch-msg',
'pre-applypatch',
'post-applypatch',
'pre-rebase',
'post-rewrite',
'post-checkout',
'post-merge',
'pre-push',
'pre-auto-gc',
];
$schema['properties']['disabled']['items']['enum'] = $availableHooks;
foreach ($availableHooks as $hook) {
$schema['properties']['hooks']['properties'][$hook] = [
'type' => 'array',
'items' => [
'type' => 'string',
'minLength' => 1,
'pattern' => '^(?!\s*$).+',
],
'minItems' => 1,
];
}

return json_decode(json_encode($schema), false);
}
}
5 changes: 1 addition & 4 deletions app/Whisky.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace ProjektGopher\Whisky;

use Illuminate\Support\Facades\File;
use Phar;

class Whisky
Expand Down Expand Up @@ -50,9 +49,7 @@ public static function base_path(string $path = ''): string

public static function readConfig(string $key): string|array|null
{
$path = self::cwd('whisky.json');

$cfg = json_decode(File::get($path), true);
$cfg = FileJson::make(self::cwd('whisky.json'))->read();

return data_get($cfg, $key);
}
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"mockery/mockery": "^1.5.1",
"nunomaduro/larastan": "^2.0",
"nunomaduro/termwind": "^1.15.1",
"swaggest/json-schema": "^0.12.41",
"pestphp/pest": "^2.5",
"pestphp/pest-plugin-type-coverage": "^2.0"
},
Expand Down Expand Up @@ -67,7 +68,7 @@
"@php whisky test"
],
"types": [
"vendor/bin/pest --type-coverage --min=100"
"php -d memory_limit=1G vendor/bin/pest --type-coverage --min=100"
]
},
"minimum-stability": "stable",
Expand Down
144 changes: 143 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fa93dd7

Please sign in to comment.