Node.js Sequelize-在验证模型关联时获取正确的路径
我正在使用Sequelize作为我项目的ORM。我有这样的结构:Node.js Sequelize-在验证模型关联时获取正确的路径,node.js,postgresql,validation,orm,sequelize.js,Node.js,Postgresql,Validation,Orm,Sequelize.js,我正在使用Sequelize作为我项目的ORM。我有这样的结构: const Event = sequelize.define('event', { // fields defined }); const Question = sequelize.define('question', { description: { type: Sequelize.STRING, allowNull: false, defaultValue: '
const Event = sequelize.define('event', {
// fields defined
});
const Question = sequelize.define('question', {
description: {
type: Sequelize.STRING,
allowNull: false,
defaultValue: '',
validate: {
notEmpty: { msg: 'Description should be set.' }
},
},
// other fields defined
});
Event.hasMany(Question);
Question.belongsTo(Event);
然后我创建了一个事件模型的实例,带有associate,如下所示:
const body = {
questions: [
{ description: '' } // is obviously invalid
],
// some other fields
}
const newEvent = await Event.create(body, {
include: [ Question ]
});
如果事件实例本身存在验证错误,它将返回SequelizeValidationError
,在那里我可以看到每个validationerritem
的path
属性。但是,当我在子模型上出现验证错误时,此验证错误的路径
属性不清楚:
{
"message": "Description should be set.",
"type": "Validation error",
"path": "description",
"value": "",
"origin": "FUNCTION",
"instance": {
"required": true,
"id": null,
"description": "",
"event_id": 60,
"updated_at": "2018-06-11T12:25:04.666Z",
"created_at": "2018-06-11T12:25:04.666Z"
},
"validatorKey": "notEmpty",
"validatorName": "notEmpty",
"validatorArgs": [
{
"msg": "Description should be set."
}
],
"__raw": {
"validatorName": "notEmpty",
"validatorArgs": [
{
"msg": "Description should be set."
}
]
}
问题是,不清楚是什么导致了这个错误,哪个孩子是无效的。当我将Mongoose用作ORM时,如果我也这样做,path
属性将等于类似于questions.0.description
,这更清楚,这样您可以看到哪个子级无效
因此,我的问题是:在验证子模型时,是否有方法设置
路径
属性?显然它还没有出现,我已经在Sequelize回购上提出了一个问题,如下所示: