PHP code example of kojirock5260 / laravel-json-schema-validate
1. Go to this page and download the library: Download kojirock5260/laravel-json-schema-validate library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kojirock5260 / laravel-json-schema-validate example snippets
Route::group(['middleware' => ['json_schema']], function () {
Route::get('/member', 'MemberController@index')->name('MemberList');
});
declare(strict_types=1);
namespace App\Http\Schema\Request;
use Kojirock5260\JsonSchemaValidate\SchemaInterface;
class MemberList implements SchemaInterface
{
public static function getSchema(): array
{
return [
' 'type' => 'string',
'enum' => array_map('strval', array_keys(\App\Models\Member::EMPLOYMENT_LIST)),
],
'department' => [
'type' => 'string',
'enum' => array_map('strval', array_keys(\App\Models\Member::DEPARTMENT_LIST)),
],
'mailAddress' => [
'type' => 'string',
'format' => 'email'
],
],
];
}
}
/**
* Prepare exception for rendering.
*
* @param \Throwable $e
* @return \Throwable
*/
protected function prepareException(Throwable $e)
{
if ($e instanceof JsonSchemaException) {
return ValidationException::withMessages($e->getSchemaErrors());
}
return parent::prepareException($e);
}
return [
/**
* Schema Directory Base Namespace
*/
'namespace' => 'Acme\\Member\\Schema',
];
bash
php artisan vendor:publish --provider=Kojirock5260\\JsonSchemaValidate\\JsonSchemaServiceProvider
protected $routeMiddleware = [
...
'json_schema' => \Kojirock5260\JsonSchemaValidate\Middleware\JsonSchemaValidator::class,
];