我是 Sequelize 的新手,之前主要使用 Yii2 ActiveRecord 作为 ORM。
关于 Sequelize 验证,目前我只收到以下格式的错误:-
"errors": [
{
"message": "notNull Violation: modelname.model_attribute cannot be null,",
}
]
Sequelize 是否有任何内在方法以以下格式返回错误?
"errors": [
{
"modelname":
{
"model_attribute": ["cannot be null"]
}
}
]
我会发现通过 API 调用处理表单错误等会更加理智。
谢谢。
最佳答案
关于相同的格式,我不确定,但对于自定义消息在模型中添加验证功能,如下所示:
battery_name: {
type: DataTypes.STRING(66),
allowNull: false,
validate: {
len: {
args: [0, 60],
msg: 'Some custom message'
},
notEmpty: {
msg: 'Another custom message'
}
}
}
更多信息在这里:https://sequelize.readthedocs.io/en/v3/docs/models-definition/#validations
此外,您可以像这样添加 catch 来对错误进行分组:
SequelizeModel.create({}).then(() => { /* ... */ }).catch(Sequelize.ValidationError, e => {
/* create logic to group/format/change default sequelize error */
})
关于validation - Sequelize 验证 - 是否可以按属性/字段进行 Sequelize 组错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50937380/