未验证 提交 b3091ef3 编写于 作者: B beyondgfw

Check the structure of the input data.

上级 94376b99
var assert = require('assert');
function checkType(obj, propName, typeName) {
var prop = obj[propName];
var actualTypeName = Array.isArray(prop) ? 'array' : typeof prop;
var msg = `"${propName}" must be of type: ${typeName}, but it is: ${actualTypeName} "${prop}".`;
assert(actualTypeName === typeName, msg);
}
module.exports = function (data) {
checkType(data, 'header', 'string');
checkType(data, 'footer', 'string');
checkType(data, 'blockHeader', 'string');
checkType(data, 'blockFooter', 'string');
checkType(data, 'hosts', 'array');
data.hosts.forEach(function (block) {
checkType(block, 'name', 'string');
checkType(block, 'items', 'array');
block.items.forEach(function (item) {
if (item.comment !== undefined) checkType(item, 'comment', 'string');
else {
checkType(item, 'ip', 'string');
if (item.domain !== undefined) checkType(item, 'domain', 'string');
else {
checkType(item, 'domains', 'array');
item.domains.forEach(function (domain) {
checkType({ domains: domain }, 'domains', 'string');
});
}
}
});
});
};
......@@ -12,6 +12,7 @@ glob.sync(path.join(process.argv[2], '*.yml')).forEach(function(specFile) {
data.hosts = specData.hosts.concat(data.hosts);
specSuffix = `-${specName}`;
}
require('./check-structure')(data);
glob.sync(path.join(__dirname, 'generators/*.js')).forEach(function (genFile) {
var generator = require(genFile);
var outputPath = path.parse(path.join(process.argv[4], generator.fileName));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册