-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
2b80e54
commit fa93dd7
Showing
5 changed files
with
434 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.