Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 是否可能只对猫鼬使用JsonSchema?_Node.js_Mongodb_Validation_Express_Jsonschema - Fatal编程技术网

Node.js 是否可能只对猫鼬使用JsonSchema?

Node.js 是否可能只对猫鼬使用JsonSchema?,node.js,mongodb,validation,express,jsonschema,Node.js,Mongodb,Validation,Express,Jsonschema,我有一个使用express、mongodb的api,我使用AJV验证来验证传入的请求 //JSONSchema var recordJsonSchema = { type: "object", properties: { name: { type: "string" }, idNumber: { type: "number" }, content: { type: "string" } }, requi

我有一个使用express、mongodb的api,我使用AJV验证来验证传入的请求

//JSONSchema
var recordJsonSchema = {
     type: "object",
     properties: {
         name: { type: "string" },
         idNumber: { type: "number" },
         content: { type: "string" }
     },
     required: ['name','idNumber']
}
我会使用这个JSON模式来验证传入的请求,就像这样

app.post('/record', (req,res) => {
   let errors = ajv.inspect(req.body, recordJsonSchema)
   return errors ? res.send(errors) : res.send(this.handler(req));
})
这工作很好,速度也很快。我也喜欢JsonSchema,因为它遵循OpenAPI标准

不幸的是,为了通过mongoose读/写mongo,我还需要创建一个MongoSchema来记录。它们非常相似,但在处理必填字段等方面有点不同

var recordSchema = new Schema({
   name: { type: "string", required: true },
   idNumber: { type: "number", required: true },
   content: { type: "string" }
})
所以对于我的记录模型,我现在有两个模式。一个用于JSONschema,另一个用于处理Mongo读/写


我正在寻找一种切断MongoSchema的方法,有什么建议吗?

可能是这样,它似乎从条目中导入了您的ajv模式,并将其放置在mongoose模式中

我也面临同样的问题。我认为在新的Mongo4.4中,我们可以直接将ajv模式加载到mongodb

感谢您的建议,但这仍然需要编写两个模式。