|
| 1 | +/** |
| 2 | + * USAGE: call this file using node and pass in directory with configs or config file |
| 3 | + * i.e. node config_validator.js example.com.json |
| 4 | + * i.e. node config_validator.js configs |
| 5 | + * |
| 6 | + * Validates shortcut JSON config |
| 7 | + * More details found here: https://paper.dropbox.com/doc/JSON-Data-Format-XaQ21RmPGZS778W2LKza2 |
| 8 | + */ |
| 9 | + |
| 10 | +let fs = require('fs'); |
| 11 | +let path = require('path'); |
| 12 | + |
| 13 | +const SUPPORTED_OS = ['default', 'windows', 'macos', 'linux', 'ubuntu']; |
| 14 | +const ALPHABET_KEYS = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', |
| 15 | + 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; |
| 16 | +const NUMBER_KEYS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; |
| 17 | +const SPECIAL_KEYS = ['`', 'esc', 'tab', 'caps', 'shift', 'fn', 'ctrl', 'alt', 'cmd', '-', '_', '=', '+', '?', 'back', 'delete', |
| 18 | + 'enter', '[', ']', '\\', ';', "'", ',', '.', '/', 'up', 'down', 'left', 'right', 'home', 'end', |
| 19 | + 'pg dn', 'pg up', 'win', 'space']; |
| 20 | +const SUPPORTED_KEYS = ALPHABET_KEYS.concat(NUMBER_KEYS, SPECIAL_KEYS); |
| 21 | + |
| 22 | +main(); |
| 23 | + |
| 24 | +function main() { |
| 25 | + let pathArg = process.argv[2]; |
| 26 | + let configArray = readInConfigs(pathArg); |
| 27 | + configArray.forEach((config) => { |
| 28 | + let res = validate(config.data); |
| 29 | + if (res.status === 'success') { |
| 30 | + console.log(config.path + ': success\n'); |
| 31 | + } else { |
| 32 | + console.log(config.path + ': error: ' + res.error + '\n'); |
| 33 | + } |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +function readInConfigs(pathArg) { |
| 38 | + let configArray = []; |
| 39 | + if (fs.lstatSync(pathArg).isDirectory()) { |
| 40 | + files = fs.readdirSync(pathArg); |
| 41 | + files.forEach((file) => { |
| 42 | + configArray = configArray.concat(readInConfigs(path.join(pathArg, file))); |
| 43 | + }); |
| 44 | + } else { |
| 45 | + let data = JSON.parse(fs.readFileSync(pathArg, 'utf8')); |
| 46 | + configArray.push({path: pathArg, data: data}); |
| 47 | + } |
| 48 | + return configArray; |
| 49 | +} |
| 50 | + |
| 51 | +function validate(data) { |
| 52 | + try { |
| 53 | + validateConfig(data); |
| 54 | + return {status: 'success'}; |
| 55 | + } catch (err) { |
| 56 | + return {status: 'error', error: err}; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +function validateConfig(config) { |
| 61 | + numKeys = Object.keys(config).length; |
| 62 | + if (numKeys > 3) throw 'more than 3 keys in config'; |
| 63 | + if (numKeys === 3) validateStringProp(config, 'description'); |
| 64 | + validateStringProp(config, 'name'); |
| 65 | + validateArrayProp(config, 'sections'); |
| 66 | + validateSections(config.sections); |
| 67 | +} |
| 68 | + |
| 69 | +function validateSections(sections) { |
| 70 | + if (sections.length === 0) { |
| 71 | + return; |
| 72 | + } |
| 73 | + section = sections[0]; |
| 74 | + numKeys = Object.keys(section).length; |
| 75 | + if (numKeys > 3) throw 'more than 3 keys in section'; |
| 76 | + if (numKeys === 3) validateStringProp(section, 'description'); |
| 77 | + validateStringProp(section, 'name'); |
| 78 | + validateArrayProp(section, 'shortcuts'); |
| 79 | + validateShortcuts(section.shortcuts); |
| 80 | + validateSections(sections.slice(1, sections.length)); |
| 81 | +} |
| 82 | + |
| 83 | +function validateShortcuts(shortcuts) { |
| 84 | + if (shortcuts.length === 0) { |
| 85 | + return; |
| 86 | + } |
| 87 | + shortcut = shortcuts[0]; |
| 88 | + numKeys = Object.keys(shortcut).length; |
| 89 | + if (numKeys > 3) throw 'more than 3 keys in shortcut'; |
| 90 | + if (numKeys === 3) validateStringProp(shortcut, 'description'); |
| 91 | + validateStringProp(shortcut, 'name'); |
| 92 | + validateArrayProp(shortcut, 'keys'); |
| 93 | + validateKeys(shortcut.keys); |
| 94 | + validateShortcuts(shortcuts.slice(1, shortcuts.length)); |
| 95 | +} |
| 96 | + |
| 97 | +function validateKeys(keys) { |
| 98 | + if (keys.length === 0) { |
| 99 | + return; |
| 100 | + } |
| 101 | + key = keys[0]; |
| 102 | + validateOSProp(key, 'default'); |
| 103 | + for (var os in key) { |
| 104 | + if (SUPPORTED_OS.indexOf(os) > -1) validateOSProp(key, os); |
| 105 | + else throw 'unsupported os: ' + os + 'in object: ' + JSON.stringify(key); |
| 106 | + } |
| 107 | + validateKeys(keys.slice(1, keys.length)); |
| 108 | +} |
| 109 | + |
| 110 | +// Helper functions |
| 111 | + |
| 112 | +function validateArrayProp(obj, prop) { |
| 113 | + if (!obj.hasOwnProperty(prop)) throw 'missing prop: ' + prop + ' in object: ' + JSON.stringify(obj); |
| 114 | + if (!(obj[prop] instanceof Array)) throw 'expected array for prop: ' + prop + ' in object: ' + JSON.stringify(obj); |
| 115 | + if (obj[prop].length === 0) throw 'array size === 0 for prop: ' + prop + ' in object: ' + JSON.stringify(obj); |
| 116 | +} |
| 117 | + |
| 118 | +function validateStringProp(obj, prop) { |
| 119 | + if (!obj.hasOwnProperty(prop)) throw 'missing prop: ' + prop + ' in object: ' + JSON.stringify(obj); |
| 120 | + if (!isValidString(obj[prop])) throw 'invalid string for prop: ' + prop + ' in object: ' + JSON.stringify(obj); |
| 121 | +} |
| 122 | + |
| 123 | +function isValidString(x) { |
| 124 | + return typeof x === 'string' && x === x.toLowerCase(); |
| 125 | +} |
| 126 | + |
| 127 | +function validateOSProp(obj, prop) { |
| 128 | + if (!obj.hasOwnProperty(prop)) throw 'missing prop: ' + prop + ' in object: ' + JSON.stringify(obj); |
| 129 | + if (!(obj[prop] instanceof Array)) throw 'expected type array for prop: ' + prop + ' in object: ' + JSON.stringify(obj); |
| 130 | + for (var i = 0; i < obj[prop].length; i++) { |
| 131 | + var key = obj[prop][i]; |
| 132 | + if (!isValidString(key)) throw 'invalid string: ' + key + ' for os: ' + prop + ' in object: ' + JSON.stringify(obj); |
| 133 | + if (SUPPORTED_KEYS.indexOf(key) === -1) { |
| 134 | + validateIfKeyIsInPlusFormat(key); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +function validateIfKeyIsInPlusFormat(key) { |
| 140 | + let keys = key.split('+'); |
| 141 | + if (keys.length > 1) { |
| 142 | + keys.forEach((k) => { |
| 143 | + if (SUPPORTED_KEYS.indexOf(k) === -1) { |
| 144 | + throw 'unsupported key: ' + k + ' for os: ' + prop + ' in object: ' + JSON.stringify(obj); |
| 145 | + } |
| 146 | + }); |
| 147 | + } else { |
| 148 | + throw 'unsupported key: ' + key + ' for os: ' + prop + ' in object: ' + JSON.stringify(obj); |
| 149 | + } |
| 150 | +} |
| 151 | + |
0 commit comments