Skip to content

Commit

Permalink
Add validation feature to 'input' and 'rawlist'
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed May 19, 2013
1 parent 6d58125 commit 1672f22
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
10 changes: 9 additions & 1 deletion examples/pizza.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ var questions = [
{
type: "input",
name: "phone",
message: "What's your phone number?"
message: "What's your phone number?",
validate: function(value) {
var pass = value.match(/^([01]{1})?[\-\.\s]?\(?(\d{3})\)?[\-\.\s]?(\d{3})[\-\.\s]?(\d{4})\s?((?:#|ext\.?\s?|x\.?\s?){1}(?:\d+)?)?$/i);
if (pass) {
return true;
} else {
return "Please enter a valid phone number";
}
}
},
{
type: "list",
Expand Down
37 changes: 27 additions & 10 deletions lib/prompts/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,7 @@ module.exports = Prompt;
*/

function Prompt(question, rl) {
_.assign(this, {
// Defaults
filter: function(value) {
return value;
},
validate: function(value) {
return true;
},
height: 0
}, question);
_.assign(this, { height : 0 }, question);

if (_.isArray(this.choices)) {
this.choices = utils.normalizeChoices(this.choices);
Expand Down Expand Up @@ -64,3 +55,29 @@ Prompt.prototype.clean = function(extra) {
charm.left(300).foreground("white");
return this;
};

/**
* Write error message
* @param {String} Error Error message
* @return {Prompt} Self
*/

Prompt.prototype.error = function(error) {
charm.erase("line");
charm.foreground("red").write(">> ").foreground("white")
.write(error || "Please enter a valid value");
charm.up(1);
return this;
};

/**
* "noop" default functions
*/

Prompt.prototype.validate = function(value) {
return true;
};

Prompt.prototype.filter = function(value) {
return value;
};
14 changes: 10 additions & 4 deletions lib/prompts/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@ Prompt.prototype.run = function(cb) {
var self = this;

// Once user confirm (enter key)
this.rl.once("line", function(input) {
this.rl.on("line", function(input) {
var value = input || self.default || "";
self.clean(1).render();
charm.foreground("cyan").write(value).foreground("white");
cb(value);
var valid = self.validate(value);
if (valid === true) {
self.clean(1).render();
charm.foreground("cyan").write(value).foreground("white");
self.rl.removeAllListeners("line");
cb(value);
} else {
self.error(valid).clean().render();
}
});

// Init
Expand Down
2 changes: 1 addition & 1 deletion lib/prompts/rawlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Prompt.prototype.run = function(cb) {
cb(self.choices[input - 1].value);
return;
}
self.clean(1).render();
self.error("Please enter a valid index").clean().render();
});

// Init the prompt
Expand Down

0 comments on commit 1672f22

Please sign in to comment.