NodeJS JSON 模式验证不工作
NodeJS JSON Schema Validation not working
我是 JSON Schema Validator 的新手,但我认为它非常强大。但是,我无法验证一个 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" 属性 不存在,但验证 returns 为真!而且,对于 "new_width" 我将值设置为 null... 并再次通过验证,所以我不知道我做错了什么。
它似乎工作正常。控制台正确记录错误。这是我的 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"]
,则有效负载中需要所有三个属性,但您的有效负载中似乎缺少 url
。
如果你希望 url
是可选的,那么,不要把它放在 required 约束中。
如果是这种情况,验证器还会返回错误 message.It returns missing parameters/properties。
我是 JSON Schema Validator 的新手,但我认为它非常强大。但是,我无法验证一个 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" 属性 不存在,但验证 returns 为真!而且,对于 "new_width" 我将值设置为 null... 并再次通过验证,所以我不知道我做错了什么。
它似乎工作正常。控制台正确记录错误。这是我的 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"]
,则有效负载中需要所有三个属性,但您的有效负载中似乎缺少 url
。
如果你希望 url
是可选的,那么,不要把它放在 required 约束中。
如果是这种情况,验证器还会返回错误 message.It returns missing parameters/properties。