thosakwe / feathers-validator

WIP. A validator for Feathers services, or any service.
https://www.npmjs.com/package/feathers-validator
GNU General Public License v2.0
11 stars 4 forks source link

unique custom validator #9

Open neerajincaendo opened 7 years ago

neerajincaendo commented 7 years ago

Hello, i am making unique validator function. But encountered a problem , the response of query is coming after return can you help me with this.

function generateUniqueValidatorFunction(params, data){ "use strict"; return function (key, value){ var query = {}; var res; query[params[1]] = data[params[1]]; hook.app.service(hook.path).find({query: query }).then(function(found){ console.log('found '+ JSON.stringify(found)); if(found.data.length > 0){ return { valid: true }; }else{ return { valid: false, error: 'The '+ key + ' must be unique.' }; }

    });    
}

}

Please Help!!!!!

thosakwe commented 7 years ago

Validators run synchronously, whereas service methods run asynchronously. You'll need to implement this additional validation logic as a hook.

If you're willing to submit a PR to allow async validators, that would be nice, but I don't really plan on touching this library every again. I haven't used Feathers in a long time.