tgriesser / checkit

simple, flexible validations for node and the browser
MIT License
223 stars 53 forks source link

Unique Name Validation using multiple columns #90

Open vaidehi-imedia opened 7 years ago

vaidehi-imedia commented 7 years ago

I'm using bookshelf.js instead of knex and I want to check for unique using name and id attribute of the table.

How can I pass req.params in my custom function that checks for unique name in table?

Below is my code:

function assertEditRoleName(val, params, context) {
        return Role
            .forge()
            .where('id', '<>', 2)
            .where('name', '=', val)
            .where('is_deleted', '=', 0)
            .fetchAll()
            .then(function (existing) {
                if (existing.models.length > 0) {
                    throw new Error('Duplicated role name');
                }
            }).catch(function (err) {
                console.log(err);
            });
    }

In above code, i'm getting only name in val and I want to pass id also from my request body.

I have checked #26 Thanks