DEV Community

Mesak
Mesak

Posted on

1 1

Laravel FormRequest With Opis Validator

I published a package for using Opis JSON Schema to FormRequest on laravel

https://github.com/mesak/laravel-opis-validator

installation

composer require mesak/laravel-opis-validator
Enter fullscreen mode Exit fullscreen mode

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"
            ]
        ];
    }
Enter fullscreen mode Exit fullscreen mode

Controller


use App\Http\Requests\JsonSchema as JsonSchemaRequest;

    public function update(JsonSchemaRequest $request)
    {
        dd($request->validated());
    }
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay