carsdotcom/laravel-json-schema
Composer 安装命令:
composer require carsdotcom/laravel-json-schema
包简介
Json Schema validation for Laravel projects
README 文档
README
Use JsonSchema in Laravel apps
Purpose
This library builds on the outstanding JsonSchema validator opis/json-schema
The entire intent of this library is to make JsonSchema feel like a first class citizen in a Laravel project.
- Adds a new config file,
config/json-schema.php, to configure your root directory for self-hosted schema files. - Adds the
SchemaValidatorfacade that can be used to instantiate the validator with appropriate loaders. - Adds new PhpUnit assertions in the
Carsdotcom\JsonSchemaValidation\Traits\JsonSchemaAssertionstrait, such as validating that a mixed item validates for a specific schema. - Most interestingly, it lets you use JsonSchema to validate incoming Requests bodies, and/or validate your own outgoing response bodies, all using JsonSchema schemas that you can then export into OpenAPI documentation.
Laravel Version Compatibility
This package supports Laravel v10+ and PHP 8.1+.
Installation
composer require carsdotcom/laravel-json-schema
Using Laravel JSON Schema
Setup
Config File
Copy the json-schema.php file from the vendor/carsdotcom/laravel-json-schema/config folder to your application's config folder.
Schema Storage
- Create a
Schemasfolder under your application root folder, such asapp/Schemas. - Create a new storage disk under the
diskskey within your application'sconfig/filesystem.phpfile:
'disks' => [
'schemas' => [
'driver' => 'local',
'root' => app_path('app/Schemas'), // must match the 'config.json-schema.local_base_prefix' value
]
]
- Add your schema files to the
app/Schemasfolder. You may create subfolders to keep things organized.
Generate Enum Schemas
This is an optional step, but can be super helpful.
For native PHP backed enums (BackedEnum) — recommended for all new code:
- Add
use Carsdotcom\JsonSchemaValidation\Traits\GeneratesSchemaTrait;to the enum. - Add a
SCHEMAconstant whose value is the relative path to your schema file:const SCHEMA = 'Acme/Enums/item_type.json'; - Run
php artisan schemas:generate.
For MyCLabs enums (MyCLabs\Enum\Enum subclasses) — legacy support only, not recommended for new code:
- Add
use Carsdotcom\JsonSchemaValidation\Traits\GeneratesSchemaMyCLabsTrait;to the class. - Add a
SCHEMAconstant as above. - Run
php artisan schemas:generate.
Validating JSON Data Against a Schema
For this example, we'll be using these objects:
Hosted JSON Schema File
This is assumed to be stored in your app/Schemas folder as Product.json.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/product.schema.json",
"title": "Product",
"description": "A product from Acme's catalog",
"type": "object",
"properties": {
"productId": {
"description": "The unique identifier for a product",
"type": "integer"
},
"productName": {
"description": "Name of the product",
"type": "string"
},
"price": {
"description": "The price of the product",
"type": "number",
"exclusiveMinimum": 0
}
},
"required": [ "productId", "productName", "price" ]
}
JSON Data to be Validated
{
"productId": 1,
"productName": "An ice sculpture",
"price": 12.50
}
Application Code for Validation
use Carsdotcom\JsonSchemaValidation\SchemaValidator;
SchemaValidator::validateOrThrow($json, 'Product.json');
Additional Functionality
Getting the Content of a Schema File
use Carsdotcom\JsonSchemaValidation\SchemaValidator;
SchemaValidator::getSchemaContents('Product.json');
Storing a Schema File at a Specific Location
use Carsdotcom\JsonSchemaValidation\SchemaValidator;
SchemaValidator::getSchemaContents('Customer.json', $jsonSchemaForCustomer);
Adding an In-Memory Schema File
$schemaKey = (new SchemaValidatorService)->registerRawSchema($jsonSchema);
carsdotcom/laravel-json-schema 适用场景与选型建议
carsdotcom/laravel-json-schema 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 42.26k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2023 年 07 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「library」 「validation」 「laravel」 「JsonSchema」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 carsdotcom/laravel-json-schema 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 carsdotcom/laravel-json-schema 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 carsdotcom/laravel-json-schema 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Inbox pattern process implementation for your Laravel Applications
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
A Laravel validator for delimiter-separated list of emails.
A CakePHP behavior to validate foreign keys
Core library that defines common interfaces used by the rest of the intahwebz..
统计信息
- 总下载量: 42.26k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 17
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-07-06