Last active
May 12, 2021 14:55
-
-
Save adadgio/6bd3b675ca3794d03f5083dcb65ea474 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
// 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