Created
May 5, 2019 13:22
-
-
Save pratyushcrd/07eccc5a19d5dcf65a4ca9c80744ae4e to your computer and use it in GitHub Desktop.
This file contains 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
module.exports = { | |
/** | |
* Function to verify all validations | |
* @param {...any} paramsRaw An array of validation results, | |
* Values can either be `true` or `error string`. | |
*/ | |
all: async function (...paramsRaw) { | |
const params = await Promise.all(paramsRaw) | |
const error = params | |
.filter(val => typeof val === 'string')[0] | |
if (error) { | |
return Promise.reject(error) | |
} | |
}, | |
maxLength: function (maxLen, paramOb) { | |
const failedEntries = Object.keys(paramOb) | |
.filter(key => String(paramOb[key] || '').length > maxLen) | |
if (failedEntries.length) { | |
const errorMessage = `'${failedEntries.join(', ')}' exceed the limit` | |
return errorMessage | |
} | |
return true | |
}, | |
notEmpty: function (paramOb) { | |
const failedEntries = Object.keys(paramOb) | |
.filter(key => !paramOb[key]) | |
if (failedEntries.length) { | |
const errorMessage = `'${failedEntries.join(', ')}' cannot be empty` | |
return errorMessage | |
} | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment