定制 henriqueramos/laravel_json_schema_validator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

henriqueramos/laravel_json_schema_validator

Composer 安装命令:

composer require henriqueramos/laravel_json_schema_validator

包简介

Laravel JSON Schema Validator it's a Composer package created to help us to validate JSON Schemas.

README 文档

README

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',
        ];
    }
}

henriqueramos/laravel_json_schema_validator 适用场景与选型建议

henriqueramos/laravel_json_schema_validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.36k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2020 年 04 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「package」 「json」 「validator」 「laravel」 「JSON Schema」 「Henrique Ramos」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 henriqueramos/laravel_json_schema_validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 henriqueramos/laravel_json_schema_validator 我们能提供哪些服务?
定制开发 / 二次开发

基于 henriqueramos/laravel_json_schema_validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 26.36k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 2
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-26