[Resolved] Help. Where is the error? | Custom Validator does not throws a message

My custom validator doesn’t throws message. Its objective is validate a youtube url.

My custom validator code

'use strict'

const { ServiceProvider } = require('@adonisjs/fold')

class YoutubeValidationProvider extends ServiceProvider {

  _youtubeUrl(data, field, message, args, get) {
    const value = get(data, field)

    if (!value) {
      return
    }

    const rgx = /^(https?\:\/\/)?((www\.)?youtube\.com|youtu\.?be)\/.+$/;

    if (!rgx.test(value)) {
      console.log('Should throws message');

      throw message || 'Não é um link válido do Youtube'
    } else {
      console.log('fine');

    }

  }


  boot() {
    const Validator = use('Validator');
    // Validator.extend('youtubeUrl', this._youtubeUrl);
    Validator.extend('youtubeUrl', this._youtubeUrl.bind(this));
  }
}

module.exports = YoutubeValidationProvider;

It is a provider and is registered in start/app.js, like this:

image

Tests

When I send this:

image

Prints Should throws message
image

When i send this:
image

Prints this:
image

It is printing the correct console.log, so it is runing. But is not throwing the message and the route is being loaded.

Where am i wrong?

Missing async on method _youtubeUrl.

I added async and it works. This information should be on docs.

2 Likes