JSON 架构对象验证问题(它在内部如何工作)?
Issue with JSON Schema object validation (How does it work internally)?
背景:
我在这里模拟了我的问题以供参考:
https://jsonschemalint.com/#/version/draft-06/markup/json?gist=4c185903d7aeb13f6977852a09cf5462
我正在使用这个 npm 包:https://www.npmjs.com/package/jsonschema
代码
//i read in JSON specified in files (the contents of which are below) and parse them into JSON objects. This process works fine.
var jsonDef = JSON.parse(schemaFile); //i store this jsonDef in my database as an object
var jsonObj = JSON.parse(objFile);
var jsonv = new JSONValidator();
var validateResult = jsonv.validate(jsonObj, jsonDef); //jsonDef is read from my database
//validateResult.valid is true
问题:
我有一个像这样的通用模式 + 元数据定义("props"
包含我要验证的实际对象模式)
架构文件:
{
"name":"schoolDemo",
"displayName":"School Demo",
"propertiesKey":"assetId",
"props":{
"assetId": {
"type": "string"
},
"grade": {
"type": "number"
}
}
}
obj文件:
{
"assetId": "75255972",
"grade": "A"
}
但是,当我尝试针对以下用户输入对象进行验证时,它成功了。 它不应该失败因为:
(1)初始元数据+模式定义中没有"properties"
元素?根据此处显示的示例,似乎需要此字段:https://www.npmjs.com/package/jsonschema
(2) grade
的 type
不是 number
我做错了什么?
验证通过,因为架构格式不正确("properties" 缺失)。
试试这个,然后验证将失败:
{
"name": "schoolDemo",
"displayName": "School Demo",
"propertiesKey": "assetId",
"properties": {
"assetId": {
"type": "string"
},
"grade": {
"type": "number"
}
}
}
背景: 我在这里模拟了我的问题以供参考: https://jsonschemalint.com/#/version/draft-06/markup/json?gist=4c185903d7aeb13f6977852a09cf5462
我正在使用这个 npm 包:https://www.npmjs.com/package/jsonschema
代码
//i read in JSON specified in files (the contents of which are below) and parse them into JSON objects. This process works fine.
var jsonDef = JSON.parse(schemaFile); //i store this jsonDef in my database as an object
var jsonObj = JSON.parse(objFile);
var jsonv = new JSONValidator();
var validateResult = jsonv.validate(jsonObj, jsonDef); //jsonDef is read from my database
//validateResult.valid is true
问题:
我有一个像这样的通用模式 + 元数据定义("props"
包含我要验证的实际对象模式)
架构文件:
{
"name":"schoolDemo",
"displayName":"School Demo",
"propertiesKey":"assetId",
"props":{
"assetId": {
"type": "string"
},
"grade": {
"type": "number"
}
}
}
obj文件:
{
"assetId": "75255972",
"grade": "A"
}
但是,当我尝试针对以下用户输入对象进行验证时,它成功了。 它不应该失败因为:
(1)初始元数据+模式定义中没有"properties"
元素?根据此处显示的示例,似乎需要此字段:https://www.npmjs.com/package/jsonschema
(2) grade
的 type
不是 number
我做错了什么?
验证通过,因为架构格式不正确("properties" 缺失)。
试试这个,然后验证将失败:
{
"name": "schoolDemo",
"displayName": "School Demo",
"propertiesKey": "assetId",
"properties": {
"assetId": {
"type": "string"
},
"grade": {
"type": "number"
}
}
}