New SchemaPack API exposed, validatorAgainstHistory v1

This commit is contained in:
Abhijit Kane
2019-12-15 14:51:52 +05:30
parent 7a2ad3da39
commit 3f551fd259
16 changed files with 1406 additions and 783 deletions

View File

@@ -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
};