-
Notifications
You must be signed in to change notification settings - Fork 26
/
generate_example_records.js
executable file
·84 lines (80 loc) · 3.34 KB
/
generate_example_records.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
#!/usr/bin/env node
var fs = require('fs'),
jsf = require('json-schema-faker'),
moment = require('moment'),
path = require('path');
jsf.format('date', function(gen, schema){
var max = moment('2020-12-31', 'YYYY-MM-DD').unix()
var timestamp = Math.floor(Math.random() * max);
var date = moment.unix(timestamp);
return date.format('YYYY-MM-DD');
});
jsf.format('uri-reference', function(gen, schema){ return gen.randexp('^(https?://)?[\\w.]+/[\\w]+[\\w/]+$');});
jsf.format('orcid', function(gen, schema){
var almost_orcid = gen.randexp(/0000-0002-\d{4}-\d{3}/)
var input = almost_orcid.replace(/-/g,'')
var r = 0
for (i = 0; i < input.length; i++) {
r = (r + parseInt(input.split('')[i]))*2
}
var ck = (12 - r % 11) % 11
if (ck == 10) {
ck = 'X'
} else {
ck = String(ck)
}
return almost_orcid + ck
});
jsf.format('timezone', function(gen, schema){
return gen.randexp(/Europe\/Zurich|US\/Eastern|Asia\/Shanghai/)
});
function resolve_schema(unresolved_schema, base_path) {
var keys = [];
var resolved_schema = {};
if (Array.isArray(unresolved_schema)) {
new_array = []
for (var value of unresolved_schema) {
if (value !== null && typeof(value) === 'object') {
new_array = new_array.concat(resolve_schema(value, base_path))
} else {
new_array = new_array.concat([value])
}
}
return new_array
}
for (var key in unresolved_schema) {
if (key === '$ref' && typeof(unresolved_schema[key]) === 'string' && unresolved_schema[key][0] !== '#') {
var schema_path = base_path + "/" + unresolved_schema[key];
var new_base_path = path.dirname(schema_path);
var element_schema = JSON.parse(fs.readFileSync(schema_path));
var resolved_element_schema = resolve_schema(element_schema, new_base_path);
delete(resolved_element_schema['$schema'])
return resolved_element_schema
} else {
if (resolved_schema['type'] === 'array' && !resolved_schema.hasOwnProperty('minItems')){
resolved_schema['minItems'] = 1
}
if (resolved_schema.hasOwnProperty('properties') && !resolved_schema.hasOwnProperty('anyOf')) {
resolved_schema['required'] = Object.keys(resolved_schema['properties'])
}
var value = unresolved_schema[key];
if (value !== null && typeof(value) === 'object') {
value = resolve_schema(unresolved_schema[key], base_path);
delete(value['$schema'])
}
resolved_schema[key] = value
}
}
return resolved_schema
}
var schemas = ['hep', 'authors', 'conferences', 'data', 'experiments', 'institutions', 'jobs', 'journals', 'seminars']
for (var schema_name of schemas) {
console.log('Generating example for ' + schema_name)
var data = fs.readFileSync('inspire_schemas/records/'+schema_name+'.json', 'utf8')
var unresolved_schema = JSON.parse(data)
var full_schema = resolve_schema(unresolved_schema, "inspire_schemas/records")
var sample = jsf(full_schema)
var outfile = schema_name + '_example.json'
fs.writeFile(outfile, JSON.stringify(sample, null, 4))
console.log(' Generated at ' + outfile)
}