dthree / vorpal

Node's framework for interactive CLIs
http://vorpal.js.org
MIT License
5.64k stars 280 forks source link

Prompt validation does not visualize the output (errors) #69

Open sanderhouttekier opened 8 years ago

sanderhouttekier commented 8 years ago

using inquirer a prompt with validate function should show the validate output, but with vorpal it is somehow omitted.

this.prompt([
    {
        type: 'input',
        name: 'test',
        message: 'Please type something different than `test` to see an error pop up... or type `test` to continue',
        validate: function (val) {
            if (val != 'test') {
                return "Please answer with the word test";
            }
            return true;
        }
    }
], function(answers) {
    console.log(answers);
});

in Inquirer you can see this work perfectly in the examples/input.js example. however the error gets lost somewhere in the process within vorpal I assume.

jackyjieliu commented 8 years ago

throw the error instead of returning it

validate: function (val) {
  if (val != 'test') {
      throw "Please answer with the word test";
  }
}
sanderhouttekier commented 8 years ago

@jackyjieliu will try, however I doubt that's going to be the issue, I'm positive that it is vorpal who surpresses the errors, since inquirer.js (used by vorpal) tells us to do it by returning an errorMessage.

see the third input in their examples. https://github.com/SBoudrias/Inquirer.js/blob/master/examples/input.js

// ...
{
    type: "input",
    name: "phone",
    message: "What's your phone number",
    validate: function( value ) {
      var pass = value.match(/^([01]{1})?[\-\.\s]?\(?(\d{3})\)?[\-\.\s]?(\d{3})[\-\.\s]?(\d{4})\s?((?:#|ext\.?\s?|x\.?\s?){1}(?:\d+)?)?$/i);
      if (pass) {
        return true;
      } else {
        return "Please enter a valid phone number";
      }
    }
  }
// ...
dthree commented 8 years ago

Got it. Will check out as soon as I can!

sarcadass commented 8 years ago

I've got the same problem here,

scotthovestadt commented 8 years ago

This is a tough one since Vorpal has completely taken over rendering, so it'll probably also have to take over looking for the validate parameter, calling it, and rendering the output.

dthree commented 8 years ago

Yes, that's correct.

marcos-abreu commented 7 years ago

I've just created a PR that hopefully fix the issue. I've tested in one project I'm working and it fixes the issue on the 3 instances I'm validating.