tmbo / questionary

Python library to build pretty command line user prompts ✨Easy to use multi-select lists, confirmations, free text prompts ...
MIT License
1.52k stars 87 forks source link

Feature Request: Validation #7

Closed AlbertEmil closed 3 years ago

AlbertEmil commented 5 years ago

I understand questionary as a growing up Python-equivalent of inquirerer.js. Since the landscape of available CLI modules seems to be somewhat scattered around unresolved dependency updates (e.g. like with whaaaaat, pyinquirer ...), I pretty much like what questionary aims at. However, one of the features I currently miss in general is input validation since I would like to prevent the user from proceeding to the next question if the input of the last question is invalid.

tmbo commented 5 years ago

So you men the concept of when (only raising a question given a certain condition is met)?

https://github.com/CITGuru/PyInquirer/blob/master/examples/pizza.py#L104

AlbertEmil commented 5 years ago

I mean something like the validate option of inquirer.js:

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.

Further execution is prevented as long as no valid input is provided. For my current project, the NodeJs snippet looks like this (currently porting the codebase from NodeJS to Python):

    {
      type:     'checkbox',
      message:  'Which serial ports should be detected?',
      name:     'ports',
      pageSize: 6,
      choices:  portInfo.map(port => port.comName),
      validate: (selection) => (selection.length < 1) ? ERROR_MSG_SELECT_PORTS : true,
    }

Giving the following output if no port is selected (which is invalid):

$ node app.js 
? Which serial ports should be detected? (Press <space> to select, <a> to toggle all, <i> to invert selection)
❯◯ /dev/tty.Bluetooth-Incoming-Port
 ◯ /dev/tty.Bluetooth-Modem
>> Please choose at least one serial port.

In the meantime I had a deeper look at the examples and found a similar validation approach in text.py which raises a ValidationError and seems to be pretty much what I am looking for. However, I need to somehow apply this validation to a checkbox-type question...

cyberbeast commented 5 years ago

I had a similar concern about this too. Currently, I get around this by manually processing the input and repeating the question infinite times until the correct input conditions are met. It would certainly be an improvement in "developer/user experience" if the control logic built into the question type would not allow the next question being asked if the "answer conditions" are not satisfied based on a lambda or a well defined function.

woile commented 5 years ago

Agreed this feature would be really useful! If I have time in the coming weeks I'll take a look

SonGokussj4 commented 4 years ago

Hi. This is the only thing missing for me and the reason I'm still using PyInquirer.

I'm using these validations for input type often:

Is there an ETA something like this can be integrated?

Zylvian commented 4 years ago

So there is a 'validate' field, at least in the build I'm using, not sure if that is what you're looking for.

'validate': lambda val: re.compile('#\d').match(val) is not None,

kiancross commented 4 years ago

I've submitted a PR to add validation to the checkbox: https://github.com/tmbo/questionary/pull/48.