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

Feature request: Add a validation before filter is applied

Open melinerunen opened this issue 7 years ago • 4 comments

Hi I want to validate an input before the filter is applied. For example for this prompt I'm asking for a subdomain of a full url and then using the filter to convert into a full url before passing it to the program:

{
      type: 'input',
      message: 'Enter subdomain:',
      name: 'subdomain',

      validate: function (answer) {
        var match = answer.match(/^[0-9a-zA-Z\-]+$/);
        return (match) ? true : 'Please enter a valid subdomain (letters, numbers and slashes)';
      },
      filter: function (answer) {
        return 'https://' + answer + '.some-domain.com';
      }
    }

The problem is that inside of validate the answer is already formatted by the filter and I can't validate the subdomain itself.

As a workarround I'm adding, to the regex, the strings that i'm adding in the filter but this is not elegant 😬

A proposal will be to add a event called "beforeFilter" or "validateBeforeFilter" or something like that and add it to the nested call:

image

Regards,

David

melinerunen avatar Oct 11 '17 01:10 melinerunen

I've the same problem.

validate should run before filter, transformer, when.

@SBoudrias what do you think?

pldg avatar Jun 09 '19 15:06 pldg

Hey @SBoudrias I also would like to see this.
The docs don't mention the order in which the functions are called, so you have to try it out to notice that format is called before validate.

Here's the problem with the current situation (first format, then validate):

  • usually, you need to validate the raw user input, e.g. by checking if it can be parsed by some other function
    example: user inputs a time string and you want to validate if ms can parse it into milliseconds
  • at the end, you want your answer to be the time in milliseconds, so you want to format (parse) it before passing it to your program
  • you don't want the user to see the formatted input inside the CLI (that's what the transformer function is for, after all)
    example: if the user inputs a negative value (e.g. -3s), which is invalid for my use case, I want to show some feedback (Invalid time string!), but don't replace the current CLI text with the formatted value (e.g. -3000), but instead keep the -3s so that the user can edit it. (there are also some problems with editing in the former case, probably related to https://github.com/SBoudrias/Inquirer.js/issues/866)

So, three take-aways:

  1. The documentation should mention the order in which functions are called/applied
  2. Please change the order in which format and validate are applied, because it doesn't make too much sense right now.
    This would be a breaking change, so I'd understand if you don't want to do this for now (maybe in a future release though)
  3. If the order should stay the same, please add an additional function like the proposed validateBeforeFilter, so that this issue can be mostly resolved :)

I'd be willing to provide a PR for this, however it would likely require less time if you did it instead :)

Have a great day! :D

Chaphasilor avatar May 23 '21 13:05 Chaphasilor

Plus one! I had to find this out the hard way and it would be great to have a way to validate before filter is applied

TheLimifiedLime avatar Aug 02 '21 01:08 TheLimifiedLime

Any info about this issue?

pahar0 avatar Sep 17 '22 03:09 pahar0