adonisjs/validator

Use a validator from a provider

Closed this issue · 2 comments

Hi, i have a set of base validators in a provider and i want to use them as a route validator.

I add the singleton in the provider like this:

 this.app.singleton('Nuviio/Validators/UserOwnedListValidator', () => {
      return require('./Validators/UserOwnedListValidator')
    })

And i try to call it in a controller like this.

Route.get('/', 'Api/CampaignController.list')
    .middleware([
      'logRequest',
      'auth:jwt',
      'userIsActive',
      'processQueryParams'
    ]).validator(['Nuviio/Validators/UserOwnedListValidator'])

It always gives me a module_not_found error. Please help.

Yup, since it is looking for the validator inside the conventional directory, app/Validators. You can tell the resolver to pull a binding from the IoC container as follows.

validator('@provider:Nuviio/Validators/UserOwnedListValidator')

Closing since answered and no response from issue reporter.