- Documentation for
ErrorBuilder.addTransform()
- create a definition file e.g.
/definitions/transformations.js
ErrorBuilder.addTransform('toObject', function(isResponse) {
var builder = {};
for (var i = 0, length = this.items.length; i < length; i++) {
var err = this.items[i];
// err.name
// err.error
// err.path
// err.index
builder[err.name] = err.error;
}
// HTTP status (if the schema is used in the routing)
this.status = 400;
// Returns a new transformed object
return isResponse ? JSON.stringify(builder) : builder;
});
// Sets the new transformation as a default output for all ErrorBuilders
ErrorBuilder.setDefaultTransform('toObject');
Tests:
var err = new ErrorBuilder();
err.push('email', 'Email is not valid');
err.push('age', 'You are too young');
err.push('something'); // error message will be read from a resource filfe
console.log(err.output());
// { email: 'Email is not valid', age: 'You are too young' }
Yes. You can create unlimited count of transformations.
ErrorBuilder.addTransform('csv', function() {
// ...
});
ErrorBuilder.addTransform('xml', function() {
// ...
});
Usage:
var err = new ErrorBuilder();
err.push('email', 'Email is not valid');
err.push('age', 'You are too young');
err.setTransform('xml');
// OR
var err = new ErrorBuilder();
err.push('email', 'Email is not valid');
err.push('age', 'You are too young');
err.setTransform('csv');