NodeJS JSON模式validation不起作用

我是JSON Schema Validator的完全新手,但我认为它非常强大。 但是,我只是无法validation一个JSON。

这是我的模式

{ title: "Example Schema", type: "object", properties: { original_image:{ type: "object", properties: { temp_id: {type: "string"}, url: {type: "string"}, scale:{ type: "object", properties:{ new_width: {type: "number"}, new_height: {type: "number"} }, required:["new_width","new_height"] } }, required:["url","temp_id","scale"] } }, required:["image"] } 

这是实际的JSON:

 { "original_image": { "temp_id": "this is my id", "scale": { "new_width": null, "new_height": 329 } } } 

所以你可以看到来自“original_image”的“url”属性不存在,但validation返回true! 而且,对于“new_width”,我将该值设置为null …并再次通过validation,所以我不知道我在做什么错误。

它似乎工作正常。 控制台正确logging错误。 这是我的index.js

 var Validator = require('jsonschema').Validator; var v = new Validator(); var instance = { "original_image": { "temp_id": "this is my id", "scale": { "new_width": null, "new_height": 329 } } }; var schema = { title: "Example Schema", type: "object", properties: { original_image:{ type: "object", properties: { temp_id: {type: "string"}, url: {type: "string"}, scale:{ type: "object", properties:{ new_width: {type: "number"}, new_height: {type: "number"} }, required:["new_width","new_height"] } }, required:["url","temp_id","scale"] } }, required:["image"] }; console.log(v.validate(instance, schema)); 

如果您按照required:["url","temp_id","scale"]放置条件required:["url","temp_id","scale"] ,那么在有效负载中需要所有这三个属性,但url似乎在有效负载中不存在。 如果你想要的url是可选的,那么,不要把它放在所需的约束。 validation器还返回错误消息。如果是这种情况,它将返回缺less的参数/属性。