GithubHelp home page GithubHelp logo

laravel-json-schema-validate's Introduction

Laravel JsonSchema Validate

JsonSchema Validate For Laravel Request And Response

Installation

composer require kojirock5260/laravel-json-schema-validate
php artisan vendor:publish --provider=Kojirock5260\\JsonSchemaServiceProvider

Setup

Please describe

1. app/Http/Kernel.php

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

2. routes/*.php

<?php

Route::group(['middleware' => ['json_schema']], function () {
    Route::get('/member', 'MemberController@index')->name('MemberList');
});
  • Be sure to set the route name

3. Schema Files

Path

Request
  • App\Http\Schema\Request\{RouteName}.php
Response
  • App\Http\Schema\Response\{RouteName}.php
<?php

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#',
            'required'   => ['page'],
            'type'       => 'object',
            'properties' => [
                'page' => [
                    'type'    => 'string',
                    'pattern' => '^([1-9]|[1-9][0-9]*)$',
                ],
                'employment' => [
                    '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'
                ],
            ],
        ];
    }
}

Schema Directory Customise

<?php

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]);
    }
}

License

The MIT License (MIT). Please see License File for more information.

laravel-json-schema-validate's People

Contributors

kojirock5260 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.