Skip to content

Instantly share code, notes, and snippets.

@the-vampiire
Created August 4, 2017 02:02
Show Gist options
  • Select an option

  • Save the-vampiire/fe3f0543e19ccb718d380c5b28b7f7fc to your computer and use it in GitHub Desktop.

Select an option

Save the-vampiire/fe3f0543e19ccb718d380c5b28b7f7fc to your computer and use it in GitHub Desktop.
errorScan and verifyKeys functions from valStringer
// scans a custom attachment object for errors
errorScan = (type, customAttachment) => {
const expectedKeys = ['text', 'callback_id', 'actions'];
const expectedSubKeys = type === 'menu' ? ['name', 'type', 'data_source'] : ['text', 'name', 'type'];
const keysError = verifyKeys(customAttachment, expectedKeys, 'outer attachment properties');
if(keysError){
return keysError;
}
const actions = customAttachment.actions[0];
const subKeysError = verifyKeys(actions, expectedSubKeys, 'actions array object');
if(subKeysError){
return subKeysError;
}
else {
switch(type){
case 'menu':
switch(true) {
case actions.type !== 'select':
return `invalid custom attachment: action type must be "select" for interactive menus`;
case actions.data_source !== 'static':
return `invalid custom attachment: data_source must be "static" for interactive menus`;
}
break;
case 'button':
switch(true){
case actions.type !== 'button':
return `invalid custom attachment: action type must be "button" for interactive buttons`;
}
break;
}
}
return false;
};
// verifies the keys of a custom attachment object
verifyKeys = (object, expectedKeys, location) => {
let error;
expectedKeys.forEach( e => {
if(!object.hasOwnProperty(e)){
error = `invalid custom attachment: missing the property "${e}" in the ${location}`;
}
});
return error ? error : false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment