logzilla
Version:
Node.js Logging Adapter for Devebot
35 lines (34 loc) • 767 B
JavaScript
;
var Constructor = function (params) {
params = params || {};
var Ajv = requireSafe('ajv');
var validator = Ajv ? new Ajv(Object.assign({
allErrors: true
}, params.options)) : null;
this.validate = function (data, schema, opts) {
var output = {
ok: true,
hasErrors: false,
skipped: false
};
if (!validator) {
output.skipped = true;
return output;
}
var valid = validator.validate(schema, data);
if (!valid) {
output.ok = false;
output.hasErrors = true;
output.errors = validator.errors;
}
return output;
};
};
function requireSafe(packageName) {
try {
return require(packageName);
} catch (err) {
return null;
}
}
module.exports = Constructor;