oav/validate.js

125 строки
4.5 KiB
JavaScript
Исходник Обычный вид История

2016-11-19 09:49:15 +03:00
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
'use strict';
var msrest = require('ms-rest'),
msrestazure = require('ms-rest-azure'),
ResourceManagementClient = require('azure-arm-resource').ResourceManagementClient,
log = require('./lib/util/logging'),
utils = require('./lib/util/utils'),
Constants = require('./lib/util/constants'),
path = require('path'),
util = require('util'),
SpecValidator = require('./lib/specValidator');
2016-11-19 09:49:15 +03:00
exports.finalValidationResult = { validityStatus: true };
2016-11-19 09:49:15 +03:00
exports.getDocumentsFromCompositeSwagger = function getDocumentsFromCompositeSwagger(compositeSpecPath) {
let compositeSwagger;
let finalDocs = [];
return utils.parseJson(compositeSpecPath).then(function (result) {
compositeSwagger = result;
if (!(compositeSwagger.documents && Array.isArray(compositeSwagger.documents) && compositeSwagger.documents.length > 0)) {
throw new Error(`CompositeSwagger - ${compositeSpecPath} must contain a documents property and it must be of type array and it must be a non empty array.`);
}
let docs = compositeSwagger.documents;
let basePath = path.dirname(compositeSpecPath);
for (let i=0; i<docs.length; i++) {
if (docs[i].startsWith('.')) {
docs[i] = docs[i].substring(1);
}
let individualPath = '';
if (docs[i].startsWith('http')) {
individualPath = docs[i];
} else {
individualPath = basePath + docs[i];
}
finalDocs.push(individualPath);
}
return finalDocs;
}).catch(function (err) {
return Promise.reject(err);
});
};
2016-11-19 09:49:15 +03:00
exports.validateSpec = function validateSpec(specPath, json) {
let validator = new SpecValidator(specPath);
exports.finalValidationResult[specPath] = validator.specValidationResult;
validator.initialize().then(function() {
log.info(`Semantically validating ${specPath}:\n`);
validator.validateSpec();
exports.updateEndResultOfSingleValidation(validator);
exports.logDetailedInfo(validator, json);
return;
}).catch(function(err) {
log.error(err);
return;
});
};
2016-11-22 08:26:02 +03:00
exports.validateCompositeSpec = function validateCompositeSpec(compositeSpecPath, json){
return exports.getDocumentsFromCompositeSwagger(compositeSpecPath).then(function(docs) {
let promiseFactories = docs.map(function(doc) {
return exports.validateSpec(doc, json);
2016-11-22 08:26:02 +03:00
});
return utils.executePromisesSequentially(promiseFactories);
}).catch(function (err) {
log.error(err);
});
};
exports.validateExamples = function validateExamples(specPath, operationIds, json) {
let validator = new SpecValidator(specPath);
exports.finalValidationResult[specPath] = validator.specValidationResult;
validator.initialize().then(function() {
log.info(`Validating "examples" and "x-ms-examples" in ${specPath}:\n`);
validator.validateOperations(operationIds);
exports.updateEndResultOfSingleValidation(validator);
exports.logDetailedInfo(validator, json);
return;
}).catch(function (err) {
log.error(err);
});
};
exports.validateExamplesInCompositeSpec = function validateExamplesInCompositeSpec(compositeSpecPath, json){
return exports.getDocumentsFromCompositeSwagger(compositeSpecPath).then(function(docs) {
let promiseFactories = docs.map(function(doc) {
return exports.validateExamples(doc, json);
2016-11-22 08:26:02 +03:00
});
return utils.executePromisesSequentially(promiseFactories);
}).catch(function (err) {
log.error(err);
});
};
2016-11-22 08:26:02 +03:00
exports.updateEndResultOfSingleValidation = function updateEndResultOfSingleValidation(validator) {
if (validator.specValidationResult.validityStatus) {
let consoleLevel = log.consoleLogLevel;
log.consoleLogLevel = 'info';
log.info('No Errors were found.');
log.consoleLogLevel = consoleLevel;
2016-11-22 08:26:02 +03:00
}
if (!validator.specValidationResult.validityStatus) {
exports.finalValidationResult.validityStatus = validator.specValidationResult.validityStatus;
2016-11-19 09:49:15 +03:00
}
return;
};
2016-11-19 09:49:15 +03:00
exports.logDetailedInfo = function logDetailedInfo(validator, json) {
if (json) {
let consoleLevel = log.consoleLogLevel;
log.consoleLogLevel = 'info';
log.info('############################');
log.info(validator.specValidationResult);
log.info('----------------------------');
log.consoleLogLevel = consoleLevel;
} else {
log.silly('############################');
log.silly(validator.specValidationResult);
log.silly('----------------------------');
}
};
2016-11-19 09:49:15 +03:00
exports = module.exports;