vei*_*ich 6 schema jsonschema mongoose node.js
有没有办法采用像下面这样的有效 JSON 模式并将其转换为猫鼬模式?
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "some desc",
"title": "Product",
"type": "object",
"properties": {
"endpoints": {
"type": "array",
"items": {
"type": "string"
}
},
"poi": {
"type": "array",
"items": {
"type": "object",
"properties": {
"location_name": {
"type": "string"
},
"distance": {
"type": "string"
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎非常基本和简单,但我没有在网上找到任何东西。
有很多关于如何获取 JSON 模式的示例,还有很多如何从这样的对象创建 mongoose 模式的示例:
const newSchema = new mongoose.Schema({ name: String });
如果我尝试直接放置 JSON 模式,则会出现错误
node_modules/mongoose/lib/schema.js:674
throw new TypeError('Undefined type `' + name + '` at `' + path +
^
TypeError: Undefined type `Http://json-schema.org/draft-04/schema#` at `$schema`
Did you try nesting Schemas? You can only nest using refs or arrays.
Run Code Online (Sandbox Code Playgroud)
但是我在网上找不到任何从一种类型到另一种类型的转移。
以前有人遇到过这个问题吗?
编辑:
这个问题在概念上是不正确的。
基本上你所做的是在将数据保存到数据库之前针对数据验证 JSON 模式。您可以使用jsonschema来自 npm 或其他方式执行此操作。
所以数据验证步骤与保存到数据库步骤没有直接联系。
我以为您可以将 JSON 模式应用于 MongoDB 模式,但事实并非如此。(特别是当你有很深的嵌套对象时——那就是一团糟)
我一直在调查这个问题。由于您node在问题上添加了标签,我找到了这些npm存储库:
到目前为止,两者都在工作。
他们都几岁了。前者(TypeScript)有更新的提交。我最终可能会更喜欢后者。
| 归档时间: |
|
| 查看次数: |
7288 次 |
| 最近记录: |