gpt4 book ai didi

validation - Sequelize : how to prevent execution of following validators if the current one is failed

转载 作者:行者123 更新时间:2023-12-03 22:20:27 26 4
gpt4 key购买 nike

我在模型中有一个字段,其中包含一些验证器:

...
someField: {
...
validate: {
someValidator1: true,
someValidator2: true,
}
},
...

Sequelize 中的两个验证器都是独立执行的。

如果第一个失败,如何 不执行 第二个?

最佳答案

一旦您抛出错误,脚本执行就会停止。

http://docs.sequelizejs.com/en/latest/docs/models-definition/ ,Sequelize.js 的作者给出了这个例子:

var ValidateMe = sequelize.define('foo', {
foo: {
type: Sequelize.STRING,
validate: {
// custom validations are also possible:
isEven: function(value) {
if(parseInt(value) % 2 != 0) {
// This error will stop code execution and give you the chance to catch and handle the error
throw new Error('Only even values are allowed!')
// we also are in the model's context here, so this.otherField
// would get the value of otherField if it existed
}
}
}
}
});

关于validation - Sequelize : how to prevent execution of following validators if the current one is failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37947542/

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