承接 nayzo/json-schema-validator-bundle 相关项目开发

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

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

nayzo/json-schema-validator-bundle

Composer 安装命令:

composer require nayzo/json-schema-validator-bundle

包简介

Symfony bundle used to handle Json Schema validation for PSR7 & PSR17

README 文档

README

tests Latest Stable Version

The JsonSchemaValidatorBundle is a Symfony bundle that validate request payload and response with Json Schema for PSR7 & PSR17.

This Bundle is compatible with Symfony >= 5.0

Installation

Install the bundle:

$ composer require nayzo/json-schema-validator-bundle

Usage

Configuration:

# config/packages/nayzo_json_schema_validator.yaml
nayzo_json_schema_validator:
    resources:
        my_account:
            contract_file_path: '%kernel.project_dir%/resources/api_contract/myaccount1.yaml'
            enable_default_request_validation_subscriber: true  # default to false
            enable_default_response_validation_subscriber: true # default to false

        my_second_account:
            contract_file_path: '%kernel.project_dir%/resources/api_contract/myaccount2.yaml'

    enable_default_validation_exception_subscriber: true  # default to false. (if activated, the default validation exception subscriber will be triggered and executed).
    validation_exception_status_code: 400 # default to 422 

Define the Validation Support (mandatory):

Note that if no validation support is found (exp: MyAccountValidationSupport), no request/response validation subscriber will be triggered or executed.

<?php

use Nayzo\JsonSchemaValidatorBundle\Configurator\ValidationSubscriberSupportInterface;

final class MyAccountValidationSupport implements ValidationSubscriberSupportInterface
{
    public function __construct(private RequestStack $requestStack)
    {
    }
    
    public function support(): bool
    {
        // some code logic here... example:
        return '/v1/my-account' === $this->requestStack->getMainRequest()->getPathInfo();
    }

    public static function getResourceName(): string
    {
        return 'my_account';  // this value represents the resource name.
    }
}

Use default the request/response validation subscribers:

The JsonSchemaValidatorBundle defines two default validation subscribers: one for requests and another for responses.
These subscribers are responsible for validating request and response schemas.
To activate them, you must enable the corresponding flags: enable_default_request_validation_subscriber and enable_default_response_validation_subscriber, by setting their values to true.

If you prefer to use your own custom validation subscriber instead of the default ones provided by the bundle, you must disable the default subscribers by setting the enable_default_request_validation_subscriber flag to false, and then define the custom validation subscriber you wish to use.

Below is an example of usage without the default request/response validation subscribers.

<?php
    // EventSubscriber
    
    use Nayzo\JsonSchemaValidatorBundle\Validator\ValidatorFactory;
    
    public function __construct(private JsonSchemaValidatorContractInterface $myAccountContract)
    {
    }

    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::CONTROLLER => 'onKernelController',
            KernelEvents::RESPONSE => 'onKernelResponse',
        ];
    }
    
    public function onKernelController(ControllerEvent $event): void
    {       
        if ($event->getController() instanceof ErrorController) {
            return;
        }

        $request = $event->getRequest();
        $path = $this->myAccountContract->getContractFile();  // '/some/path/my-contract.yaml'
        
        $validatorFactory = new ValidatorFactory();
        $validator = $validatorFactory->build($path);
        $validator->validate($request, $request->getPathInfo(), $request->getMethod());
    }

    public function onKernelResponse(ResponseEvent $event): void
    {
        $request = $event->getRequest();
        $response = $event->getResponse();
        $path = $this->myAccountContract->getContractFile();  // '/some/path/my-contract.yaml'
        
        $validatorFactory = new ValidatorFactory();
        $validator = $validatorFactory->build($path);
        $validator->validate($response, $request->getPathInfo(), $request->getMethod());
    }

Contract Service Dependency Injection:

<?php

use Nayzo\JsonSchemaValidatorBundle\Contract\JsonSchemaValidatorContractInterface;

    public function __construct(
        private JsonSchemaValidatorContractInterface $myAccountContract, // represent "my_account" in "resources" configuration
        private JsonSchemaValidatorContractInterface $mySecondAccountContract, // represent "my_second_account" in "resources" configuration
    ) {
    }
    
    public function something(): array
    {
        $this->myAccountContract->getContractFile();        // '/some/path/my-contract.yaml'
        $this->mySecondAccountContract->getContractFile();  // '/some/path/my-second-contract.yaml'
    }

// Note: the "Contract" suffix must be added to the contract variable to be loaded correctly.

Exceptions

By default, the value of the enable_default_validation_exception_subscriber flag is set to false. If activated (set to true), the default validation exception subscriber will be triggered and executed.

To use your own custom validation exception subscriber, you must disable the enable_default_validation_exception_subscriber flag by setting its value to false and implement a subscriber that listens to kernel.exception events.

Be aware that when an exception is thrown Before the validation of the request or response implemented by the JsonSchemaValidatorBundle (for example, an access denied), it will be directly intercepted by the application's or framework's kernel.exception and will not be captured by the bundle's validation workflow.

OpenAPI

Official OpenAPI documentation here:

OpenAPI Specification (official): ➡️ https://spec.openapis.org/oas/v3.0.1.html

OpenAPI Guide (Swagger): ➡️ https://swagger.io/specification

Example of openapi implementation:
openapi: 3.0.0
info:
    title: 'Some Title'
    version: 1.0.0
paths:
    /v1/foo:
        get:
            summary: 'Some summary'
            description: 'Some description'
            parameters:
                -   name: uuid
                    in: query
                    required: true
                    schema:
                        type: string

            responses:
                '200':
                    description: 'Some description'
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/CustomComponent'
                '401':
                    description: Unauthorized
                '403':
                    description: Forbidden
                '404':
                    description: Resource Not Found
                '422':
                    description: Unprocessable Content
                '500':
                    description: Internal server error

        post:
            summary: 'Some summary'
            description: 'Some description'
            requestBody:
                required: true
                content:
                    application/json:
                        schema:
                            type: object
                            required:
                                - name
                            properties:
                                name:
                                    type: string
                            
            responses:
                '200':
                    description: 'Some description'
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/CustomComponent'
                '401':
                    description: Unauthorized
                '403':
                    description: Forbidden
                '404':
                    description: Resource Not Found
                '422':
                    description: Unprocessable Content
                '500':
                    description: Internal server error

components:
    schemas:
        CustomComponent:
            type: object
            additionalProperties: false
            required:
                - id
                - name
                - status
            properties:
                id:
                    type: integer
                name:
                    type: string
                status:
                    type: string
                    enum: [enabled, disabled, deleted]

nayzo/json-schema-validator-bundle 适用场景与选型建议

nayzo/json-schema-validator-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 02 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 nayzo/json-schema-validator-bundle 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 31
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-17