-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·125 lines (113 loc) · 2.91 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env node
'use strict';
var fs = require('fs'),
tv4 = require('tv4'),
winston = require('winston'),
opts = require('nomnom')
.script('jsonvalidate')
.options(
{
'file': {
abbr: 'f',
help: 'JSON file',
required: true,
default: '-'
},
'extra': {
abbr: 'e',
help: 'extra JSON schema file',
list: true
},
'schema': {
abbr: 's',
help: 'JSON schema file',
required: true
},
'debug': {
abbr: 'd',
flag: true,
help: 'Debug logging',
required: false
}
}
).parse()
, debug = opts.debug
, logger = new winston.Logger({
transports:[
new ( winston.transports.Console ) ({
colorize: true,
level: "debug",
silent: !debug
})
]
})
, filename = opts.file
, schemaname = opts.schema
, extranames = opts.extra
, instream = null
, schemaString = fs.readFileSync(schemaname, 'utf8')
, schema = JSON.parse(schemaString)
, infile
;
function validateInputData(data, schema) {
// console.log("data: ", data);
// console.log("schema: ", schema);
logger.info('Starting validation');
var result = tv4.validateMultiple(data, schema, true, true);
// console.log(tv4.error);
// console.log(tv4.missing);
if (result) {
for(var i = 0; i < result.errors.length; i++) {
delete result.errors[i].stack;
}
process.stdout.write(JSON.stringify(result, null, 4));
} else {
process.stdout.write("{}");
}
process.stdout.write("\n");
}
if (extranames) {
logger.info("Extra schemas ", JSON.stringify(extranames));
for(var i = 0; i < extranames.length; i++) {
var eschemaString = extranames[i]
, eschema = eschemaString.split(':')
, eschemaname
, eschemafile
, eschemaString
, eschemaobject
;
if (eschema.length != 2) {
logger.error("Error: extra schema " + eschemaString + " is not of the form 'URL:file");
process.exit(-1);
}
eschemaname = eschema[0];
eschemafile = eschema[1];
eschemaString = fs.readFileSync(eschemafile, 'utf8');
eschemaobject = JSON.parse(eschemaString);
if (eschemaobject.id) {
logger.debug("Adding schema by id " + eschemaobject.id)
tv4.addSchema(eschemaobject);
} else {
logger.debug("Adding schema " + eschemaname);
tv4.addSchema(eschemaname, eschemaobject);
}
}
logger.debug("Extra schemas loaded", JSON.stringify(tv4.getSchemaMap(), null ,2));
}
if (filename === "-") {
instream = process.openStdin();
instream.on("data", function(data) {
if (infile) {
infile += data;
} else {
infile = data;
}
});
instream.on("end", function() {
logger.debug("infile: ", infile.toString('utf8'));
validateInputData(JSON.parse(infile.toString('utf8')), schema);
});
} else {
infile = fs.readFileSync(filename, 'utf8');
validateInputData(JSON.parse(infile), schema);
}