amiralii / jsonschema-request

Laravel 服务器端表单验证使用 JSON Schema

dev-master 2018-06-28 15:00 UTC

This package is not auto-updated.

Last update: 2024-10-01 12:39:32 UTC


README

Laravel 服务器端表单验证使用 JSON Schema

安装

composer require amiralii/jsonschema-request

如何使用

  1. 创建一个表单请求类,就像使用 artisan 一样:php artisan make:request PersonRequest
  2. 在 PersonRequest 中更改扩展的类以使用 SchemaRequest/SchemaFormRequest
  3. 实现返回 JSON Schema 字符串的 rules() 方法

示例

<?php

namespace App\Http\Requests;

use SchemaRequest\SchemaFormRequest as Request;

class PersonFormRequest extends Request
{

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return '{
            "type": "object",
            "properties": {
                "fname": {"type": "string", "maximum": 5},
                "lname": {"type": "string", "minimum": 5},
                "mname": {"type": "string", "minimum": 5},
                "age": {"type": "integer", "minimum": 21},
                "email": {"type": "string", "format": "email"}
            }
        }';
    }
}