Skip to content

Instantly share code, notes, and snippets.

@damiancipolat
Last active December 24, 2019 02:00
Show Gist options
  • Save damiancipolat/8a0563469237944750f3b1546e282eb9 to your computer and use it in GitHub Desktop.
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.
/*
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