-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathadaptJsonSchema.js
85 lines (74 loc) · 1.71 KB
/
adaptJsonSchema.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
const _ = require('lodash');
const mapJsonSchema = require('./thriftService/mapJsonSchema');
const adaptJsonSchema = (data, logger, callback, app) => {
try {
const jsonSchema = JSON.parse(data.jsonSchema);
const result = mapJsonSchema(jsonSchema, {}, (schema, parentJsonSchema, key) => {
if (schema.type === 'array' && !schema.subtype) {
return {
...schema,
subtype: getArraySubtypeByChildren(schema),
};
} else {
return schema;
}
});
callback(null, {
...data,
jsonSchema: JSON.stringify(result),
});
} catch (error) {
const err = {
message: error.message,
stack: error.stack,
};
logger.log('error', err, 'Remove nulls from JSON Schema');
callback(err);
}
};
const getArraySubtypeByChildren = arraySchema => {
const subtype = type => `array<${type}>`;
if (!arraySchema.items) {
return;
}
if (Array.isArray(arraySchema.items) && _.uniq(arraySchema.items.map(item => item.type)).length > 1) {
return subtype('union');
}
let item = Array.isArray(arraySchema.items) ? arraySchema.items[0] : arraySchema.items;
if (!item) {
return;
}
switch (item.type) {
case 'string':
case 'text':
return subtype('txt');
case 'number':
case 'numeric':
case 'integer':
return subtype('num');
case 'interval':
return subtype('intrvl');
case 'object':
case 'struct':
return subtype('struct');
case 'array':
return subtype('array');
case 'map':
return subtype('map');
case 'union':
return subtype('union');
case 'timestamp':
return subtype('ts');
case 'date':
return subtype('date');
}
if (item.items) {
return subtype('array');
}
if (item.properties) {
return subtype('struct');
}
};
module.exports = {
adaptJsonSchema,
};