3

我正在尝试验证我的 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 ......知道吗?

4

2 回答 2

3

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

  • 数组的对象未定义

  • 类型 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,
        },
    },
};
于 2015-12-18T16:33:10.910 回答
1

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

{
    "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
        }
    }
};

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

非常感谢

于 2016-01-04T15:39:42.773 回答