我尝试验证我的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因为我删除了"手机"来自架构的属性,但测试仍然有效... 我试图将模式更改为{type:array,properties:{type:object,properties {list of properties} additionalProperties:false}}但是test仍然返回PASS而不是FAIL ...任何想法?
答案 0 :(得分: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,
},
},
};
答案 1 :(得分: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
}
}
};
我现在能够正确验证架构。
非常感谢