带有allOf和其他属性的全面数据验证

时间:2019-03-09 08:05:29

标签: swagger ajv

需要帮助,以使用allOf和AdditionalProperty解决错误的定义模式: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对象(名称,电话)的其他属性,则在验证地址架构时,验证会在(名称和电话)上发现错误

如何解决我的架构定义

1 个答案:

答案 0 :(得分: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/