Giter Club home page Giter Club logo

laravel-json-schema-validator's Introduction

JSON Schema Validator for Laravel

JSON Schema Validator for Laravel it is a Composer package created to validate JSON objects against JSON Schemas as an Illuminate\Validation\Validator custom rule.

Build Status Scrutinizer Code Quality Latest Stable Version License

About

This package works only at Laravel versions >= 5.8. And PHP version >= 7.3.

We uses the incredible package swaggest/json-schema as a dependency to make everything works like magic.

Installation

Add the following line to the require section of composer.json:

{
    "require": {
        "henriqueramos/laravel_json_schema_validator": "^1.0.0"
    }
}

Setup

  1. Run php artisan vendor:publish --provider="RamosHenrique\JsonSchemaValidator". This will create on your config folder a file named json_schema_validator.php.
  2. In your .env file, add your JSON Schema files storage path with key JSON_SCHEMA_VALIDATOR_STORAGE_PATH (i.e JSON_SCHEMA_VALIDATOR_STORAGE_PATH=storage/jsonschemas/).
  3. Set up your JSON Schema file

What is a JSON Schema

We supported the following schemas:

Here's an example for JSON Schema and a valid payload for him:

$schemaJson = <<<'JSON'
{
    "type": "object",
    "properties": {
        "uuid": {
            "type": "integer"
        },
        "userId": {
            "type": "integer"
        },
        "items": {
            "type": "array",
            "minimum": 1,
            "items": {
                "$ref": "#/definitions/items"
            }
        }
    },
    "required":[
	    "uuid",
	    "userId",
	    "items",
	],
    "definitions": {
        "items": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer"
                },
                "price": {
                    "type": "number"
                },
                "updated": {
                    "type": "string",
                    "format": "date-time"
                }
            },
            "required":[
	            "id",
	            "price"
	        ]
        }
    }
}
JSON;

$payload = <<<'JSON'
{
  "uuid": 8,
  "userId": 1,
  "items": [
	{
    	"id": 12,
        "price": 49.90,
        "updated": "2020-09-07T20:20:39-03:00"
	},
    {
      	"id": 15,
      	"price": 99,
      	"updated": "2020-06-22T16:48:12-03:00"
    }
  ]
}
JSON;

Usage

After save the JSON Schema file on your chosen storage path, you can use this as a Validation Rule on your FormRequest extended class.

<?php declare(strict_types = 1);

namespace RamosHenrique\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ValidatingPayloadRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(): array
    {
        return [
            'jsonData' => [
                'bail',
                'required',
                'json',
                'json_schema_validator:validJSONSchema.json',
            ],
        ];
    }

    /**
     * Custom messages for the route validator.
     *
     * @return array
     */
    public function messages(): array
    {
        return [
            'expectedData.required' => 'required.jsonData',
            'expectedData.json' => 'expectedData.needs.needs.to.be.a.valid.json',
        ];
    }
}

laravel-json-schema-validator's People

Contributors

henriqueramos avatar

Watchers

 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.