gpt4 book ai didi

node.js - Sequelize 自定义验证是否可能出现多个验证错误?

转载 作者:太空宇宙 更新时间:2023-11-03 22:50:29 26 4
gpt4 key购买 nike

  deliverableSchedule: {
type: DataTypes.STRING(64),
validate: {
isIn: {
args: ['onetime', 'recurrent'],
msg: 'Must be one of "onetime", "recurrent"'
},
isRecurrent(value) {
if (value === 'recurrent' && !this.recurrenceInterval) {
throw new Error('recurrenceInterval must be set if deliverableSchedule is "recurrent"')
}

if (value === 'recurrent' && !(this.recurrenceCount > 0)) {
throw new Error('recurrenceCount must be greater than 0')
}
}
}
},

这是我的模型定义。问题是,如果 deliverableSchedulerecurrent 并且缺少 recurrenceIntervalrecurrenceCount

最佳答案

您可以创建自定义错误

class CustomError extends Error {

constructor(message, errors) {

super(message);

this._errors = errors

}

get errors() {

return this._errors;

}

}

module.exports = CustomError;

然后

 var CustomError= require('./CustomError')


deliverableSchedule: {
type: DataTypes.STRING(64),
validate: {
isIn: {
args: ['onetime', 'recurrent'],
msg: 'Must be one of "onetime", "recurrent"'
},
isRecurrent(value) {
var errroList = [];
if (value === 'recurrent' && !this.recurrenceInterval) {
errroList.push('recurrenceInterval must be set if deliverableSchedule is "recurrent"')
}

if (value === 'recurrent' && !(this.recurrenceCount > 0)) {
errroList.push('recurrenceCount must be greater than 0')
}

if (errroList.length > 0) {
throw new CustomError('Is recurrent error. See errors prop for error list', errroList);
}
}
}
},

关于node.js - Sequelize 自定义验证是否可能出现多个验证错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47838465/

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