问题描述
我正在尝试验证我的JSON模式并使用extraProperties:false来确认没有其他属性。
我的响应正文如下:
I'm trying to validate my JSON schema and use the additionalProperties: false to confirm there is not other properties.My responseBody look like this:
[
{
"id": 1234567890987654,
"email": "[email protected]",
"civility": 0,
"firstname": "john",
"lastname": "do",
"function": null,
"phone": null,
"cellphone": null,
"role": 1,
"passwordws": "jdnfjnshn55fff5g8"
},
{
...}
]
在邮递员测试中,我将其添加
In the postman tests, I add this
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;
测试应返回失败,因为我从架构中删除了 phone属性,但测试仍然有效...
我试图将架构更改为{type:array,properties:{type:object,properties {属性列表} additionalProperties:false}},但是测试仍然返回PASS而不是FAIL ...任何想法?
the test should return FAIL because I deleted the "phone" properties from schema, but test still run valid...I tried to change the schema to {type:array, properties: {type: object, properties {list of properties}additionalProperties: false}} but test still return PASS instead of FAIL... any idea ?
推荐答案
经过一些测试,并记录结果,该错误是由于有时在对象中收到的空值所致。
我更改了您发送给我的架构
After some test, and log the result, the error was due to the null value I received sometimes in objects.I changed the schema you sent me
{
"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
}
}
};
现在我可以正确地验证架构了。
and I'm now able to validate correctly the schema.
非常感谢
这篇关于无法验证架构并正确使用附加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!