PHP code example of tricktrick / laravel-json-schema-validate-fork

1. Go to this page and download the library: Download tricktrick/laravel-json-schema-validate-fork 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/ */

    

tricktrick / laravel-json-schema-validate-fork example snippets


    protected $routeMiddleware = [
        ...
        'json_schema' => \Kojirock5260\Middleware\JsonSchemaValidate::class,
    ];



Route::group(['middleware' => ['json_schema']], function () {
    Route::get('/member', 'MemberController@index')->name('MemberList');
});



declare(strict_types=1);

namespace App\Http\Schema\Request;

use Kojirock5260\SchemaInterface;

class MemberListSchema implements SchemaInterface
{
    public static function getSchema(): array
    {
        return [
            '$schema'    => 'http://json-schema.org/draft-07/schema#',
            '     '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'
                ],
            ],
        ];
    }
}




declare(strict_types=1);

namespace App\Http\Middleware;

class AppJsonSchemaValidate extends \Kojirock5260\Middleware\JsonSchemaValidate
{
    /**
     * Get JsonSchema ClassName.
     * @param \Illuminate\Routing\Route $route
     * @param string                    $type
     * @return string
     */
    public function getJsonSchemaClassName(\Illuminate\Routing\Route $route, string $type): string
    {
        $routeName   = $route->getName();
        $routePrefix = $this->getRoutePrefix($route);
        return "App\\Http\\Schema\\{$routePrefix}\\{$type}\\{$routeName}Schema";
    }

    /**
     * Get Route Prefix.
     * @param \Illuminate\Routing\Route $route
     * @return string
     */
    public function getRoutePrefix(\Illuminate\Routing\Route $route): string
    {
        // prefix = api/admin
        $prefixData = explode('/', $route->getPrefix());

        if (!isset($prefixData[1])) {
            return 'Front';
        }
        return ucfirst($prefixData[1]);
    }
}

bash
php artisan vendor:publish --provider=Kojirock5260\\JsonSchemaValidate\\JsonSchemaServiceProvider