gpt4 book ai didi

node.js - Sequelize - 在验证模型关联时获得正确的路径

转载 作者:行者123 更新时间:2023-12-03 22:39:47 25 4
gpt4 key购买 nike

我使用 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 ,我可以在其中看到每个 pathValidationErrorItem 属性。但是,当我在子模型上出现验证错误时,此验证错误的 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."
}
]
}

问题是,不清楚是什么导致了这个错误以及哪个 child 是无效的。当我使用 Mongoose 作为 ORM 时,如果我也这样做,那么 path 属性将等于 questions.0.description 之类的东西,这样就更清楚了,这样您就可以看到哪个 child 无效。

所以,我的问题是:有没有办法在验证子模型的同时设置 path 属性?

最佳答案

显然它还没有出现,我已经在 Sequelize repo 上提交了一个问题,这里是:https://github.com/sequelize/sequelize/issues/9524

关于node.js - Sequelize - 在验证模型关联时获得正确的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50797936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com