Skip to content

Instantly share code, notes, and snippets.

@pratyushcrd
Created May 5, 2019 13:22
Show Gist options
  • Save pratyushcrd/07eccc5a19d5dcf65a4ca9c80744ae4e to your computer and use it in GitHub Desktop.
Save pratyushcrd/07eccc5a19d5dcf65a4ca9c80744ae4e to your computer and use it in GitHub Desktop.
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