- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 jsonschema-master 来验证使用 Express 通过 POST 请求输入的 json 请求。请参阅下面的代码和示例。
如果属性标签丢失或拼写错误(例如“model”、“areas”、“id”),它会拾取,但如果这些属性的值符合规范,则不会拾取。例如,“model”属性被定义为“premium”或“basic”的枚举类型,但我似乎可以将任何旧字符串放在那里,并且无论如何它都会继续,坐标也被定义为类型号,但它再次忽略这一点,然后错误会通过验证器并进一步导致问题。不确定我错过了什么。
node.js 代码:
var Validator = require('jsonschema-master').Validator;
var v = new Validator();
var bodySchema = {
"model": {
"enum": [ "premium","basic" ]
},
"areas": {
"type":"array",
"items": {
"id": {"type": "string"},
"geometry": {
"type": { "type":"string"},
"coordinates": {
"type":"array",
"items": {
"type":"array",
"items": [
{"type":"number"},
{"type":"number"},
{"type":"number"}
]
}
},
"required" : ["type","coordinates"]
},
"required" : ["id","geometry"]
}
},
"required" : ["model","areas"]
};
var valResult = v.validate(doc.request, bodySchema);
if (valResult.errors.length) {
// Validation failed.
// All processing will now stop.
console.log('Request invalid: '+ doc._id +" - "+valResult.errors);
}
正确的 JSON 请求示例(在 doc.request 中)
{
"model": "premium",
"areas": [
{
"id": "1234",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
453600.0,
181100.0,
0
],
[
453600.0,
181200.0,
0
],
[
453700.0,
181200.0,
0
],
[
453700.0,
181100.0,
0
],
[
453600.0,
181100.0,
0
]
]
]
}
}
]
}
最佳答案
谢谢大家,但我最终还是解决了。以下是该架构的更正语法:
var bodySchema = {
"type" : "object",
"properties": {
"model": {
"enum": [ "premium","basic" ]
},
"areas": {
"type":"array",
"items": {
"type" : "object",
"properties": {
"id": {"type": "string"},
"geometry": {
"type" : "object",
"properties": {
"type": { "type":"string"},
"coordinates": {
"type":"array",
"items": {
"type":"array",
"items": {
"type":"array",
"items": [
{"type":"number"},
{"type":"number"},
{"type":"number"}
]
}
}
}
},
"required" : ["type","coordinates"]
}
},
"required" : ["id","geometry"]
}
}
},
"required" : ["model","areas"]
};
关于javascript - jsonschema-master 验证 json 请求未按预期响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48184370/
我有两个参数,比如 number_1和 number_2 . number_2的值应该大于 number_1 的值.这如何在 jsonschema 中完成? 最佳答案 这不能用普通的 jsonsche
我有正确验证从返回单个文章的端点返回的文章的代码。我很确定它可以正常工作,因为当我故意不在文章中包含必填字段时,它会给出验证错误。 我还有这段代码,它尝试验证从返回文章数组的端点返回的文章数组。但是,
使用 Python 时 jsonschema可以定义无法用有效 JSON 表达的模式和实例。 >>> import jsonschema >>> schema = { ... "type": "o
我刚学 jsonschema .我不清楚 items 之间的区别是什么和 properties .有人可以解释一下吗? 最佳答案 items用于验证数组,properties用于验证对象。 items
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 10 个月前关闭。 Improve
件JSON Schema很容易与 Angular、React 和 Alpaca 等前端 JavaScript 库一起使用,以创建带验证的 html 表单。这也适用于 Python 解决方案 djang
这是一个使用 propertyNames 的 JSON 模式: { "$schema": "http://json-schema.org/draft-07/schema#", "typ
假设我有一个具有四个可能属性的对象:a、b、c、d。 a 和 b 只能一起出现(即,当且仅当 b 出现时 a 出现)。如果a和b出现,c就不能出现(即a/b和c是互斥的)。如果 a 和 b 没有出现,
我正在使用以下架构来验证我的 json: { "$schema": "http://json-schema.org/schema#", "title": " Rules", "
我希望将 json 模式的公共(public)部分捕获到一个文件中,然后从主模式文件中引用这个文件。所以基本上不是 1 个大的 json 模式文件,而是多个相互引用的文件。我正在使用 json-sch
它是有效的json模式吗? object: $ref: '#/definitions/object' 您会建议使用这种格式吗? 最佳答案 自我引用是允许的并且有用的。但是,您的示例看起来就
我有一个 json 模式,它描述了一个相当复杂的 API 查询语法。一些属性是模式匹配的,但还需要接受除模式之外的其他值(即其他显式字符串)。我似乎无法在众多 json 模式站点中找到任何此类示例。
我有一个 JSON 响应,我正在尝试为其创建 JSONSchema { "gauges": { "foo": { "value": 1234
我正在尝试学习 json 模式,但有些事情对我来说并不奏效。我正在尝试运行来自 http://json-schema.org/understanding-json-schema/reference/c
在下面的架构中,我需要 items_list , price和 variance作为必需的键。条件是 price和 variance可能为空,也可能不为空,但都不能为空。 虽然我能够实现它,但我很期待
对于以下 json 字符串: { "abc" : 123, "def" : 345 } 以下架构认为它有效: { "$schema": "http://json-schema.
在 jsonSchema 中,您可以使用“required”属性指示定义的字段是否是必填的: { "$schema": "http://json-schema.org/draft-04/sch
我想使用 JSON Schema 并允许新类型(目前有字符串数字数组对象 boolean 和 null) 例如我想添加一个联系人类型 在 JSON 中甚至可能吗?有什么建议吗? 谢谢 最佳答案 如果您
给定一个名为 location 的架构的一部分,如下所示: { type: 'object', required: [ 'country' ], additionalProperties:
我想使用 JSON Schema 并允许新类型(目前有字符串数字数组对象 boolean 和 null) 例如我想添加一个联系人类型 在 JSON 中甚至可能吗?有什么建议吗? 谢谢 最佳答案 如果您
我是一名优秀的程序员,十分优秀!