Add additional validations

This commit is contained in:
Andrew Stewart 2015-06-17 09:03:12 -07:00
parent 4238385be4
commit 10982e4003
1 changed files with 57 additions and 9 deletions

View File

@ -2,7 +2,8 @@
// validates an Object containing Robot parameters // validates an Object containing Robot parameters
var Logger = require("./logger"); var Logger = require("./logger"),
_ = require("./utils/helpers");
function hasProp(object, prop) { function hasProp(object, prop) {
return object.hasOwnProperty(prop); return object.hasOwnProperty(prop);
@ -14,32 +15,79 @@ function die() {
throw RobotDSLError; throw RobotDSLError;
} }
function log(messages) { function warn(messages) {
messages = [].concat(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), var single = hasProp(opts, key),
plural = hasProp(opts, key + "s"); plural = hasProp(opts, key + "s");
if (single && !plural) { if (single && !plural) {
log([ fatal([
"The single-object '" + key + "' syntax for robots is not valid.", "The single-object '" + key + "' syntax for robots is not valid.",
"Instead, use the multiple-value '" + key + "s' key syntax.", "Instead, use the multiple-value '" + key + "s' key syntax.",
"Details: http://cylonjs.com/documentation/guides/working-with-robots/" "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) { module.exports.validate = function validate(opts) {
opts = opts || {}; opts = opts || {};
["connection", "device"].map(function(type) { _.each(checks, function(check) {
check.singleObjectSyntax(opts, type); check(opts);
}); });
}; };