-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
85 lines (68 loc) · 2.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var _ = require('lodash'),
chalk = require('chalk'),
yaml = require('js-yaml'),
parse = require('csv-parse'),
validator = require('validator'),
transform = require('stream-transform');
module.exports = function(yml, stream, validators) {
var index = 0,
errorCount = 0,
config = yaml.safeLoad(yml),
settings = _.extend({ columns: true }, config.settings),
parser = parse(settings),
transformer = transform(testRow);
if (validators && _.isObject(validators)) addValidators(validators);
stream.pipe(parser).pipe(transformer);
stream.on('end', finish);
function testRow(row) {
index++;
_.each(row, function(value, field) {
var error = testField(value, field, row);
if (error) console.error(chalk.red(error));
});
}
function testField(value, field, row) {
var fieldIndex = '✗ [row ' + index + ', field ' + field + '] ',
rules = config.fields[field],
output = [];
rules = _.isString(rules) ? [rules] : rules;
_.each(rules, function(options, rule) {
if (_.isArray(rules)) {
rule = options;
options = undefined;
}
if (_.isObject(rule)) {
options = _.values(rule)[0];
rule = _.keys(rule)[0];
}
var args = _.isArray(options) ?
[value].concat(options) : [value, options],
test = validator[rule];
if (!test) throw new Error('`' + rule + '` is not a valid rule.');
this.row = row;
this.field = field;
this.value = value;
if (!test.apply(this, args)) {
errorCount = errorCount + 1;
output.push(fieldIndex + '`' + rule + '` failed.');
}
});
return output.join('\n');
}
function finish() {
console.log(index + ' rows tested');
if (errorCount) {
var label = errorCount === 1 ? ' error' : ' errors';
console.log(chalk.red(errorCount + label + ' found'));
process.exit(1);
} else {
console.log(chalk.green('no errors found'));
process.exit(0);
}
}
function addValidators(validators) {
_.each(validators, function(func, key) {
validator.extend(key, func);
});
}
};