zepgram / module-json-schema

针对 Magento 2 的 JSON Schema 验证

安装数: 13,649

依赖项: 1

建议者: 0

安全性: 0

星标: 1

关注者: 1

分支: 0

开放问题: 0

类型:magento2-module

0.1.0 2022-04-01 21:59 UTC

This package is auto-updated.

Last update: 2024-08-29 05:38:06 UTC


README

这是一个 Magento 实现,用于验证 JSON 结构与给定的 Schema 是否一致,支持 Draft-3 或 Draft-4 版本的 Schema。
基于https://github.com/justinrainbow/json-schema json schema。
有关 json schema 的更多信息: https://json-schema.fullstack.org.cn/

安装

composer require zepgram/module-json-schema
bin/magento module:enable Zepgram_JsonSchema
bin/magento setup:upgrade

指南

  1. 在您的模块中添加格式化的 json-schema,在 /etc/schema 目录下实现 webservice。
  2. 创建一个类型为 Zepgram\JsonSchema\Model\Validator 的虚拟类,并调整 fileNamemoduleName 参数。

示例

/etc/di.xml

<virtualType name="HelloWorldValidator" type="Zepgram\JsonSchema\Model\Validator">
    <arguments>
        <argument name="fileName" xsi:type="string">schema/hello-world-service.json</argument>
        <argument name="moduleName" xsi:type="string">Zepgram_HelloWorld</argument>
    </arguments>
</virtualType>

/etc/schema/hello-world-service.json

{
  "type":"array",
  "items":{
    "type":"object",
    "required":[
      "helloId",
      "helloContact"
    ],
    "properties":{
      "helloId":{
        "type":"string",
        "description":"Hello Id"
      },
      "helloContact":{
        "type":"array",
        "items":{
          "type":"object",
          "required":[
            "age",
            "fullName"
          ],
          "properties":{
            "age":{
              "type":"integer",
              "format":"int32",
              "description":"contact age",
              "minimum":-2147483648,
              "maximum":2147483647
            },
            "fullName":{
              "type":"object",
              "required":[
                "firstname",
                "lastname"
              ],
              "properties":{
                "firstname":{
                  "type":"string",
                  "description":"contact firstname"
                }, 
                "lastname":{
                  "type":"string",
                  "description":"contact lastname"
                }
              }
            }
          }
        }
      }
    }
  },
  "$schema":"https://json-schema.fullstack.org.cn/draft-04/schema#"
}

Open API V3

如果您的架构使用 open API-V3 来描述 web service 使用,您可以将其转换为 json schema。

  1. 安装 node 工具: https://github.com/mikunn/openapi2schema
  2. 将 open api yaml 文件转换为 JSON
    openapi2schema -i my-custom-service.yaml | python -m json.tool | jq '."%end_point%".post.body' > my-custom-service.json
    其中 %endpoint% 必须替换为在 yaml 文件中描述的 API 端点(例如:/v1/customEndPoint)

问题

如果在安装或使用过程中遇到问题,请在此 github 仓库中报告。