If you want to write this style of code, I'd prefer using cond from lodash which reimplements cond from Lisp.
constvalidateInput=_.cond([[({field1})=>!field1,Promise.reject('Missing field 1')],[({field2})=>!Array.isArray(field2),Promise.reject('field2 is not an array')],[({field3})=>!isValidType(field3),Promise.reject('field3 is invalid')],[_.stubTrue,Promise.resolve()]]);
Edit: didn't realise it was an object, not an array.
Could even do something like this:
constisMissing=key=>obj=>!obj[key];constnotArray=key=>obj=>!Array.isArray(obj[key]);constnotType=(type,key)=>obj=>typeofobj[key]!==type;constvalidateInput=_.cond([[isMissing('field1'),Promise.reject('Missing field 1')],[notArray('field2'),Promise.reject('field2 is not an array')],[notType('string','field3'),Promise.reject('field3 is invalid')],[_.stubTrue,Promise.resolve()]]);
If you want to write this style of code, I'd prefer using cond from
lodash
which reimplementscond
from Lisp.Edit: didn't realise it was an object, not an array.
Could even do something like this:
This is a great suggestion, I actually ended up doing something similar with Ramda's cond