-
Notifications
You must be signed in to change notification settings - Fork 2
/
validate.js
executable file
·332 lines (297 loc) · 10.6 KB
/
validate.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
#!/bin/node
/**
* This script validate a service swagger schema
**/
const Fs = require('fs');
const Path = require('path');
const ZSchema = require('z-schema');
const LuaParser = require('luaparse');
const Yaml = require('js-yaml');
const DeepCopy = require('deep-copy');
const Promise = require('bluebird');
const Tls = require('tls');
const exchangeSchema = require('./exchangeServiceSchemaDefinition.json');
const ASYNC_TIMEOUT = 5000;
const serviceFolder = process.argv.slice(2)[0] || './examples';
ZSchema.registerFormat('json', str => {
try {
JSON.parse(str);
return true;
} catch (err) {
return false;
}
});
ZSchema.registerFormat('lua', str => {
try {
LuaParser.parse(str, { luaVersion: '5.2' });
return true;
} catch (err) {
console.log(`[swagger] lua parsing error: ${err.message}`);
return false;
}
});
ZSchema.registerFormat('secureHost', (host, callback) => {
const hostParts = host.split(':');
const domain = hostParts[0];
const port = hostParts[1] || 443;
const options = {servername: domain};
const client = Tls.connect(port, domain, options, callback.bind(this, true));
client.on('error', (err) => {
console.log(`Host checking failed with ${err}, ${host}`);
callback(false); // eslint-disable-line standard/no-callback-literal
client.end();
});
setTimeout(_ => {
console.log(`Host checking timeout: ${host}`);
callback(false); // eslint-disable-line standard/no-callback-literal
client.end();
}, ASYNC_TIMEOUT - 500);
});
/**
* Custom error constructor
*
* @param {string|Error} message or error
* @param {Error} [error]
* @returns {Error}
**/
class WrongSwaggerError extends Error {
constructor (message, error) {
if (!error) {
error = message;
}
if (typeof message !== 'string') {
message = error.message || error[0] && error[0].message || '';
}
super(`Wrong Swagger definition: ${message}`);
this.statusCode = 400;
this.error = error.error || error.data || error.details || error;
}
}
const validator = new ZSchema({
asyncTimeout: ASYNC_TIMEOUT,
ignoreUnknownFormats: true
});
const LIFECYCLETAGS = ['x-exosite-init', 'x-exosite-info', 'x-exosite-update', 'x-exosite-gc'];
/**
* Resolve schema references
*
* @param {object} obj the current schema node
* @param {object} schema the full swagger
* @param {[string]} depth current tree depth to avoid endless recursion
* @returns {object} resolved node
**/
function resolveReferences (obj, schema, depth = []) {
if (!obj) {
return obj;
}
Object.assign(obj, obj.schema || {});
delete obj.schema;
if ('$ref' in obj) {
if (depth.includes(obj.$ref)) {
throw new WrongSwaggerError(`Circular schema reference in ${schema.alias || schema.info.title}: ${depth}`);
}
depth.push(obj.$ref);
const path = obj.$ref.split('#').pop();
// TODO could get remote reference.. not for now
if (!path) {
throw new WrongSwaggerError(`Wrong reference ${obj.$ref}: no path in schema ${schema.alias || schema.info.title}`);
}
const ref = path.replace(/^\//, '').split('/')
.filter(path => path)
.reduce((ref, path) =>
ref && ref[path], schema);
if (!ref || typeof ref !== 'object') {
throw new WrongSwaggerError(`Wrong reference ${obj.$ref}: cannot resolve ${path} or wrong type ${typeof ref} in schema ${schema.alias || schema.info.title}`);
}
if (!('type' in ref)) {
ref.type = 'object';
}
const definition = resolveReferences(ref, schema, depth);
if (!definition) {
return;
}
if (obj.description && definition.description && obj.description !== definition.description) {
obj.title = obj.description;
}
Object.assign(obj, definition);
delete obj.$ref;
}
// resolve parameters
if (typeof obj.properties === 'object') {
for (var prop in obj.properties) {
const subProperty = resolveReferences(obj.properties[prop], schema, depth.concat(prop));
if (!subProperty) {
delete obj.properties[prop];
continue;
}
// comply draft 3+ JSON schema required for parameters
if (obj.properties[prop].required === true) {
if (typeof obj.required !== 'object' || !(obj.required instanceof Array)) {
obj.required = [];
}
if (obj.required.indexOf(prop) === -1) {
obj.required.push(prop);
}
delete obj.properties[prop].required;
}
} // for properties
}
if (typeof obj.patternProperties === 'object') {
for (var pattProp in obj.patternProperties) {
const subProperty = resolveReferences(obj.patternProperties[pattProp], schema, depth.concat(pattProp));
if (!subProperty) {
delete obj.patternProperties[pattProp];
}
} // for patternProperties
}
if (typeof obj.additionalProperties === 'object') {
const subProperty = resolveReferences(obj.additionalProperties, schema, depth.concat('additionalProperties'));
if (!subProperty) {
delete obj.additionalProperties;
}
}
// resolve items
if ('items' in obj) {
resolveReferences(obj.items, schema, depth.concat('[]'));
}
return obj;
} // resolveReferences
/**
* Parse & validate a swagger schema
*
* @param {string} schema SWAGGER
* @return {object} swagger
**/
function parse (schema) {
if (typeof schema === 'object') {
return schema;
}
if (typeof schema !== 'string') {
throw new WrongSwaggerError('Schema must be a string');
}
if (!schema.length) {
throw new WrongSwaggerError('Schema cannot be empty');
}
try {
return schema.charAt(0) === '{' ? JSON.parse(schema)
: Yaml.safeLoad(schema);
} catch (error) {
throw new WrongSwaggerError(`Unable to parse the swagger schema: ${error.message}`);
}
}
function _customValidate (schema) {
if (!schema.schemes || !schema.schemes.length) {
schema.schemes = ['https'];
} else if (schema.schemes[0] !== 'https') {
// Un-secured external service, reject it.
throw new WrongSwaggerError('Invalid url, https protocol is required');
}
// @TODO instead make a 'resolveReferences' function that doesn't modify the schema
const copy = schema;
schema = DeepCopy(schema);
if ('parameters' in schema) {
for (let parameter in schema.parameters) {
resolveReferences(schema.parameters[parameter], schema, [ 'parameters', parameter ]);
}
}
// map operations to ensure the unique
const operationIdsMap = {};
for (var path in schema.paths) {
// get parameters from the path
const pathParams = {};
if ('parameters' in schema.paths[path]) {
for (let i = 0, len = schema.paths[path].parameters.length; i < len; i++) {
resolveReferences(schema.paths[path].parameters[i], schema, [ path, 'parameters', i ]);
pathParams[schema.paths[path].parameters[i].name] = schema.paths[path].parameters[i];
}
}
for (var method in schema.paths[path]) {
// skip custom fields
if (method === 'parameters' || method.toLowerCase().indexOf('x-') !== -1) {
continue;
} else if (method === '$ref') {
// @TODO fetch the reference: resolve(schema.paths[path][method])
continue;
}
const operation = schema.paths[path][method];
if (operation.operationId in operationIdsMap) {
// Duplicated operationId
throw new WrongSwaggerError(`Duplicated operationId ${operation.operationId} at ${method} ${path}`);
} else { // Add to map
operationIdsMap[operation.operationId] = operation;
} // If else operation
if (operation.responses) {
for (let resp in operation.responses) {
if ('schema' in operation.responses[resp]) {
resolveReferences(operation.responses[resp], schema, [ path, method, 'responses', resp ]);
}
} // for responses
} // if operation.responses
if (operation.parameters) {
// Map operations to ensure the unique
const parameters = Object.assign({}, pathParams);
// Check parameters uniqueness
for (let pi = 0, plen = operation.parameters.length; pi < plen; pi++) {
resolveReferences(operation.parameters[pi], schema, [ path, method, 'parameters', pi ]);
if (parameters[operation.parameters[pi].name]) {
throw new WrongSwaggerError(`Duplicated parameters "${operation.parameters[pi].name}" in operation ${operation.operationId}`);
}
parameters[operation.parameters.name] = operation.parameters[pi];
} // for parameters
} // if operation.parameters
} // for each operations
} // for each paths
// Ensure service namespace life-cycle operations exists
const missingLCOperation = LIFECYCLETAGS.find(tag => tag in schema && !(schema[tag] in operationIdsMap));
if (missingLCOperation) {
throw new WrongSwaggerError(`Non-existing ${missingLCOperation} operation ${schema[missingLCOperation]}`);
}
return copy;
} // _customValidate
/**
* Validate a Service schema, and ensure it is Pegasus compatible
*
* @param {object|string} schema SWAGGER
* @returns {promise} swagger
**/
async function validate (schema) {
if (!schema) return;
schema = parse(schema);
return Promise.fromCallback(callback =>
validator.validate(schema, exchangeSchema, callback), {multiArgs: true})
.catch(error =>
Promise.reject(new WrongSwaggerError(error)))
.spread((valid, error) =>
error ? Promise.reject(new WrongSwaggerError(error))
: Promise.try(_ => _customValidate(schema)));
} // validateSwagger
function loadAndTestService (service) {
var swagger;
if (service.match(/\.yaml$/)) {
const yaml = require('js-yaml');
const YAMLSchema = Fs.readFileSync(Path.resolve(__dirname, Path.join(serviceFolder, service)), 'utf8');
try {
swagger = yaml.safeLoad(YAMLSchema);
} catch (err) {
return Promise.reject(new Error(`Error during YAML parsing of service ${service}: ${err.message}`));
}
} else {
// Expect JSON
swagger = require(`../${serviceFolder}/${service}`);
}
return validate(swagger)
.then(
_ => console.log(`${service}: ok`),
err =>
console.log(`Invalid ${service} schema:`, JSON.stringify(err.error || err, null, 2)) ||
Promise.reject(new Error(`Invalid ${service} schema: ${err.message || err}`)));
}
// load and check all services in the services directory
console.log(`Validating core services from folder ${serviceFolder} ..`);
Promise.all(
Fs.readdirSync(Path.resolve(__dirname, serviceFolder))
.filter(file => ['yaml', 'json'].indexOf(file.split('.')[1]) !== -1)
.map(loadAndTestService))
.then(
_ => console.log('All services are valid!') || process.exit(0),
err => console.log(err.message) || process.exit(1));