forked from discourse/discourse_api_docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toschema.js
46 lines (37 loc) · 1.1 KB
/
toschema.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
var gen = require('json-schema-generator');
var yaml = require('yamljs');
var fs = require('fs');
var inputFile = '';
var outputFile = '';
if (process.argv.length > 3) {
inputFile = process.argv[2];
outputFile = process.argv[3];
} else {
console.log('Please specify input and output files!');
console.log('node schema.js input.json output.yml');
}
var addTypeIfEmptyObject = function(obj) {
for(var i in obj) {
if (typeof obj[i] === "object") {
if (Object.keys(obj[i]).length === 0) {
obj[i] = {type: "object"};
} else {
addTypeIfEmptyObject(obj[i]);
}
}
}
return null;
}
fs.readFile(inputFile, 'utf8', function(err, data) {
if (err) throw err;
var json = JSON.parse(data);
var schema = gen(json);
delete schema.required;
//there are some properties without types on them(they should just be set to `null`)
addTypeIfEmptyObject(schema.properties);
var yml = yaml.stringify(schema, 16, 2);
fs.writeFile(outputFile, yml, function(err) {
if (err) return console.log(err);
console.log('converted json response to yml schema');
});
});