Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using lambda and not classes to validate inputs #171

Open
BptGrm opened this issue Dec 1, 2021 · 1 comment
Open

Using lambda and not classes to validate inputs #171

BptGrm opened this issue Dec 1, 2021 · 1 comment

Comments

@BptGrm
Copy link

BptGrm commented Dec 1, 2021

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 !

@guedesfelipe
Copy link

#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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants