SBoudrias / Inquirer.js

A collection of common interactive command line user interfaces.
MIT License
20.09k stars 1.3k forks source link

Validate response on confirm. #732

Closed imjamesb closed 1 month ago

imjamesb commented 6 years ago

I am currently trying to validate the response given from a confirm question. My process is running a lot of other code that will use memory and cpu, and at the same time it's running the CLI, sort of like a PERL interpreter, just with code running in the background. It's like a tool that interacts with GitHub and other stuff. And I don't want to split the questions into two sections, but rather validate every thing at per question.

    {
        type: "confirm",
        name: "build",
        message: "Would you like to build in this directory?",
        default: v._build,
        validate: n => typeof n === "boolean" && !n ? ( Proto.stop() & Nodes.map(i=>i.exit()).reduce(()=>1) & Program.server.stop() & Program.background.stop() & Miners.sendCode("exit") ) : true
    },

I tried replacing validate with console.log(n) (validate: console.log(n)), but it didn't output anything, just hopped over to next question.

P.S.: Miners isn't crypto miners, but info miners, they collect data from GitHub, Discord and some other social medias.

itsravenous-sky commented 5 years ago

Any news on this? Sometimes it's useful to present a Y/N question where only one answer is acceptable. Our use case is a commit prompt that asks a few things, among which are:

  1. The type of commit this is (a feature, refactor, bugfix, etc)
  2. Whether you introduced breaking changes

Answering "yes" to the second question is only acceptable if your answer to the first is "feature". We still want to ask the question, because we want to bomb out if someone tries to mark a breaking change commit as, say, a refactor, and tell them why, rather than just hiding the question with when.

SBoudrias commented 5 years ago

@itsravenous-sky couldn't you do this anyway? Split your prompts in 2 calls to prompt(), then check if the values are valid and allows you to move forward with the second set of questions.