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' ;
2017-01-20 07:56:42 +03:00
var msrest = require ( 'ms-rest' ) ,
msrestazure = require ( 'ms-rest-azure' ) ,
ResourceManagementClient = require ( 'azure-arm-resource' ) . ResourceManagementClient ,
log = require ( './lib/util/logging' ) ,
2017-01-17 09:14:02 +03:00
utils = require ( './lib/util/utils' ) ,
2017-01-20 07:56:42 +03:00
Constants = require ( './lib/util/constants' ) ,
2017-01-17 09:14:02 +03:00
path = require ( 'path' ) ,
2017-01-20 07:56:42 +03:00
util = require ( 'util' ) ,
2017-01-17 09:14:02 +03:00
SpecValidator = require ( './lib/specValidator' ) ;
2016-11-19 09:49:15 +03:00
2017-01-17 09:14:02 +03:00
exports . finalValidationResult = { validityStatus : true } ;
2016-11-19 09:49:15 +03:00
2017-01-17 09:14:02 +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
2017-01-17 09:14:02 +03:00
exports . validateSpec = function validateSpec ( specPath , json ) {
let validator = new SpecValidator ( specPath ) ;
exports . finalValidationResult [ specPath ] = validator . specValidationResult ;
validator . initialize ( ) . then ( function ( ) {
2017-01-20 07:56:42 +03:00
log . info ( ` Semantically validating ${ specPath } : \n ` ) ;
2017-01-17 09:14:02 +03:00
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
2017-01-17 09:14: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
} ) ;
2017-01-20 07:56:42 +03:00
return utils . executePromisesSequentially ( promiseFactories ) ;
2017-01-17 09:14:02 +03:00
} ) . 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 ( ) {
2017-01-20 07:56:42 +03:00
log . info ( ` Validating "examples" and "x-ms-examples" in ${ specPath } : \n ` ) ;
2017-01-17 09:14:02 +03:00
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
} ) ;
2017-01-20 07:56:42 +03:00
return utils . executePromisesSequentially ( promiseFactories ) ;
2017-01-17 09:14:02 +03:00
} ) . catch ( function ( err ) {
log . error ( err ) ;
} ) ;
} ;
2016-11-22 08:26:02 +03:00
2017-01-17 09:14:02 +03:00
exports . updateEndResultOfSingleValidation = function updateEndResultOfSingleValidation ( validator ) {
if ( validator . specValidationResult . validityStatus ) {
2017-01-24 08:16:27 +03:00
let consoleLevel = log . consoleLogLevel ;
log . consoleLogLevel = 'info' ;
2017-01-17 09:14:02 +03:00
log . info ( 'No Errors were found.' ) ;
2017-01-24 08:16:27 +03:00
log . consoleLogLevel = consoleLevel ;
2016-11-22 08:26:02 +03:00
}
2017-01-17 09:14:02 +03:00
if ( ! validator . specValidationResult . validityStatus ) {
exports . finalValidationResult . validityStatus = validator . specValidationResult . validityStatus ;
2016-11-19 09:49:15 +03:00
}
2017-01-17 09:14:02 +03:00
return ;
2017-01-20 07:56:42 +03:00
} ;
2016-11-19 09:49:15 +03:00
2017-01-17 09:14:02 +03:00
exports . logDetailedInfo = function logDetailedInfo ( validator , json ) {
if ( json ) {
2017-01-24 08:16:27 +03:00
let consoleLevel = log . consoleLogLevel ;
log . consoleLogLevel = 'info' ;
2017-01-17 09:14:02 +03:00
log . info ( '############################' ) ;
log . info ( validator . specValidationResult ) ;
log . info ( '----------------------------' ) ;
2017-01-24 08:16:27 +03:00
log . consoleLogLevel = consoleLevel ;
2017-01-17 09:14:02 +03:00
} else {
log . silly ( '############################' ) ;
log . silly ( validator . specValidationResult ) ;
log . silly ( '----------------------------' ) ;
}
} ;
2016-11-19 09:49:15 +03:00
2017-01-17 09:14:02 +03:00
exports = module . exports ;