forked from geraintluff/schema-org-gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.js
executable file
·253 lines (237 loc) · 6.31 KB
/
generate.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
var OUTPUT_DIR = "schema.org";
var OUTPUT_SUFFIX = ".json";
var URL_PREFIX = "";
var URL_SUFFIX = '.json';
var REL_PREFIX = 'http://schema.org/';
var path = require('path');
var fs = require('fs');
var request = require('request');
var prettyJson = require('./pretty-json');
var hardcodedSchemas = require('./hardcoded-schemas.json');
var propertyMultiplicity = require('./property-multiplicity.json');
var ignoreProperties = require('./ignore-properties.json');
try {
fs.mkdirSync(OUTPUT_DIR);
} catch (e) {
}
function createRel(key) {
if (key === 'url') {
return 'self';
}
return REL_PREFIX + encodeURIComponent(key);
}
request.get('http://schema.link.fish/downloads/all.json', function (err, request, body) {
if (err) throw err;
var allData = JSON.parse(body);
var allSchemas = {};
function getHardcoded(key) {
return JSON.parse(JSON.stringify(hardcodedSchemas[key]));
}
function merge(objA) {
var result = {};
for (var i = 0; i < arguments.length; i++) {
var obj = arguments[i];
for (var key in obj) {
result[key] = obj[key];
}
}
return result;
}
function trimSchema(schema) {
if (!schema.title) {
delete schema.title;
}
if (!schema.description) {
delete schema.description;
}
if (schema.type.length == 0) {
delete schema.type;
}
if (schema.allOf.length == 0) {
delete schema.allOf;
}
if (schema.links.length == 0) {
delete schema.links;
}
if (Object.keys(schema.properties).length == 0) {
delete schema.properties;
if (schema.type == 'object' && schema.allOf) {
delete schema.type;
}
}
if (Object.keys(schema.definitions).length == 0) {
delete schema.definitions;
}
return schema;
}
function createSchema(key, spec) {
var schemaId = [URL_PREFIX,spec.id,URL_SUFFIX].join('');
var schema = {
id: schemaId,
title: spec.label,
description: spec.comment_plain,
format: spec.url,
media: {type: 'application/json;profile=' + spec.url},
allOf: [],
type: [],
properties: {},
links: [],
definitions: {}
};
//if (spec.instances && spec.instances.length > 0) {
// schema['enum'] = spec.instances;
//}
schema.type = 'object';
schema.properties = {};
var linkRefObj = {
type: 'string',
format: 'uri',
links: [{ 'rel': 'full', 'href': '{+$}' }]
};
schema.definitions.array = {
type: 'array',
items: {'$ref': '#'}
};
if (hardcodedSchemas[key]) {
return trimSchema(merge(schema, getHardcoded(key)));
}
schema.definitions.possibleRef = {
oneOf: [
{'$ref': '#'},
linkRefObj
]
};
schema.definitions.possibleRefArray = {
oneOf: [
{
type: 'array',
items: {'$ref': '#/definitions/possibleRef'}
},
linkRefObj
]
};
schema.allOf = spec.supertypes.map(function (supertype) {
return {"$ref": supertype + URL_SUFFIX};
});
spec.specific_properties.forEach(function (key) {
if (key === 'array' || key === 'possibleRef' || key === 'possibleRefArray') {
throw new Error('Not allowed key: ' + key);
}
var propSpec = allData.properties[key];
if (ignoreProperties[key] || /\(legacy spelling;/.test(propSpec['comment_plain'])) {
ignoreProperties[key] = true;
return;
}
if (hardcodedSchemas[key]) {
schema.properties[key] = getHardcoded(key);
} else {
var options = [];
propSpec.ranges.forEach(function (type) {
if (hardcodedSchemas[type]) {
options.push(getHardcoded(type));
} else {
options.push({"$ref": type + URL_SUFFIX + "#/definitions/possibleRef"});
}
});
if (options.length == 1) {
schema.properties[key] = options[0];
} else {
schema.properties[key] = {anyOf: options};
}
}
var description = propSpec['comment_plain'];
if (typeof propertyMultiplicity[key] !== 'boolean') {
if (/^An? /.test(description)) {
propertyMultiplicity[key] = true;
} else if (/^The /.test(description) || /^is[A-Z]/.test(key)) {
propertyMultiplicity[key] = false;
} else {
propertyMultiplicity[key] = description;
}
}
var subSchema = schema.properties[key];
var shouldAddLink = (subSchema.format === 'uri');
if (!schema.properties[key]['$ref'] && !shouldAddLink) {
var subSchema = schema.properties[key];
subSchema = merge({
title: propSpec.label,
description: propSpec.comment_plain
}, subSchema);
schema.definitions[key] = subSchema
schema.properties[key] = {'$ref': '#/definitions/' + key}
}
if (propertyMultiplicity[key] === true) {
var subSchema = schema['properties'][key];
if (subSchema['$ref'] && /^[^#]+#\/definitions\/possibleRef?$/.test(subSchema['$ref'])) {
subSchema['$ref'] += 'Array';
} else if (shouldAddLink) {
if (subSchema['$ref']) {
subSchema = {
allOf: [subSchema]
};
}
subSchema.links = subSchema.links || [];
subSchema.links.push({
rel: createRel(key),
href: "{+$}",
linkSource: 2
});
schema.properties[key] = {
type: "array",
items: subSchema
};
} else {
schema.properties[key] = {
oneOf: [
{
type: "array",
items: subSchema
},
subSchema
]
};
}
} else if (propertyMultiplicity[key] === false) {
if (shouldAddLink) {
schema.links.push({
rel: createRel(key),
href: "{+" + encodeURIComponent(key) + "}"
});
}
} else {
var subSchema = schema['properties'][key];
if (shouldAddLink) {
if (subSchema['$ref']) {
subSchema = {
allOf: [subSchema]
};
}
subSchema.links = subSchema.links || [];
subSchema.links.push(linkRefObj.links[0]);
}
schema.properties[key] = {
oneOf: [
subSchema,
{
type: "array",
items: subSchema
}
]
};
}
});
return trimSchema(schema);
}
for (var key in allData.types) {
allSchemas[key] = createSchema(key, allData.types[key]);
}
for (var key in allData.types) {
var spec = allData.types[key];
var filename = path.join(OUTPUT_DIR, spec.id + OUTPUT_SUFFIX);
fs.writeFileSync(filename, prettyJson(allSchemas[key]));
}
fs.writeFileSync('./property-multiplicity.json', prettyJson(propertyMultiplicity));
fs.writeFileSync('./ignore-properties.json', prettyJson(ignoreProperties));
var typeCount = Object.keys(allSchemas).length;
console.log('Successfully generated',typeCount,'schemas in',OUTPUT_DIR,'!');
});