Closed lvjiaxuan closed 4 weeks ago
validate
isn't supposed to prevent or replace input.
That being said, it's possible something is wrong when we end up printing the number on the terminal.
I do not think this is related to the type of NaN
, but it might be how the prompt renders the input (I'm not sure anymore if it processes it before printing it out. If that's the case, then it's possible NaN
prevents the type from becoming valid again.)
I got the same error, after fails validation, the cli shows NaN
and I cant delete.
my code:
const inquirer = require("inquirer");
const getTourId = async (inputs = []) => {
const prompts = [
{
type: 'number',
name: 'value',
message: "Tour Id?",
validate: function(value) {
var pass = Number.isInteger(value)
if (pass) {
return true;
}
return 'Please enter a valid tour ID';
}
},
];
const answers = await inquirer.prompt(prompts);
return answers.value;
};
const main = async () => {
const tourId = await getTourId();
const answers = {
tourId,
}
console.log(answers);
};
main();
quick fix, just change the type to "input" and validate
const getTourId = async (inputs = []) => {
const prompts = [
{
type: 'input',
name: 'value',
message: 'Tour Id?',
validate: function(value) {
var pass = !isNaN(value)
if (pass) {
return true
}
return 'Please enter a valid tour ID'
},
},
]
const answers = await inquirer.prompt(prompts)
return answers.value
}
@letanure I've just got the same error and ended up doing the same as you said => Switching from type: 'number'
to type: 'input'
.
However, your answers
ends up being :
{
value: '150'
}
instead of
{
value: 150
}
So it is necessary to cast manually the values to have numbers when the prompt is done. Because I tried filter
with a parseInt()
and ended up with the same NaN
that can't be deleted from the prompt.
I just ran into this problem too. I couldn't delete it, but i could get rid of it via the up
and down
arrows. 👍
As @SBoudrias mentioned, the issue is that after the validation fails, the user input is still NaN therefore will cause the error prompt to show again. NaN input => show error => input is still NaN => show error => repeat.
My fix was to filter the input and return string if input is NaN.
const { qty } = await prompts({
type: 'input',
name: 'qty',
message: 'How many ?',
default: '1',
validate: value => isNaN(parseInt(value)) ? 'Not a number!': true,
filter: value => isNaN(parseInt(value)) ? value : parseInt(value, 10)
});
console.log(qty);
This is a pretty major bug IMHO that will bite any user that types anything other than a number in response. Would be great if this could be addressed since its been open so long. The only solution seems to be to not use the 'number' type.
Whats the tentative timeline for this issue , when will this be picked. The issue is been open for more than 4 years.
A possible approach--> If the type is set as number user should not be allowed to press any other keys. only number inputs should be taken and be printed on the command line interface. If a user presses any other non number key just ignore it don't take it in the input string.
The problem users are facing is that sometimes by mistake user presses some other non number key and presses enter it gives nan
output when we cannot be removed from the cli by pressing backspace key
PR to fix this is welcomed, the code itself is pretty short https://github.com/SBoudrias/Inquirer.js/pull/663
number
as a prompt was removed from the latest version (@inquirer/prompts
) due to the bad UX it has. New version release announcement over here #1214, and we're considering re-adding number
with the modern API over at #1383.
A number prompt is back in @inquirer/prompts
, and inquirer
v10 includes it.
When input an invalid value like
10
or'test'
, I couldn't delete it in the command line.I think it should return as a string type.But I can't pass the ci/pr.