【发布时间】:2016-11-04 00:48:39
【问题描述】:
在创建新记录时,我正在尝试验证电子邮件在 Bookshelf.js 中是否已存在验证。
我在这里找到了一个解决方案 github,但它不起作用,即使我尝试使用 Promise
User = bookshelf.Model.extend({
tableName: 'users',
initialize: function() {
this.on('saving', this._assertEmailUnique);
},
_assertEmailUnique: function(model, attributes, options) {
if (this.hasChanged('email')) {
return this
.query('where', 'email', this.get('email'))
.fetch(_.pick(options, 'transacting'))
.then(function (existing) {
if (!existing) throw new Error('duplicate email');
});
}
}
});
对于当前使用Joi 的模型验证,看起来 Joi 也不支持对此进行自定义验证。我正在使用 Postgres 数据库。还有其他方法可以做到这一点..请帮助...
提前谢谢..
【问题讨论】:
标签: node.js validation express bookshelf.js joi