-
Notifications
You must be signed in to change notification settings - Fork 571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unique email validation before create new record #1313
Comments
Hi @raj-optisol 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) {
// Currently it throw an error if 'email' is existing
// existing = new bookshelf.Model()
// console.log(!existing) // true
if (!existing) throw new Error('duplicate email');
});
}
}
}); So it should be... if (existing) throw new Error('duplicate email'); |
After removing this 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()
.then(function (existing) {
// Currently it throw an error if 'email' is existing
// existing = new bookshelf.Model()
// console.log(!existing) // true
if (existing) throw new Error('duplicate email');
});
}
}
}); and also changed query like this I checked with existing email, its not finding anything in the DB.. returning like its new always.. |
Fixed: Querying on User = bookshelf.Model.extend({
tableName: 'users',
initialize: function() {
this.on('saving', this._assertEmailUnique);
},
_assertEmailUnique: function(model, attributes, options) {
if (this.hasChanged('email')) {
return User
.query('where', 'email', this.get('email'))
.fetch()
.then(function(existing) {
if (existing) {
throw new Error('Duplicated email: User id #' + existing.id);
}
});
}
}
}); |
Just put a unique constraint on the column... On Wed, Jul 6, 2016, 8:51 PM raj-optisol notifications@github.com wrote:
|
@rhys-vdw If i add unique constraint on the column, it won't insert and throw error, How can i catch and show/parse that error ? |
@raj-optisol There is now way at the moment how to distinguish errors thrown by knex. knex/knex#522 |
Here's what I'm using. Start with a unique constraint. // migration file
knex.table("table_name", t => {
t.unique("unique_column_name")
t.unique("compound_index", "part_two")
}) Then count duplicates on each create or save, respecting transactions, and filtering out the current model from the count. function assertUnique(/* columns */) {
const columns = _.toArray(arguments)
if (columns.length === 0) {
throw new Error("please pass unique columns")
}
return function(model, attributes, options) {
const hasChanged = _.some(columns, column => {
return model.hasChanged(column)
})
if (model.isNew() || hasChanged) {
return this.constructor.query(q => {
columns.forEach(column => {
q.where(column, '=', model.get(column))
})
if (!model.isNew()) {
q.where(model.idAttribute, '<>', model.id)
}
})
.count({ transacting: options.transacting })
.then(n => {
if (n > 0) {
return bluebird.reject({
name: "DuplicateError",
message: columns.join(", ")
})
}
})
}
}
} Then run the assertion for any model with unique constraints. // users
this.on("saving", assertUnique("email"))
// posts
this.on("saving", assertUnique("md5_hash"))
this.on("saving", assertUnique("user_id", "title")) It should work for most use cases. |
Am trying to validate Email already exists validation in Bookshelf.js when creating new record.
I found one solution here github but its not working, even i tried with Promise
For Model validation currently am using Joi, looks like Joi also not supporting for custom validation for this. Am using Postgres Database. There is any other way to do it.. Please help...
The text was updated successfully, but these errors were encountered: