Skip to content

Commit

Permalink
snippets can be injected
Browse files Browse the repository at this point in the history
  • Loading branch information
orangewise committed Oct 10, 2016
1 parent b6f35eb commit 736717b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 24 deletions.
10 changes: 6 additions & 4 deletions bin/rotan
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ program

program
.usage('<command> <options>')
.command('json <schema.json> <input.json or input.yaml>')
.command('json <schema.json> <input.json/yaml>')
.option('-x, --xunit', 'Output xunit.')
.option('-i, --inject <path>', 'Inject yaml/json snippets from <path> before validation.')
.description('Validate if input file conforms to JSON schema.')
.action(function (schemaFile, jsonFile, options) {
var opts = options;
Expand Down Expand Up @@ -63,9 +64,10 @@ program
program.on('--help', function () {
console.log(' Examples:');
console.log('');
console.log(' $ rotan openapi <openapi.json>');
console.log(' $ rotan json <schema.json> <input.json or input.yaml>');
console.log(' $ rotan js <tape-test.js>');
console.log(' $ rotan openapi api.json');
console.log(' $ rotan json schema.json input.json');
console.log(' $ rotan json schema.json input.yaml -i \'snippets/*.yaml\'');
console.log(' $ rotan js tape-test.js');
console.log('');
});

Expand Down
22 changes: 20 additions & 2 deletions lib/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ var glob = require('glob');
var sortObj = require('sort-object');
var util = require('./util.js');

exports.array = function (arr) {

exports.array = array = function (arr) {
merged = {};
arr.forEach(function (a) {
merged = merge(merged, a);
Expand All @@ -16,7 +17,7 @@ exports.array = function (arr) {

// Read files at a certain location,
// return them as an array of json objects.
exports.readFiles = function (pattern, cb) {
exports.readFiles = readFiles = function (pattern, cb) {
var arr = [];
glob(join(process.cwd(), pattern), function (e, files) {
var fileCount = files.length;
Expand All @@ -36,6 +37,23 @@ exports.readFiles = function (pattern, cb) {
});
};

// Inject snippets if options.inject is used, else return api.
exports.snippets = function (api, options, cb) {
if (options.inject) {
// console.log('inject before doing json-schema validation');
readFiles(options.inject, function (e, injectJson) {
// Add api to injectJson
injectJson.push(api);
var merged = array(injectJson);
return cb(null, merged);
});
} else {
process.nextTick(function () {
return cb(null, api);
});
}
};

// var writeToFile = function (mergedApiFile, mergedApi) {
// fs.writeFile(mergedApiFile, JSON.stringify(mergedApi, null, 2), function (err) {
// if (err) {
Expand Down
40 changes: 24 additions & 16 deletions lib/json-tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var util = require('util');
var ZSchema = require('z-schema');
var util2 = require('./util.js');
var inject = require('./inject.js');

ZSchema = new ZSchema({
breakOnFirstError: true,
Expand All @@ -9,7 +10,10 @@ ZSchema = new ZSchema({
reportPathAsArray: true
});



exports.test = function (options) {

var test = options.tape.createHarness();
var stream = test.createStream();

Expand All @@ -21,25 +25,29 @@ exports.test = function (options) {
} else {
t.pass(options.jsonFile + ' is valid');

util2.fileContents(options.schemaFile, function (e, schema) {
if (e) {
t.fail(options.schemaFile + ' is NOT valid JSON');
} else {
t.pass(options.schemaFile + ' is valid JSON');
// Inject if necessary.
inject.snippets(json, options, function (e, injected) {
util2.fileContents(options.schemaFile, function (e, schema) {

var isValid;
if (json && schema) {
isValid = ZSchema.validate(json, schema);
}
if (isValid) {
t.pass(options.jsonFile + ' is conform ' + options.schemaFile);
if (e) {
t.fail(options.schemaFile + ' is NOT valid JSON');
} else {
var err = ZSchema.getLastError();
var msg = util.inspect(err, { depth: 20 });
t.fail(options.jsonFile + ' is NOT conform ' + options.schemaFile + ': '+ msg);

t.pass(options.schemaFile + ' is valid JSON');
var isValid;
if (injected && schema) {
isValid = ZSchema.validate(injected, schema);
}
if (isValid) {
t.pass(options.jsonFile + ' is conform ' + options.schemaFile);
} else {
var err = ZSchema.getLastError();
var msg = util.inspect(err, { depth: 20 });
t.fail(options.jsonFile + ' is NOT conform ' + options.schemaFile + ': '+ msg);
}
t.end();
}
t.end();
}
});
});
}
});
Expand Down
6 changes: 4 additions & 2 deletions test/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ test('return folder as array', function (t) {
});

test('openapi is still valid after injection', function (t) {

inject.readFiles('./test/fixtures/inject/*.yaml', function (e, injectJson) {
util.fileContents('./test/fixtures/swagger-base.yaml', function (e, api) {
// Add api to injectJson
Expand All @@ -37,5 +36,8 @@ test('openapi is still valid after injection', function (t) {
});

test('inject cors into swagger', function (t) {
t.end();
inject.snippets({ }, { inject: './test/fixtures/inject/*.yaml' }, function (e, injected) {
t.deepEqual(injected, cors, 'noooo');
t.end();
});
});

0 comments on commit 736717b

Please sign in to comment.