https://github.com/mesak/laravel-opis-validator
Laravel FormRequest With Opis Validator
https://github.com/mesak/laravel-opis-validator
Last synced: 30 days ago
JSON representation
Laravel FormRequest With Opis Validator
- Host: GitHub
- URL: https://github.com/mesak/laravel-opis-validator
- Owner: mesak
- License: mit
- Created: 2022-08-14T15:13:14.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-08-16T09:04:52.000Z (almost 4 years ago)
- Last Synced: 2026-02-23T18:50:18.019Z (5 months ago)
- Language: PHP
- Size: 9.77 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Laravel Opis JSON Schema
Laravel FormRequest With Opis JSON Schema Validator
Use [Opis JSON Schema](https://github.com/opis/json-schema) to validate your laravel form requests.
# installation
```bash
composer require mesak/laravel-opis-validator
```
Or you could directly reference it into your `composer.json` file as a dependency
```json
{
"require": {
"mesak/laravel-opis-validator": "^1.0.0"
}
}
```
## Example
### Requests
```php
"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
```php
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
}
}'
```