-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (88 loc) · 2.67 KB
/
index.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
'use strict';
const Ajv = require('ajv');
const fs = require('fs');
const schema = require('./searchResultV2');
const ajv = new Ajv({verbose: true, allErrors: true});
const toOpenApi = require('json-schema-to-openapi-schema');
const OpenAPISchemaValidator = require('openapi-schema-validator').default;
const swaggerDraft = require('./swaggerDraft.json');
function debug(data)
{
console.log(data);
}
function clone(obj)
{
return JSON.parse(JSON.stringify(obj));
}
function isPrimitive(test) {
return (test !== Object(test));
}
function fixPatternProperties(obj)
{
const defaultValue = obj instanceof Array && [] || {};
return Object.entries(obj).reduce((res, [name, value])=>{
if(name==='additionalProperties')
{
if(!res.additionalProperties)
{
res.additionalProperties = value;
}
return res;
}
if(name==='patternProperties')
{
const inner = fixPatternProperties(Object.values(value)[0]);
res.additionalProperties = inner;
res['x-pattern'] = Object.keys(value)[0];
return res;
}
if(isPrimitive(value))
{
res[name]=value;
return res;
}
res[name]=fixPatternProperties(value);
return res;
}, defaultValue)
}
debug('compiling schema via ajv');
try {
ajv.compile(schema);
}
catch(err)
{
debug('Schema is invalid');
process.exit(1);
}
debug('schema is valid');
fs.writeFileSync('jsonSchema.json', JSON.stringify(schema, null, 3), 'utf8');
debug('converting patternProperties');
const propertiesConverted = fixPatternProperties(schema);
fs.writeFileSync('jsonSchemaConvertedProps.json', JSON.stringify(propertiesConverted, null, 3), 'utf8');
const convertedSchema = toOpenApi(Object.assign( {}, propertiesConverted, {'$schema': 'http://json-schema.org/draft-07/schema#'}));
// debug(convertedSchema);
fs.writeFileSync('openApi.json', JSON.stringify(convertedSchema, null, 3), 'utf8');
var validator = new OpenAPISchemaValidator({
//optional
version: 3,
// optional
version2Extensions: {
/* place any properties here to extend the schema. */
},
// optional
version3Extensions: {
/* place any properties here to extend the schema. */
}
});
const swaggerDoc = clone(swaggerDraft);
swaggerDoc.paths["/_api/searching/startSync5/"].get.responses['200'].content['application/json'].schema = convertedSchema.schema;
fs.writeFileSync('swagger.json', JSON.stringify(swaggerDoc, null, 3), 'utf8');
const errors = validator.validate(swaggerDoc);
if(errors && errors.length)
{
debug(`There were errors while validating converted schema as open api:`);
debug(JSON.stringify(errors, null, 3));
process.exit(1);
}
debug('validation of coverted schema okay!');
process.exit(0);