mirror of
https://github.com/postmanlabs/openapi-to-postman.git
synced 2022-11-29 22:05:00 +03:00
New SchemaPack API exposed, validatorAgainstHistory v1
This commit is contained in:
61
index.js
61
index.js
@@ -1,57 +1,28 @@
|
||||
var converter = require('./lib/convert.js'),
|
||||
validate = require('./lib/validate.js'),
|
||||
fs = require('fs');
|
||||
'use strict';
|
||||
|
||||
const SchemaPack = require('./lib/schemapack.js').SchemaPack;
|
||||
|
||||
module.exports = {
|
||||
// Old API wrapping the new API
|
||||
convert: function(input, options, cb) {
|
||||
if (input.type === 'string' || input.type === 'json') {
|
||||
// no need for extra processing before calling the converter
|
||||
// string can be JSON or YAML
|
||||
return converter.convert(input.data, options, cb);
|
||||
}
|
||||
else if (input.type === 'file') {
|
||||
return fs.readFile(input.data, 'utf8', function(err, data) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
var schema = new SchemaPack(input);
|
||||
|
||||
// if the file contents were JSON or YAML
|
||||
return converter.convert(data, options, cb);
|
||||
});
|
||||
if (schema.validated) {
|
||||
return schema.convert(options, cb);
|
||||
}
|
||||
return cb(null, {
|
||||
result: false,
|
||||
reason: 'input type:' + input.type + ' is not valid'
|
||||
});
|
||||
|
||||
return cb(null, schema.validationResult);
|
||||
},
|
||||
|
||||
validate: function(input) {
|
||||
try {
|
||||
var data;
|
||||
if (input.type === 'string') {
|
||||
return validate(input.data);
|
||||
}
|
||||
else if (input.type === 'json') {
|
||||
return validate(input.data);
|
||||
}
|
||||
else if (input.type === 'file') {
|
||||
data = fs.readFileSync(input.data).toString();
|
||||
return validate(data);
|
||||
}
|
||||
return {
|
||||
result: false,
|
||||
reason: 'input type is not valid'
|
||||
};
|
||||
}
|
||||
catch (e) {
|
||||
return {
|
||||
result: false,
|
||||
reason: e.toString()
|
||||
};
|
||||
}
|
||||
var schema = new SchemaPack(input);
|
||||
return schema.validationResult;
|
||||
},
|
||||
|
||||
getOptions: function() {
|
||||
return converter.getOptions();
|
||||
}
|
||||
return SchemaPack.getOptions();
|
||||
},
|
||||
|
||||
// new API
|
||||
SchemaPack
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user