gpt4 book ai didi

node.js - NodeJS JSON 架构验证不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 21:56:18 25 4
gpt4 key购买 nike

我是 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”属性不存在,但验证返回 true!而且,对于“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));

关于node.js - NodeJS JSON 架构验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39843566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com