【问题标题】:Fail to validate schema and correctly use additionalProperties无法验证架构并正确使用附加属性
【发布时间】:2016-03-21 02:14:31
【问题描述】:

我正在尝试验证我的 JSON 架构并使用 additionalProperties: false 来确认没有其他属性。 我的 responseBody 如下所示:

[
  {
    "id": 1234567890987654,
    "email": "eemail@domain.com",
    "civility": 0,
    "firstname": "john",
    "lastname": "do",
    "function": null,
    "phone": null,
    "cellphone": null,
    "role": 1,
    "passwordws": "jdnfjnshn55fff5g8"
  },
  {
...}
]

在邮递员测试中,我添加了这个

var schema = {
    "type": "array",
    "properties": {
        "id": {"type":"number"},
        "email": {"type":"string"},
        "civility": {"type":"number"},
        "firstname": {"type":"string"},
        "lastname": {"type":"string"},
        "function": {"type":"string"},
        "cellphone": {"type":"string"},
        "role": {"type":"number"},
        "passwordws": {"type":"string"},
    },
    "additionalProperties": false,
    "required": ["id", "email", "civility", "role", "passwordws"]
};

var data = JSON.parse(responseBody);
var result = tv4.validateResult(data, schema);
tests["Valid schema"] = result.valid;

测试应该返回失败,因为我从架构中删除了“电话”属性,但测试仍然有效... 我试图将架构更改为 {type:array, properties: {type: object, properties {list of properties}additionalProperties: false}} 但测试仍然返回 PASS 而不是 FAIL...任何想法?

【问题讨论】:

    标签: json schema postman tv4


    【解决方案1】:

    您的响应是一个对象数组,我看到了:

    • 数组的对象没有定义

    • 用“数字”而不是“整数”定义类型 id

    试试这个:

    var schema = {
      "type":"array",
      "items": { "$ref": "#/definitions/MyObject" }
    
      "definitions" : {
            "MyObject" : {
              "type":"object",
                "required" : ["id", "email", "civility", "role", "passwordws"],
                "properties": {
                    "id": {"type":"integer"},
                    "email": {"type":"string"},
                    "civility": {"type":"integer"},
                    "firstname": {"type":"string"},
                    "lastname": {"type":"string"},
                    "function": {"type":"string"},
                    "phone": {"type":"string"},
                    "cellphone": {"type":"string"},
                    "role": {"type":"integer"},
                    "passwordws": {"type":"string"}
                },
               "additionalProperties": false,
            },
        },
    };
    

    【讨论】:

    • 感谢您的回答。目前它不能解决我的问题。架构总是返回失败
    【解决方案2】:

    经过一些测试并记录结果,错误是由于我有时在对象中收到的空值。 我更改了您发送给我的架构

    {
        "type": "array",
        "items": {
            "$ref": "#/definitions/MyObject"
        },
    
        "definitions": {
            "MyObject": {
                "type": "object",
                "required": ["id", "email", "civility", "role", "passwordws"],
                "properties": {
                    "id": {
                        "type": "integer"
                    },
                    "email": {
                        "type": "string"
                    },
                    "civility": {
                        "type": "integer"
                    },
                    "firstname": {
                        "type": ["string", "null"]
                    },
                    "lastname": {
                        "type": ["string", "null"]
                    },
                    "function": {
                        "type": ["string", "null"]
                    },
                    "phone": {
                        "type": ["string", "null"]
                    },
                    "cellphone": {
                        "type": ["string", "null"]
                    },
                    "role": {
                        "type": "integer"
                    },
                    "passwordws": {
                        "type": "string"
                    }
                },
                "additionalProperties": false
            }
        }
    };
    

    我现在能够正确验证架构。

    非常感谢

    【讨论】:

      猜你喜欢
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 2014-07-18
      • 2015-10-27
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多