Inquirer.js icon indicating copy to clipboard operation
Inquirer.js copied to clipboard

Validate doesn't return a default error message

Open pensierinmusica opened this issue 5 years ago • 3 comments

Hi,

The docs say that:

validate: (Function) Receive the user input and answers hash. Should return true if the value is valid, and an error message (String) otherwise. If false is returned, a default error message is provided.

But when my validate function returns false, no default error message is shown on screen.

If the validate function returns a string instead, the custom error message is shown correctly.

pensierinmusica avatar Feb 27 '20 12:02 pensierinmusica

Hey @pensierinmusica, which prompt are you using when this happens?

SBoudrias avatar Mar 22 '20 04:03 SBoudrias

Good question! I don't remember, sorry this was a few weeks ago and there's no commit that reproduces the issue. Maybe someone can quickly iterate through the different prompt types and test if they show inconsistent behaviors?

pensierinmusica avatar Apr 14 '20 18:04 pensierinmusica

Hi,

I also faced this issue where no default error message is shown on the console.

In my case, I am using the type: input, whereby I prompt the user to enter a URL.

When the validate function return false, the interactive command line would look seemingly stuck due to no default error message is shown. The gif below shows that when I enter an invalid URL https://www.sitemaps.or (Sorry it's not so obvious! I waited for 3 seconds), there is no default error message. Upon entering a valid URL, my app is able to continue to use Apify to go through the pages.

ValidateURL

Here's my code snippet for the prompt

{
    type: 'input',
    name: 'url',
    message: answers => {
      if (answers.isLogin) {
        answers.scanner = scannerTypes.login;
      }

      return getUrlMessage(answers.scanner);
    },
    async validate(url, answers) {
      const checkIfExit = url.toLowerCase();

      if (checkIfExit === 'exit') {
        process.exit(1);
      }

      const res = await checkUrl(answers.scanner, url);

      if (res.status === 200) {
        answers.url = res.url;
        return true;
      }

      return false;
    },
},

Do point me to some examples if any available, thank you! Appreciate your help and time!

bernicecpz avatar Jun 06 '21 14:06 bernicecpz