Skip to content

Instantly share code, notes, and snippets.

@the-simian
Created February 3, 2017 04:29
Show Gist options
  • Save the-simian/07e9e223d948af5e051da4fb9766d3ab to your computer and use it in GitHub Desktop.
Save the-simian/07e9e223d948af5e051da4fb9766d3ab to your computer and use it in GitHub Desktop.
//take a joi object and return a string that is actually ok to display on a front-end
import _ from 'lodash';
function joiToEnglish(joiErr){
return _
.chain(joiErr.details)
.reduce((collected, detail) => {
collected[detail.context.key] = collected[detail.context.key] || [];
collected[detail.context.key].push(detail.message.replace(`"${detail.context.key}"`, ''));
return collected;
}, {})
.reduce((result, value, key) => {
let message = _.uniq(value).join(', ');
result += `${key} ${message}. `;
return result;
}, '')
.value();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment