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.
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
- Run
php artisan vendor:publish --provider="RamosHenrique\JsonSchemaValidator". This will create on yourconfigfolder a file namedjson_schema_validator.php. - In your
.envfile, add your JSON Schema files storage path with keyJSON_SCHEMA_VALIDATOR_STORAGE_PATH(i.eJSON_SCHEMA_VALIDATOR_STORAGE_PATH=storage/jsonschemas/). - 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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 henriqueramos/laravel_json_schema_validator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Runn Me! Validation and Sanitization Library
Kinikit - PHP Application development framework MVC component
Extension for Opis JSON Schema
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
统计信息
- 总下载量: 26.36k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-04-26