承接 carsdotcom/laravel-json-schema 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

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 SchemaValidator facade that can be used to instantiate the validator with appropriate loaders.
  • Adds new PhpUnit assertions in the Carsdotcom\JsonSchemaValidation\Traits\JsonSchemaAssertions trait, 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

  1. Create a Schemas folder under your application root folder, such as app/Schemas.
  2. Create a new storage disk under the disks key within your application's config/filesystem.php file:
'disks' => [
    'schemas' => [
        'driver' => 'local',
        'root' => app_path('app/Schemas'), // must match the 'config.json-schema.local_base_prefix' value
    ]
]
  1. Add your schema files to the app/Schemas folder. 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:

  1. Add use Carsdotcom\JsonSchemaValidation\Traits\GeneratesSchemaTrait; to the enum.
  2. Add a SCHEMA constant whose value is the relative path to your schema file: const SCHEMA = 'Acme/Enums/item_type.json';
  3. Run php artisan schemas:generate.

For MyCLabs enums (MyCLabs\Enum\Enum subclasses) — legacy support only, not recommended for new code:

  1. Add use Carsdotcom\JsonSchemaValidation\Traits\GeneratesSchemaMyCLabsTrait; to the class.
  2. Add a SCHEMA constant as above.
  3. 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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 42.26k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 17
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 17
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-07-06