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

validate can get a Promise #302

Open
rluvaton opened this issue Mar 13, 2021 · 2 comments
Open

validate can get a Promise #302

rluvaton opened this issue Mar 13, 2021 · 2 comments

Comments

@rluvaton
Copy link
Contributor

Is your feature request related to a problem?

I can't use validate that returns a Promise.

Use cases which this would be helpful:

  • Check if directory/file exists
  • Read from DB
  • Do some async request

Describe the solution you'd like

I would like to be able to pass a function that returns Promise and when the promise fails or returns a string it will indicate an invalid value.

Describe alternatives you've considered

deasync - Anti pattern

Additional context

Implementation suggestions:

  1. Check if the return value is Promise by checking if .then exist
  2. Transform any value to Promise and handle it everywhere like Promise:
// Option 1.1:
function option1_1() {
   return Promise.resolve(validator(args)) 
}

// Option 1.2:
function option1_2() {
   const validatorRes = validator(args);

   if(validatorRes.then) {
      return validator;
   }

   if(validatorRes === true) {
      return Promise.resolve(validatorRes);
   }

   return Promise.reject(validatorRes);
}


// Option 2.1:

function option2_1() {
  return new Promise(resolve => {
      // If the validator function throws it will reject the promise
      resolve(validator(args));
   });
}

// Option 2.2:
function option2_2() {
   return new Promise(resolve => {
      // If the validator function throws it will reject the promise
      resolve(validator(args));
   }).then(res => {
      if(res === true) {
         return true;
      }
      throw new Error(res);
   });
}
@rluvaton
Copy link
Contributor Author

It looks like it can get a promise, just need to update the docs to say that.

@DenyVeyten
Copy link

DenyVeyten commented Oct 28, 2023

It has an issue for Windows described in #402

Also, the issue might be resolved by this PR #313, but it looks like this repo has not been maintained for 2 years.

But here is another issue for async validation:

if (!skipValidation && question.validate && question.validate(answer) !== true) {

There should be await question.validate(answer)

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