forked from auth0/rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.js
50 lines (40 loc) · 1.25 KB
/
validator.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
const _ = require('lodash');
const Ajv = require('ajv');
const yargs = require('yargs');
const argv = yargs
.option('schema', {
alias: 's',
default: __dirname + '/schema.json'
})
.option('file', {
alias: 'f',
default: __dirname + '/rules.json'
})
.argv;
const schema = require(argv.schema);
const data = require(argv.file);
const ajv = new Ajv();
const validate = ajv.compile(schema);
const valid = validate(data);
const errors = validate.errors || [];
console.log(`Validating ${argv.file} ...`);
if (!valid) {
console.log(errors);
process.exit(errors.length);
}
_.each(data, (category) => {
_.each(category.templates, (template, index) => {
let templateFunction, error = 'not a function';
try {
templateFunction = eval(`(${template.code})`);
}
catch (e) {
error = e.message || e;
}
if (typeof templateFunction !== 'function') errors.push(`${category.name}(${index}) - ${template.id}: broken code - ${error}`);
if (template.categories.indexOf(category.name) < 0) errors.push(`${category.name}(${index}) - ${template.id}: category mismatch.`);
});
});
if (errors.length) console.log(errors);
else console.log('Success!');
process.exit(errors.length);