From 4238385be48dd1eabcebf6ecbf88265db0dcdb38 Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Wed, 10 Jun 2015 17:17:11 -0700 Subject: [PATCH 1/2] Extract out Robot param validation into new file --- lib/robot.js | 38 ++++---------------------------------- lib/validator.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 34 deletions(-) create mode 100644 lib/validator.js diff --git a/lib/robot.js b/lib/robot.js index 28a08c2..222ec2b 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -14,6 +14,8 @@ var initializer = require("./initializer"), Config = require("./config"), _ = require("./utils/helpers"); +var validator = require("./validator"); + var Async = require("async"), EventEmitter = require("events").EventEmitter; @@ -36,6 +38,8 @@ var Robot = module.exports = function Robot(opts) { opts = opts || {}; + validator.validate(opts); + var methods = [ "toString", "halt", @@ -53,9 +57,6 @@ var Robot = module.exports = function Robot(opts) { }, this); this.initRobot(opts); - - this.checkForBadSyntax(opts); - this.initConnections(opts); this.initDevices(opts); @@ -166,37 +167,6 @@ Robot.prototype.initRobot = function(opts) { } }; -/** - * Checks provided options object for invalid Robot details - * - * @param {Object} opts options object to check for errors - * @return {void} - */ -Robot.prototype.checkForBadSyntax = function(opts) { - var self = this; - - var RobotDSLError = new Error("Unable to start robot due to a syntax error"); - RobotDSLError.name = "RobotDSLError"; - - function has(prop) { return opts[prop] != null; } - - function checkForSingleObjectSyntax(type) { - var plural = type + "s"; - - if (has(type) && !has(plural)) { - [ - "The single-object '" + type + "' syntax for robots is not valid.", - "Instead, use the multiple-value '" + plural + "' key syntax.", - "Details: http://cylonjs.com/documentation/guides/working-with-robots/" - ].forEach(function(str) { self.log("fatal", str); }); - - throw RobotDSLError; - } - } - - ["connection", "device"].forEach(checkForSingleObjectSyntax); -}; - /** * Initializes a Robot's connections * diff --git a/lib/validator.js b/lib/validator.js new file mode 100644 index 0000000..cc35072 --- /dev/null +++ b/lib/validator.js @@ -0,0 +1,45 @@ +"use strict"; + +// validates an Object containing Robot parameters + +var Logger = require("./logger"); + +function hasProp(object, prop) { + return object.hasOwnProperty(prop); +} + +function die() { + var RobotDSLError = new Error("Unable to start robot due to a syntax error"); + RobotDSLError.name = "RobotDSLError"; + throw RobotDSLError; +} + +function log(messages) { + messages = [].concat(messages); + messages.map(function(msg) { Logger.fatal(msg); }); +} + +var check = {}; + +check.singleObjectSyntax = function(opts, key) { + var single = hasProp(opts, key), + plural = hasProp(opts, key + "s"); + + if (single && !plural) { + log([ + "The single-object '" + key + "' syntax for robots is not valid.", + "Instead, use the multiple-value '" + key + "s' key syntax.", + "Details: http://cylonjs.com/documentation/guides/working-with-robots/" + ]); + + die(); + } +}; + +module.exports.validate = function validate(opts) { + opts = opts || {}; + + ["connection", "device"].map(function(type) { + check.singleObjectSyntax(opts, type); + }); +}; From 10982e40039bece4ff3ee81f237564c293115c34 Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Wed, 17 Jun 2015 09:03:12 -0700 Subject: [PATCH 2/2] Add additional validations --- lib/validator.js | 66 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 9 deletions(-) diff --git a/lib/validator.js b/lib/validator.js index cc35072..06f60d7 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -2,7 +2,8 @@ // validates an Object containing Robot parameters -var Logger = require("./logger"); +var Logger = require("./logger"), + _ = require("./utils/helpers"); function hasProp(object, prop) { return object.hasOwnProperty(prop); @@ -14,32 +15,79 @@ function die() { throw RobotDSLError; } -function log(messages) { +function warn(messages) { messages = [].concat(messages); - messages.map(function(msg) { Logger.fatal(msg); }); + messages.map(function(msg) { Logger.warn(msg); }); } -var check = {}; +function fatal(messages) { + messages = [].concat(messages); + messages.map(function(msg) { Logger.fatal(msg); }); + die(); +} -check.singleObjectSyntax = function(opts, key) { +var checks = {}; + +checks.singleObjectSyntax = function(opts, key) { var single = hasProp(opts, key), plural = hasProp(opts, key + "s"); if (single && !plural) { - log([ + fatal([ "The single-object '" + key + "' syntax for robots is not valid.", "Instead, use the multiple-value '" + key + "s' key syntax.", "Details: http://cylonjs.com/documentation/guides/working-with-robots/" ]); + } +}; - die(); +checks.singleObjectSyntax = function(opts) { + ["connection", "device"].map(function(key) { + var single = hasProp(opts, key), + plural = hasProp(opts, key + "s"); + + if (single && !plural) { + fatal([ + "The single-object '" + key + "' syntax for robots is not valid.", + "Instead, use the multiple-value '" + key + "s' key syntax.", + "Details: http://cylonjs.com/documentation/guides/working-with-robots/" + ]); + } + }); +}; + +checks.deviceWithoutDriver = function(opts) { + if (opts.devices) { + _.each(opts.devices, function(device, name) { + if (!device.driver || device.driver === "") { + fatal("No driver supplied for device " + name); + } + }); + } +}; + +checks.devicesWithoutConnection = function(opts) { + var connections = opts.connections, + devices = opts.devices; + + if (devices && connections && Object.keys(connections).length > 1) { + var first = Object.keys(connections)[0]; + + _.each(devices, function(device, name) { + if (!device.connection || device.connection === "") { + warn([ + "No explicit connection provided for device " + name, + "Will default to using connection " + first + ]); + } + }); } }; module.exports.validate = function validate(opts) { opts = opts || {}; - ["connection", "device"].map(function(type) { - check.singleObjectSyntax(opts, type); + _.each(checks, function(check) { + check(opts); }); };