Created
August 4, 2017 02:02
-
-
Save the-vampiire/fe3f0543e19ccb718d380c5b28b7f7fc to your computer and use it in GitHub Desktop.
errorScan and verifyKeys functions from valStringer
This file contains hidden or 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
| // 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