Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[fix]Vars from prompt's override are now being validated

  • Loading branch information...
commit f99f12e2ca4fd4b32c3c789ad31363aa6626ffc5 1 parent 363cf22
@pkrefta pkrefta authored
Showing with 121 additions and 33 deletions.
  1. +52 −0 examples/override-validation.js
  2. +69 −33 lib/prompt.js
View
52 examples/override-validation.js
@@ -0,0 +1,52 @@
+/*
+ * override-validation.js: Example of using prompt with complex properties and command-line input.
+ *
+ * (C) 2010, Nodejitsu Inc.
+ *
+ */
+
+var prompt = require('../lib/prompt'),
+ optimist = require('optimist');
+
+var schema = {
+ properties: {
+ name: {
+ pattern: /^[a-zA-Z\s-]+$/,
+ message: 'Name must be only letters, spaces, or dashes',
+ required: true
+ },
+ email: {
+ name: 'email',
+ format: 'email',
+ message: 'Must be a valid email address'
+ }
+ }
+};
+
+//
+// Set the overrides
+//
+prompt.override = optimist.argv
+
+//
+// Start the prompt
+//
+prompt.start();
+
+//
+// Get two properties from the user: email, password
+//
+prompt.get(schema, function (err, result) {
+ //
+ // Log the results.
+ //
+ console.log('Command-line input received:');
+ console.log(' name: ' + result.name);
+ console.log(' email: ' + result.email);
+});
+
+// try running
+// $ node ./override-validation.js --name USER --email EMAIL
+// You will only be asked for email becasue it's invalid
+// $ node ./override-validation.js --name h$acker --email me@example.com
+// You will only be asked for email becasue it's invalid
View
102 lib/prompt.js
@@ -141,8 +141,8 @@ prompt.history = function (search) {
};
//
-// ### function get (msg, callback)
-// #### @msg {Array|Object|string} Set of variables to get input for.
+// ### function get (schema, callback)
+// #### @schema {Array|Object|string} Set of variables to get input for.
// #### @callback {function} Continuation to pass control to when complete.
// Gets input from the user via stdin for the specified message(s) `msg`.
//
@@ -189,7 +189,7 @@ prompt.get = function (schema, callback) {
// of the forms:
//
// 'prop-name'
- //
+ //
// ['string-name', { path: ['or-well-formed-subschema'], properties: ... }]
//
// { path: ['or-well-formed-subschema'], properties: ... ] }
@@ -198,7 +198,7 @@ prompt.get = function (schema, callback) {
//
// And transforms them all into
//
- // { path: ['path', 'to', 'property'], properties: { path: { to: ...} } }
+ // { path: ['path', 'to', 'property'], properties: { path: { to: ...} } }
//
function iterate(schema, get, done) {
var iterator = [],
@@ -372,16 +372,8 @@ prompt.getInput = function (prop, callback) {
msg;
//
- // Handle overrides here.
- // TODO: Make overrides nestable
- //
- if (prompt.override && prompt.override[propName]) {
- return callback(null, prompt.override[propName]);
- }
-
- //
// If there is a stored schema for `propName` in `propmpt.properties`
- // then use it.
+ // then use it.
//
if (schema instanceof Object && !Object.keys(schema).length &&
typeof storedSchema !== 'undefined') {
@@ -395,7 +387,7 @@ prompt.getInput = function (prop, callback) {
if (typeof prop === 'string' && !storedSchema) {
schema = {};
}
-
+
schema = convert(schema);
defaultLine = schema.default;
name = prop.description || schema.description || propName;
@@ -409,7 +401,7 @@ prompt.getInput = function (prop, callback) {
};
//
- // If the schema has no `properties` value then set
+ // If the schema has no `properties` value then set
// it to an object containing the current schema
// for `propName`.
//
@@ -420,7 +412,22 @@ prompt.getInput = function (prop, callback) {
return obj;
})();
}
-
+
+ //
+ // Handle overrides here.
+ // TODO: Make overrides nestable
+ //
+ if (prompt.override && prompt.override[propName]) {
+
+ var isValid = prompt._performValidation(name, prop, prompt.override, schema, -1, callback);
+
+ if(isValid){
+ return callback(null, prompt.override[propName]);
+ }else{
+ delete prompt.override[propName];
+ }
+ }
+
//
// Calculate the raw length and colorize the prompt
//
@@ -454,7 +461,7 @@ prompt.getInput = function (prop, callback) {
}
var against = {},
- valid;
+ isValid;
if (typeof line !== 'string') {
line = '';
@@ -464,23 +471,10 @@ prompt.getInput = function (prop, callback) {
against[propName] = line;
}
- // Validate.
- try { valid = validate(against, schema) }
- catch (err) { return callback(err) }
+ // Validate
+ isValid = prompt._performValidation(name, prop, against, schema, line, callback);
- if (!valid.valid) {
- if (prompt.colors) {
- logger.error('Invalid input for ' + name.grey);
- }
- else {
- logger.error('Invalid input for ' + name);
- }
-
- if (prop.schema.message) {
- logger.error(prop.schema.message);
- }
-
- prompt.emit('invalid', prop, line);
+ if(!isValid){
return prompt.getInput(prop, callback);
}
@@ -496,6 +490,48 @@ prompt.getInput = function (prop, callback) {
};
//
+// ### function performValidation (name, prop, against, schema, line, callback)
+// #### @name {Object} Variable name
+// #### @prop {Object|string} Variable to get input for.
+// #### @against {Object} Input
+// #### @schema {Object} Validation schema
+// #### @line {String|Boolean} Input line
+// #### @callback {function} Continuation to pass control to when complete.
+// Perfoms user input validation, print errors if needed and returns value according to validation
+//
+prompt._performValidation = function(name, prop, against, schema, line, callback){
+
+ var valid, msg;
+
+ try {
+ valid = validate(against, schema);
+ }
+ catch (err) {
+ return (line !== -1) ? callback(err) : false;
+ }
+
+ if (!valid.valid) {
+
+ msg = line !== -1 ? 'Invalid input for ' : 'Invalid command-line input for ';
+
+ if (prompt.colors) {
+ logger.error(msg + name.grey);
+ }
+ else {
+ logger.error(msg + name);
+ }
+
+ if (prop.schema.message) {
+ logger.error(prop.schema.message);
+ }
+
+ prompt.emit('invalid', prop, line);
+ }
+
+ return valid.valid;
+};
+
+//
// ### function addProperties (obj, properties, callback)
// #### @obj {Object} Object to add properties to
// #### @properties {Array} List of properties to get values for
Please sign in to comment.
Something went wrong with that request. Please try again.