无法验证架构并正确使用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;

测试应该返回FAIL,因为我从架构中删除了"phone"属性,但测试仍然有效。。。我试图将架构更改为{type:array,properties:{type:object,properties{list of properties}additionalProperties:false}},但测试仍然返回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,
        },
    },
};

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

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

现在我可以正确地验证模式了。

非常感谢

相关内容

  • 没有找到相关文章

最新更新