PyInquirer icon indicating copy to clipboard operation
PyInquirer copied to clipboard

Using lambda and not classes to validate inputs

Open b-grimaud opened this issue 4 years ago • 1 comments

Hi,

I was wondering if and how is it possible to use lamda functions as validator for data input. In my opinion writing classes derived from another module's class, with its own object type, and that in the end only contain one single function which executes a simple test, seems a bit heavy and convoluted.

It apparently is possible to do so, but as far as I could tell there are two provided examples using lambdas as validators (editor and checkbox), and neither of them work (see #161 and #70).

So, is it actually possible to use lambdas, and if so, how ?

Thanks !

b-grimaud avatar Dec 01 '21 16:12 b-grimaud

#168 This pull request solves the checkbox validation issue

you can use this repo (https://github.com/guedesfelipe/PyInquirer) or make the changes on your machine, as the repository maintainer is no longer maintaining the project anymore #159

Example:

questions = [
    {
        'type': 'checkbox',
        'qmark': '📄',
        'message': 'Select Files',
        'name': 'csv_files',
        'choices': get_files(),
        'validate': lambda answers: 'You must select more than one file to join' \
            if len(answers) < 2 else True
    },
    {
        'type': 'input',
        'qmark': '✏️ ',
        'name': 'output_name',
        'message': 'Output file name',
        'validate': lambda answers: 'You need to put some name' \
            if len(answers.strip()) == 0 else True
    }
]

answers = prompt(questions)

guedesfelipe avatar Dec 01 '21 19:12 guedesfelipe