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

define question demand (not optional)

Open shaunthegeek opened this issue 3 years ago • 2 comments

now every questions are optional, there is no way to define it demand.

suggestion: add demandOption config, if user select nothing or just press enter, exit error or auto load question to retry.

options = {
  things: {
    message: 'What do you want to do?',
    type: 'checkbox',
    choices: ['Order a pizza', 'Make a reservation', 'Bug rate', 'Security'],
    demandOption: true,
  }
};

shaunthegeek avatar Mar 14 '22 07:03 shaunthegeek

Hi, I'm not super clear on your ask.

Do you want to make sure the user select at least one option within the options? If so, I think you can do that safely with a custom validate function.

validate: answers => answers.length > 1 || 'Please select at least one option'

SBoudrias avatar Mar 14 '22 20:03 SBoudrias

@SBoudrias yes, you are right, if this code can evolve to attribute, it will be better, just like js onblur vs html required:

<input type="text" onblur="alter('please input')" />

to

<input type="text" required />

https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/required

shaunthegeek avatar Mar 15 '22 01:03 shaunthegeek