rail
Version:
An enhanced HTTP/RESTful API Client
146 lines (107 loc) • 2.98 kB
JavaScript
;
var Validate = require('mgl-validate');
function ValidatePlugin(rail, options) {
if (!(this instanceof ValidatePlugin)) {
return new ValidatePlugin(rail, options);
}
rail.use('buffer');
rail.use('json');
this._rail = rail;
this._intercept = null;
this.registry = new Validate({
breakOnError: options.breakOnError === false ? false : true
});
var i;
if (options.schemas) {
for (i = 0; i < options.schemas.length; ++i) {
this.registry.addSchema(options.schemas[i]);
}
}
this._setup();
}
module.exports = ValidatePlugin;
ValidatePlugin.prototype._setup = function() {
var self = this;
var rail = this._rail;
rail.on('plugin-configure', function(call, options) {
options.validate = options.validate || {};
if (options.validate.body) {
options.json = true;
}
});
rail.on('plugin-response', function(call, options, response) {
var err;
response.validate = {
headers: null,
body: null
};
if (options.validate.headers) {
err = self._validate(options.validate.headers, response.headers);
if (err) {
if (err.validation) {
response.validate.headers = err.validation;
} else {
call.emit('error', err);
}
}
}
if (options.validate.body) {
call.__intercept('response', self._intercept);
} else if (response.validate.headers === null) {
response.validate = null;
}
});
this._intercept = function(call, options, response) {
self._interceptResponse(call, options, response);
};
};
ValidatePlugin.prototype._interceptResponse = function(call, options, response) {
var err;
if (response.json) {
err = this._validate(options.validate.body, response.json);
if (err) {
if (err.validation) {
response.validate.body = err.validation;
} else {
call.emit('error', err);
}
}
if (response.validate.headers === null && response.validate.body === null) {
response.validate = null;
}
} else {
response.validate.body = [[null, 'object', 'undefined', null]];
}
call.__emit('response', response);
};
ValidatePlugin.prototype._validate = function(schema, data) {
var result, err, id;
if (typeof schema === 'string') {
return this.registry.test(schema, data);
} else if (typeof schema === 'object') {
id = schema.id;
err = this._addSchema(schema);
if (err) {
return err;
}
result = this.registry.test(id, data);
if (result) {
err = new Error('Validation failed');
err.validation = JSON.stringify(result);
err.validation = result;
}
// technically this could throw
this.registry.removeSchema(id);
return err;
} else {
return new Error('Invalid schema');
}
};
ValidatePlugin.prototype._addSchema = function(schema) {
try {
this.registry.addSchema(schema);
} catch (e) {
return e;
}
return;
};