Skip to content

Instantly share code, notes, and snippets.

@adadgio
Last active May 12, 2021 14:55
Show Gist options
  • Save adadgio/6bd3b675ca3794d03f5083dcb65ea474 to your computer and use it in GitHub Desktop.
Save adadgio/6bd3b675ca3794d03f5083dcb65ea474 to your computer and use it in GitHub Desktop.
// In some script
if (!has(['username', 'password'], req.body) {
// console.log('Request body: <username> and <password> are required')
}
// Function in file: has.ts
export const NOT_EMPTY = true
export function has(thoseKeys: Array<string>, object: any, cantBeEmpty: boolean = false)
{
let isValid = true
for (let key of thoseKeys) {
if (!object.hasOwnProperty(key)) {
isValid = false
break
}
if (cantBeEmpty && (object[key] === null || object[key] === '') || object[key] === 'undefined' || object[key] === undefined) {
isValid = false
break
}
}
return isValid
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment