Giter Site home page Giter Site logo

Add validate property about prompts HOT 23 CLOSED

terkelg avatar terkelg commented on July 22, 2024 6
Add validate property

from prompts.

Comments (23)

terkelg avatar terkelg commented on July 22, 2024 3

onSubmit is different from what @datitisev is asking for. I still want and need the filter myself. The feature is still being considered.

from prompts.

terkelg avatar terkelg commented on July 22, 2024 2

I'll take a look at this for a version v0.2. Feel free to add a PR

from prompts.

lukeed avatar lukeed commented on July 22, 2024 2

@yoavmmn That's essentially what my example is doing 😆 I agree, I think it's sufficient.

In order to display an error message, you could attach an onError key to each prompt, then change mine to:

function onSubmit(obj, val) {
  obj.isValid(val) ? defs.shift() : obj.onError();
}

Or handle all of that inside the isCustom function:

function isCustom(val) {
  if (val > 100) return true;
  console.log('ERROR MESSAGE');
  //=> returns undefined --> falsey
}

from prompts.

terkelg avatar terkelg commented on July 22, 2024 2

validate option is added in v1.0.0 branch for Number and Text prompt.

const response = await prompts({
    type: 'number',
    name: 'value',
    message: 'How old are you?',
    validate: value => value < 18 ? `Nightclub is 18+ only` : true
});

Please give it a test run if you like @datitisev @lukeed @marcghorayeb

from prompts.

terkelg avatar terkelg commented on July 22, 2024 2

onCancel is not preventing you from canceling a single prompt, it controls whether the prompt chain should cancel or continue. If you have an array of prompts you'll see it either continue or break the chain based on the onCancel return value.

from prompts.

terkelg avatar terkelg commented on July 22, 2024 1

It's super important for me to keep the package really tight - without too many features or dependencies. What would the use case be for this?

I normally just do multiple prompts() and do validation after each.

That being said I've thinking about doing a message and split prompt.

from prompts.

yoavmmn avatar yoavmmn commented on July 22, 2024 1

@lukeed I have no idea how I missed your comment. Maybe because it's midnight already over here 😅

from prompts.

marcghorayeb avatar marcghorayeb commented on July 22, 2024 1

Validate property would be nice yep 👍

from prompts.

dsevillamartin avatar dsevillamartin commented on July 22, 2024 1

@olstenlarck I haven't coded anything with prompts yet, and as this issue is still open, I thought maybe the end result was similar to validation (after reading all the comments), but not exactly.
Maybe I misread them, apologies if I did.
I'll see if I can implement it when I have the time.

from prompts.

simonepri avatar simonepri commented on July 22, 2024

The main difference of doing the validation after the prompt is that you can't show live errors to users.

An example from inquirer:
image

Also it allows user to not have complicate logic to handle response rejection.

from prompts.

lukeed avatar lukeed commented on July 22, 2024

Personally, I'd call this a maybe. As you've both mentioned, this requires an entirely new error-like module that might go beyond the scope of @terkelg's intentions.

A quick (and still manageable) way around this is to process prompts one at a time & then "validate" them on the onSubmit hook. You could choose to exit the prompt with an error (stdout.write('Reason'); return true) or display the same prompts definition, with an updated initial or message key.

function isCustom(val) {
  // return true if I consider valid
}

let defs = [
  {
    type: 'text',
    name: 'foobar',
    isValid: isCustom
  }, { 
    // ... etc
  }
];

function onSubmit(obj, val) {
  obj.isValid(val) && defs.shift(); // if valid, remove item
}

async function run() {
  if (!defs.length) return 'done';
  return await prompts(defs[0], { onSubmit }).then(run);
}

await run();

Not sure if the above will work 😆 Quick psuedo'ing

from prompts.

simonepri avatar simonepri commented on July 22, 2024

It just feel too complicated to handle this manually for each prompt.
Also because the error is supposed to disappear after a valid input is provided.

from prompts.

yoavmmn avatar yoavmmn commented on July 22, 2024

Wait, whats wrong with options.onSubmit? It's built right into the API.
I think the only drawback is that you can't force the user to enter the expected input.

from prompts.

yoavmmn avatar yoavmmn commented on July 22, 2024

Here's a little demonstration of the capabilities of options.onSubmit.

const prompts = require('prompts');

function isInputHiOrHey(greeting = '') {
  return greeting === 'hi' || greeting === 'hey';
}

// a recursive function that checks user's input just for the prompt named greeting
function checkInput(prompt, answer) {
  if (prompt.name === 'greeting') {
    if (!isInputHiOrHey(answer)) {
      console.log('Please enter a valid greeting.');
      prompts(prompt, { onSubmit: checkInput })
    }
  }
}

prompts({
  type: 'text',
  name: 'greeting',
  message: 'What\'s your greeting?'
}, {
  onSubmit: checkInput
});

from prompts.

tunnckoCore avatar tunnckoCore commented on July 22, 2024

Just realized that the whole thing is that we need prop like onSubmit option but per question object. Then we can do anything from validate to required and etc.

from prompts.

terkelg avatar terkelg commented on July 22, 2024

After #43 all prompts have a onSubmit and onAbort. It's in master but not yet released or documented. I'm still open for implementing some nice visuals for this later.

Focus right now is to get v0.1.5 out the door.

from prompts.

dsevillamartin avatar dsevillamartin commented on July 22, 2024

Sorry for the bump.

Is this feature still being considered?
Or at least, some method to make a custom implementation of it easier?

I would like to replace inquirer with this great module 🙂

from prompts.

tunnckoCore avatar tunnckoCore commented on July 22, 2024

@datitisev it was while ago.. Can't you use onSubmit to implement it?

from prompts.

tunnckoCore avatar tunnckoCore commented on July 22, 2024

I didn't tried onSubmit still either, but.. In glance looks it seems it would work.

from prompts.

dsevillamartin avatar dsevillamartin commented on July 22, 2024

@terkelg Works great! 🙂 Thanks!

Edit: Not sure if it is a result from this change, but when the onCancel function returns true, it still exists.
image
image

from prompts.

terkelg avatar terkelg commented on July 22, 2024

@datitisev is that a single prompt or a prompt chain?

from prompts.

dsevillamartin avatar dsevillamartin commented on July 22, 2024

@terkelg A single prompt.

  let onCancel = prompt => {
    console.log('Never stop prompting!');
    return true;
  }

  const response = await prompts({
      type: 'text',
      name: 'email',
      message: 'What\'s yar email?',
  }, { onCancel });

  console.log(response);

from prompts.

terkelg avatar terkelg commented on July 22, 2024

Coming in #80

from prompts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.