Created
February 3, 2017 04:29
-
-
Save the-simian/07e9e223d948af5e051da4fb9766d3ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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