title: Bookshelf.js unique email validation before create new record
body: <p>Am trying to validate Email already exists validation in <a href="http://bookshelfjs.org/" rel="nofollow">Bookshelf.js</a> when creating new record.</p>
  
  <p>I found one solution here <a href="https://github.com/tgriesser/bookshelf/issues/812" rel="nofollow">github</a> but its not working, even i tried with <a href="http://bluebirdjs.com/docs/getting-started.html" rel="nofollow">Promise</a></p>
  
  <pre><code>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');
          });
      }
    }
  });
  </code></pre>
  
  <p>For Model validation currently am using <a href="https://github.com/hapijs/joi" rel="nofollow">Joi</a>, 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...</p>
  
  <p>Thanks in advance.. </p>