无法验证架构并正确使用 additionalProperties

Fail to validate schema and correctly use additionalProperties

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

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

测试应该 return 失败,因为我从架构中删除了 "phone" 属性,但测试仍然 运行 有效... 我试图将架构更改为 {type:array, properties: {type: object, properties {list of properties}additionalProperties: false}} 但测试仍然 return PASS 而不是 FAIL ...有什么想法吗?

你的回复是一个对象数组,我看到:

  • 数组对象未定义

  • type id 定义为 "number" 而不是 "integer"

试试这个:

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,
        },
    },
};

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

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

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

非常感谢