我正在使用Sequelize作为我项目的ORM。我有这个结构:
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);
然后我创建了一个Event模型的实例,带有关联,就像那样:
const body = {
questions: [
{ description: '' } // is obviously invalid
],
// some other fields
}
const newEvent = await Event.create(body, {
include: [ Question ]
});
如果我对Event实例本身有验证错误,则返回SequelizeValidationError
,我可以在其中看到每个path
的{{1}}属性。但是,当我在子模型上遇到验证错误时,此验证错误的ValidationErrorItem
属性不明确:
path
问题是,目前还不清楚导致此错误的原因以及哪个孩子无效。当我使用Mongoose作为ORM时,如果我这样做,{
"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."
}
]
}
属性将等同于path
,这样就更清楚了,这样你就可以看到孩子无效。
所以,我的问题是:有没有办法在验证子模型时设置questions.0.description
属性?
答案 0 :(得分:0)
显然它还没有出现,我在Sequelize回购中提出了一个问题,这里是:https://github.com/sequelize/sequelize/issues/9524