Parameter guards for Firebase calls I have code to ensure parameters existence all over the place. Is there a better way to do it? So that I do not have so many repeated codes inside different functions

export const requestUpdateAccountDetail = (
  firebase,
  uid,
  accountId,
  accountDetail = {}
) =&gt; {
  if (firebase === undefined) {
    throw Error('`firebase` instance is required')
  } else if (uid === undefined) {
    throw Error('`uid is required to update account detail`')
  } else if (accountId === undefined) {
    throw Error('`accountId` is required to update account detail')
  }

  return firebase.update(`/accounts/${uid}/${accountId}`, accountDetail)
}

export const requestAddNewAccount = (firebase, uid) =&gt; {
  if (firebase === undefined) {
    throw Error('`firebase` instance is required')
  } else if (uid === undefined) {
    throw Error('`uid is required to update account detail`')
  }

  const EMPTY_ACCOUNT_OBJ = {
    name: '',
    note: '',
    owner: uid
  }

  return firebase.push(`/accounts/${uid}`, EMPTY_ACCOUNT_OBJ)
}

export const requestDeleteAccount = (firebase, uid, accountId) =&gt; {
  if (firebase === undefined) {
    throw Error('`firebase` instance is required')
  } else if (uid === undefined) {
    throw Error('`uid is required to update account detail`')
  } else if (accountId === undefined) {
    throw Error('`accountId` is required to update account detail')
  }

  return firebase.remove(`/accounts/${uid}/${accountId}`)
}
So given this is a blanket rule across all the functions i.e. val === undefined then throw you could introduce a common validator function you could share across all your functions e.g.

function assertParams(params) {
   for (const prop in params) {
       if (params[prop] === undefined) {
           throw new Error(`Argument ${prop} is undefined`);
       }
   }
}
...
export const function requestAddNew(firebase, uid, accountId) {
    assertParams({ firebase, uid, accountId });
    ...
}
export const function requestDeleteAccount(firebase, uid, accountId) {
    assertParams({ firebase, uid, accountId });
    ...
}


you'd need to call it in every function, however, this would give you the same effect of what you already have in one line as opposed to X lines based on the number of parameters you have.

If your using Babel, something to look at would be perhaps moving it to a decorator so it's completely outside the scope of your logic e.g.

@assertParams()
export const function requestAddNew(...)


The above code wouldn't just "work" in the decorator, would need a little bit of tweaking but I'm pretty sure it's doable.