Adding usage guide for validator API

This commit is contained in:
Abhijit Kane
2019-12-18 12:10:49 +05:30
parent 18e00de729
commit 56075c044d
2 changed files with 19 additions and 9 deletions

View File

@@ -221,7 +221,6 @@ class SchemaPack {
* @description Takes in a transaction object (meant to represent a Postman history object) * @description Takes in a transaction object (meant to represent a Postman history object)
* *
* @param {*} transactions RequestList * @param {*} transactions RequestList
* @param {*} options Validation-specific options, optional
* @param {*} callback return * @param {*} callback return
* @returns {boolean} validation * @returns {boolean} validation
*/ */
@@ -239,8 +238,7 @@ class SchemaPack {
async.map(transactions, (transaction, requestCallback) => { async.map(transactions, (transaction, requestCallback) => {
// 1. Look at transaction.request.URL, and find matching request from schema // 1. Look at transaction.request.URL + method, and find matching request from schema
console.log('Starting checks for txn' + transaction.id);
let matchedPaths = schemaUtils.findMatchingRequestFromSchema( let matchedPaths = schemaUtils.findMatchingRequestFromSchema(
transaction.request.method, transaction.request.method,
transaction.request.url, // must be a string transaction.request.url, // must be a string
@@ -248,13 +246,12 @@ class SchemaPack {
); );
if (!matchedPaths.length) { if (!matchedPaths.length) {
console.log('No matched paths found'); // No matching paths found
return requestCallback(null, []); return requestCallback(null, []);
} }
// 2. perform validation for each identified matchedPath (schema endpoint) // 2. perform validation for each identified matchedPath (schema endpoint)
async.map(matchedPaths, (matchedPath, pathsCallback) => { return async.map(matchedPaths, (matchedPath, pathsCallback) => {
console.log('Starting validation for txn ' + transaction.id + ' for path ' + matchedPath.name);
// 3. validation involves checking these individual properties // 3. validation involves checking these individual properties
async.parallel({ async.parallel({
path: function(cb) { path: function(cb) {
@@ -274,7 +271,6 @@ class SchemaPack {
matchedPath.path, componentsAndPaths, options, cb); matchedPath.path, componentsAndPaths, options, cb);
} }
}, (err, result) => { }, (err, result) => {
console.log('Checked all properties for txn ' + transaction.id + ' for path ' + matchedPath.name);
let allMismatches = _.concat(result.headers, result.path, result.requestBody), let allMismatches = _.concat(result.headers, result.path, result.requestBody),
retVal = { matched: true }; // no mismatch retVal = { matched: true }; // no mismatch
@@ -286,9 +282,7 @@ class SchemaPack {
} }
pathsCallback(null, retVal); pathsCallback(null, retVal);
}); });
}, (err, result) => { }, (err, result) => {
console.log('Checked all matchedPaths for txn ' + transaction.id);
requestCallback(err, { requestCallback(err, {
requestId: transaction.id, requestId: transaction.id,
endpoints: result endpoints: result

16
validator_perf.js Normal file
View File

@@ -0,0 +1,16 @@
/* eslint-disable */
var Converter = require('./index.js'),
fs = require('fs'),
path = require('path'),
VALID_OPENAPI_PATH = './test/data/.temp/specs/spec-to-validate-against.json',
HISTORY_PATH = './test/data/.temp/specs/history_obj.json';
var openapi = JSON.parse(fs.readFileSync(path.join(__dirname, VALID_OPENAPI_PATH), 'utf8')),
historyRequest = JSON.parse(fs.readFileSync(path.join(__dirname, HISTORY_PATH), 'utf8'));
let schemaPack = new Converter.SchemaPack({ type: 'json', data: openapi }, {});
schemaPack.validateTransaction(historyRequest, (err, result) => {
// result is as described in the Schema <> Validation Doc
});