Last active
December 24, 2019 02:00
-
-
Save damiancipolat/8a0563469237944750f3b1546e282eb9 to your computer and use it in GitHub Desktop.
Avoid use assert in node.js to detect if a function parameters is defined.
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 this example I show how avoid to use assert in nodejs to validate if the parameter is defined. | |
*/ | |
//Star from here. | |
const assert = require('assert'); | |
//Using with assert | |
const sum3 = (a,b,c)=>{ | |
assert(a,'a is required'); | |
assert(b,'b is required'); | |
assert(c,'c is required'); | |
return a+b+c; | |
} | |
//Error c is missing. | |
sum3(1,2); | |
//Move to .... | |
//A function that is call when the parameter is missing. | |
const required = ()=>{throw new Error('Parameter value required');}; | |
//Required parameter validation in arrow functions | |
const sum3 = (a = required(),b = required(), c= required())=>a+b+c; | |
//Parameter value required. | |
sum3(1,2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment