-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbuild_schemas.js
354 lines (337 loc) · 10.7 KB
/
build_schemas.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* Copyright 2020 Charles Tatibouët
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const path = require("path");
const fs = require("fs-extra");
const axios = require("axios");
const { castArray, has, set, sortBy } = require("lodash");
const cheerio = require("cheerio");
const prettier = require("prettier");
const chalk = require("chalk");
const HttpStatus = require("http-status-codes");
const hardcodedSchemas = require("./hardcoded-schemas.json");
const propertyMultiplicity = require("./property-multiplicity.json");
const schemaOrgSchemasUrl =
"https://schema.org/version/latest/schemaorg-current-https.jsonld";
const schemaOrgDraftVersion = "https://json-schema.org/draft/2020-12/schema";
const schemasDir = path.join(__dirname, "schemas");
const schemaSuffix = ".schema.json";
const typesWithoutMultiplicity = new Set(["Boolean"]);
const inferredMultiplicity = new Set();
const rootSchema = "Thing";
/**
* Perform a GET request
*
* @param uri {string} - The URI to fetch from
* @returns {Promise<string>} - The fetched data
*/
async function fetch(uri) {
const response = await axios.get(uri);
if (response.status !== HttpStatus.OK) {
throw new Error(`Error while fetching '${uri}': ${response.statusText}`);
}
return response.data;
}
/**
* Check if a label has a hardcoded schema
*
* @param label {string} - The label to check
* @returns {boolean} - True if the label has a hardcoded schema
*/
function hasHardcodedSchema(label) {
return Object.prototype.hasOwnProperty.call(hardcodedSchemas, label);
}
/**
* Check if a property has a hardcoded multiplicity
*
* @param propertyLabel {string} - The property to check
* @returns {boolean} - True if the property has a hardcoded multiplicity
*/
function hasHardcodedMultiplicity(propertyLabel) {
return Object.prototype.hasOwnProperty.call(
propertyMultiplicity,
propertyLabel,
);
}
/**
* Convert HTML to plain text
*
* @param html {string} - The HTML to convert
* @returns {string} - The plain text version of the HTML
*/
function htmlToPlainText(html) {
return cheerio.load(html).text();
}
/**
* Build a JSON Schema object representing the parents of a Schema.org class
*
* @param parentsIDs {string[]} - The Schema.org @id of the parents
* @param allSchemaClasses {object[]} - All the Schema.org classes in JSON-LD format
* @returns {object} - The object representing the parents in JSON Schema format
*/
function buildParents(parentsIDs, allSchemaClasses) {
const parents = allSchemaClasses.filter((type) =>
parentsIDs.includes(type["@id"]),
);
return sortBy(parents, ["rdfs:label"]).map((parent) => ({
description: htmlToPlainText(parent["rdfs:comment"]),
$ref: parent["@id"],
}));
}
/**
* Build a JSON Schema type from a Schema.org type
*
* @param type {object} - The Schema.org type in JSON-LD format
* @returns {object} - The corresponding type in JSON Schema format
*/
function buildType(type) {
const typeLabel = type["rdfs:label"];
return hasHardcodedSchema(typeLabel)
? hardcodedSchemas[`${typeLabel}`]
: {
$ref: type["@id"],
};
}
/**
* Build multiple JSON Schema types from Schema.org types
*
* @param types {object[]} - The Schema.org types in JSON-LD format
* @param isArray {boolean} - True if the property can have multiple values
* @returns {object} - The corresponding types in JSON Schema format
*/
function buildTypes(types, isArray) {
if (types.length > 1) {
const possibleTypes = {
anyOf: sortBy(
types.map((type) => buildType(type)),
["type", "format", "$ref"],
),
};
if (isArray) {
return {
oneOf: [
possibleTypes,
{
type: "array",
items: possibleTypes,
},
],
};
}
return possibleTypes;
}
const typeLabel = types[0]["rdfs:label"];
const type = buildType(types[0]);
return isArray && !typesWithoutMultiplicity.has(typeLabel)
? {
oneOf: [
type,
{
type: "array",
items: type,
},
],
}
: type;
}
/**
* Get the properties of a Schema.org class
*
* @param id {string} - The ID of the Schema.org class
* @param allProperties {object[]} - All the Schema.org properties in JSON-LD format
* @returns {object[]} - The properties of the Schema.org class in JSON-LD format
*/
function getProperties(id, allProperties) {
return allProperties.filter(
(property) =>
has(property, "schema:domainIncludes") &&
castArray(property["schema:domainIncludes"]).some(
(parent) => parent["@id"] === id,
),
);
}
/**
* Build JSON Schema properties from Schema.org properties
*
* @param properties {object[]} - The Schema.org properties in JSON-LD format
* @param allSchemaClasses {object[]} - All the Schema.org classes in JSON-LD format
* @returns {object} - The object representing the properties in JSON Schema format
*/
function buildProperties(properties, allSchemaClasses) {
return properties.reduce((accumulator, property) => {
const propertyLabel = property["rdfs:label"];
const propertyDescription = htmlToPlainText(property["rdfs:comment"]);
if (hasHardcodedSchema(propertyLabel)) {
set(accumulator, [propertyLabel], {
description: propertyDescription,
...hardcodedSchemas[`${propertyLabel}`],
});
return accumulator;
}
const types = allSchemaClasses.filter((_schemaClass) =>
castArray(property["schema:rangeIncludes"]).some(
(possibleType) => possibleType["@id"] === _schemaClass["@id"],
),
);
if (types.length > 0) {
let isArray;
if (hasHardcodedMultiplicity(propertyLabel)) {
isArray = propertyMultiplicity[`${propertyLabel}`];
} else {
isArray = !propertyDescription.startsWith("The ");
inferredMultiplicity.add(propertyLabel);
}
set(accumulator, [propertyLabel], {
description: propertyDescription,
...buildTypes(types, isArray),
});
} else if (hasHardcodedSchema(propertyLabel)) {
set(accumulator, [propertyLabel], {
description: propertyDescription,
...hardcodedSchemas[`${propertyLabel}`],
});
} else {
console.error(chalk.red(`Unknown type for property ${propertyLabel}`));
}
return accumulator;
}, {});
}
/**
* Build a JSON Schema from a Schema.org class
*
* @param schemaClass {object} - The Schema.org class in JSON-LD format
* @param allSchemaClasses {object[]} - All the Schema.org classes in JSON-LD format
* @param allProperties {object[]} - All the Schema.org properties in JSON-LD format
* @param enumValues {object[]} - All the Schema.org enum values in JSON-LD format
* @returns {object} - The generated JSON Schema
*/
function buildSchema(schemaClass, allSchemaClasses, allProperties, enumValues) {
const id = schemaClass["@id"];
const title =
schemaClass["rdfs:label"] &&
typeof schemaClass["rdfs:label"].valueOf() === "string"
? schemaClass["rdfs:label"]
: schemaClass["rdfs:label"]["@value"];
const description = htmlToPlainText(schemaClass["rdfs:comment"]);
const schema = {
$schema: schemaOrgDraftVersion,
$id: id,
title,
description,
type: "object",
};
if (has(schemaClass, "rdfs:subClassOf")) {
const parentsIDs = castArray(schemaClass["rdfs:subClassOf"]).map(
(parent) => parent["@id"],
);
if (parentsIDs.includes("schema:Enumeration")) {
const enumMembers = enumValues.filter(
(enumValue) => enumValue["@type"] === id,
);
if (enumMembers.length > 0) {
schema.type = "string";
schema.oneOf = sortBy(enumMembers, ["rdfs:label"]).map(
(enumMember) => ({
description: htmlToPlainText(enumMember["rdfs:comment"]),
const: enumMember["rdfs:label"],
}),
);
}
} else {
const parents = buildParents(parentsIDs, allSchemaClasses);
if (parents.length > 0) {
schema.allOf = parents;
}
}
}
if (title === rootSchema) {
schema.properties = {
"@context": {
type: "string",
},
"@type": {
type: "string",
},
};
}
const propertiesToBuild = getProperties(id, allProperties);
const properties = buildProperties(
sortBy(propertiesToBuild, ["rdfs:label"]),
allSchemaClasses,
);
for (const [propertyLabel, property] of Object.entries(properties)) {
set(schema, ["properties", propertyLabel], property);
}
return schema;
}
/**
* Generate JSON Schemas from all the Schema.org classes
*
* @returns {Promise<void>} - Completion of the generation
*/
async function main() {
const jsonld = await fetch(schemaOrgSchemasUrl);
const graph = jsonld["@graph"];
const schemaClasses = graph.filter((vocabulary) =>
castArray(vocabulary["@type"]).includes("rdfs:Class"),
);
const properties = graph.filter((vocabulary) =>
castArray(vocabulary["@type"]).includes("rdf:Property"),
);
const enumValues = graph.filter((vocabulary) =>
castArray(vocabulary["@type"]).some(
(type) => type.startsWith("schema:") && type !== "schema:DataType",
),
);
await fs.ensureDir(schemasDir);
for (const schemaClass of schemaClasses.filter(
(schema) => !castArray(schema["@type"]).includes("schema:DataType"),
)) {
const schema = buildSchema(
schemaClass,
schemaClasses,
properties,
enumValues,
);
const filename = `${schema.title}${schemaSuffix}`;
fs.writeFileSync(
path.join(schemasDir, filename),
prettier.format(JSON.stringify(schema), { parser: "json" }),
);
}
return schemaClasses.length;
}
/* eslint-disable promise/prefer-await-to-callbacks, promise/prefer-await-to-then */
if (require.main === module) {
main()
.then((schemasCount) => {
for (const propertyName of Array.from(inferredMultiplicity).sort()) {
console.warn(
chalk.yellow(`Multiplicity of property ${propertyName} was inferred`),
);
}
console.log(
chalk.green(
`Built ${schemasCount} schema${schemasCount > 1 ? "s" : ""}`,
),
);
})
.catch((error) => {
console.error(error);
process.exitCode = 1;
});
}
/* eslint-enable promise/prefer-await-to-callbacks, promise/prefer-await-to-then */
module.exports = { main, buildSchema };