-
-
Notifications
You must be signed in to change notification settings - Fork 304
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
Add validate property #2
Comments
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 That being said I've thinking about doing a |
Personally, I'd call this a maybe. As you've both mentioned, this requires an entirely new A quick (and still manageable) way around this is to process 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();
|
It just feel too complicated to handle this manually for each prompt. |
I'll take a look at this for a version v0.2. Feel free to add a PR |
Wait, whats wrong with |
Here's a little demonstration of the capabilities of 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
}); |
@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 function onSubmit(obj, val) {
obj.isValid(val) ? defs.shift() : obj.onError();
} Or handle all of that inside the function isCustom(val) {
if (val > 100) return true;
console.log('ERROR MESSAGE');
//=> returns undefined --> falsey
} |
@lukeed I have no idea how I missed your comment. Maybe because it's midnight already over here 😅 |
Just realized that the whole thing is that we need prop like |
Validate property would be nice yep 👍 |
Sorry for the bump. Is this feature still being considered? I would like to replace |
@datitisev it was while ago.. Can't you use |
@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. |
I didn't tried |
|
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 |
@terkelg Works great! 🙂 Thanks! Edit: Not sure if it is a result from this change, but when the |
@datitisev is that a single prompt or a prompt chain? |
@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); |
|
Coming in #80 |
Proposal to add a
validate
property:Should return true if the value is valid, and an error message (String) otherwise.
If false is returned, a default error message is provided.
To do this we need to add a way to render errors messages.
API Usage Example:
The text was updated successfully, but these errors were encountered: