I am trying to build a MEAN project, so I need to validate some of my model's dynamic key...
我正在尝试构建一个MEAN项目,所以我需要验证一些模型的动态密钥......
I want to create a Schema like this
我想创建一个像这样的Schema
var exampleSchema = new Schema({
x: {
type: String,
default: '',
required: true,
trim: true
},
y: {}
});
as you see I have mixed type object, but actually it is a Language Map and it should be something like this,
如你所见我有混合类型对象,但实际上它是一个语言地图,它应该是这样的,
{
"en-US": "answered"
}
can I validate my key with mongoose? (I think it has no function like that)
我可以用猫鼬验证我的密钥吗? (我认为它没有这样的功能)
if no, how and where can I validate it (in model or controller)?
如果不是,我如何以及在何处验证它(在模型或控制器中)?
1 个解决方案
#1
1
You may want to look into this: http://mongoosejs.com/docs/middleware.html
您可能需要查看此内容:http://mongoosejs.com/docs/middleware.html
Specifically pre-save events. Mongoose gives you control over this and you can perform validation, mapping as needed before the actual model gets saved.
特别是预先保存的事件。 Mongoose让您可以控制它,您可以在保存实际模型之前根据需要执行验证和映射。
Also works nice for pre-init event if you need defaults such as "current date" for an audit trail such as "createdOn: date".
如果您需要诸如“createdOn:date”之类的审计跟踪的“当前日期”之类的默认值,也适用于pre-init事件。
#1
1
You may want to look into this: http://mongoosejs.com/docs/middleware.html
您可能需要查看此内容:http://mongoosejs.com/docs/middleware.html
Specifically pre-save events. Mongoose gives you control over this and you can perform validation, mapping as needed before the actual model gets saved.
特别是预先保存的事件。 Mongoose让您可以控制它,您可以在保存实际模型之前根据需要执行验证和映射。
Also works nice for pre-init event if you need defaults such as "current date" for an audit trail such as "createdOn: date".
如果您需要诸如“createdOn:date”之类的审计跟踪的“当前日期”之类的默认值,也适用于pre-init事件。