LaravelPackages.net
Acme Inc.
Toggle sidebar
mesak/laravel-opis-validator

Laravel FormRequest With Opis Validator

4.999
1
v1.0.2
About mesak/laravel-opis-validator

mesak/laravel-opis-validator is a Laravel package for laravel formrequest with opis validator. It currently has 1 GitHub stars and 4.999 downloads on Packagist (latest version v1.0.2). Install it with composer require mesak/laravel-opis-validator. Discover more Laravel packages by mesak or browse all Laravel packages to compare alternatives.

Last updated

Laravel Opis JSON Schema

Laravel FormRequest With Opis JSON Schema Validator

Use Opis JSON Schema to validate your laravel form requests.

installation

composer require mesak/laravel-opis-validator

Or you could directly reference it into your composer.json file as a dependency

{
    "require": {
        "mesak/laravel-opis-validator": "^1.0.0"
    }
}

Example

Requests

<?php

namespace App\Http\Requests;

use Mesak\LaravelOpisValidator\JsonSchemaRequest;

class JsonSchema extends JsonSchemaRequest
{
    protected $extendValidatorMessage = true;

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            '$schema' => "http://json-schema.org/draft-07/schema#",
            "type" => "object",
            "title" => "Base Preference",
            "description" => "Base Preference Setting",
            "properties" => [
                "limit" => [
                    "type" => "integer",
                    "minimum" => 5,
                    "maximum" => 15,
                    "title" => "limit",
                    "attrs" => [
                        "placeholder" => "limit (limit)"
                    ]
                ],
                "page" => [
                    "type" => "object",
                    "title" => "Page",
                    "attrs" => [
                        "placeholder" => "Page ( Page )"
                    ],
                    "properties" => [
                        "limit" => [
                            "type" => "integer"
                        ]
                    ]
                ]
            ],
            "additionalProperties" => false,
            "required" => [
                "limit",
                "page"
            ]
        ];
    }
    protected function prepareForValidation()
    {
        //clear urlencoded data from request 
        //input integer is not allowed in json schema
        $intance = $this->getValidatorInstance();
        foreach ($intance->getRules() as $key => $rule) {
            $inputValue = $this->input($key);
            if ($rule == 'integer' && preg_match('/\d+/', $inputValue)) {
                $this->merge([
                $key => intval($inputValue),
                ]);
            }
        }
        $intance->setData($this->all());
    }
}

Controller


use App\Http\Requests\JsonSchema as JsonSchemaRequest;

    public function update(JsonSchemaRequest $request)
    {
        dd($request->validated());
    }

use postman to test your request.

curl --location --request POST 'http://localhost/test/update' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--data-raw '{
    "limit" :10,
    "page": {
        "limit" : 10
    }
}'
Comments