json-schema-ngp-van-error-creator
Version:
Generates NGP VAN API compliant error messages from json schema
32 lines (27 loc) • 774 B
JavaScript
var _ = require('lodash');
/**
* maps a uri returned by JSV to a human readable property name.
**/
function getPropertyFromUri(uri) {
function formatArrayIndex(propertyPath) {
return propertyPath.replace(/\.([0-9]+)\b/, function(match, index) {
return '['+index+']';
});
}
function getPropertyPath(uri) {
return uri.match(new RegExp('#/(.+)$'))[1].replace('/', '.');
}
return formatArrayIndex(getPropertyPath(uri));
}
function createErrorResponse(errors) {
return { code: 400,
errors: _.map(errors, function(error) {
return {
properties: [ getPropertyFromUri(error.uri) ],
text: error.message + ': ' + error.details,
code: "INVALID_PARAMETER"
};
})
};
}
module.exports = createErrorResponse;