【问题标题】:Swagger data validation with allOf and additional properties使用 allOf 和其他属性进行 Swagger 数据验证
【发布时间】:2019-07-31 04:45:00
【问题描述】:

需要帮助来解决使用 allOf 和 additionalProperties 的大张旗鼓的定义模式:false

这是我的 JSON 架构

"deliveryContact": {
      "allOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "name": {
              "type": "string",
              "maxLength": 1024
            },
            "phone": {
              "type": "string",
              "maxLength": 24
            }
          }
        },
        {
          "$ref": "#/definitions/address"
        }
      ]
    },
    "address": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "address": {
          "type": "string",
          "maxLength": 1024
        },
        "postalCode": {
          "type": "string",
          "maxLength": 12
        },
        "city": {
          "type": "string",
          "maxLength": 512
        },
        "state": {
          "type": "string",
          "maxLength": 512
        }
      }
    },

样本数据

                   delivery: {
                        address: 'my address',
                        postalCode: 'my postalCode',
                        city: 'my city',
                        state: 'my state',
                        name: 'my name',
                        phone: 'my phone'
                    },

我使用 AJV 6.10.0 来验证我的数据,但我认为我的架构定义有误。 使用 Ajv 选项:

        ajv = require('ajv')({
            allErrors: true,
            verbose: true,
            removeAdditional: false,
        });

实际上,我有 6 个错误警告每个属性的附加属性

在验证 allOf(姓名和电话)中的第一个对象期间,验证在(地址、邮政编码、城市和州)上发现了错误

如果我删除了第一个 allOf 对象(姓名、电话)的附加属性,则在验证地址架构期间,验证发现(姓名和电话)上的错误

如何解决我的架构定义

【问题讨论】:

    标签: swagger ajv


    【解决方案1】:

    我设法让它工作了,我已经更新了你的数据结构,使其更合乎逻辑,见下文:

    JSON 架构

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "type": "object",
      "required": [
        "delivery"
      ],
      "properties": {
        "delivery": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "name",
            "phone",
            "address"
          ],
          "properties": {
            "name": {
              "type": "string",
              "maxLength": 1024
            },
            "phone": {
              "type": "string",
              "maxLength": 24
            },
            "address": {
              "$ref": "#/definitions/address"
            }
          }
        }
      },
      "definitions": {
        "address": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "address": {
              "type": "string",
              "maxLength": 1024
            },
            "postalCode": {
              "type": "string",
              "maxLength": 12
            },
            "city": {
              "type": "string",
              "maxLength": 512
            },
            "state": {
              "type": "string",
              "maxLength": 512
            }
          }
        }
      }
    }
    

    样本数据

    {
      "delivery": {
        "address": {
          "address": "myaddress",
          "postalCode": "mypostalCode",
          "city": "mycity",
          "state": "mystate"
        },
        "name": "myname",
        "phone": "myphone"
      }
    }
    

    如果你想测试它,你可以在这里:https://www.jsonschemavalidator.net/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-01
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      • 2014-08-01
      相关资源
      最近更新 更多