| 106 | } |
| 107 | |
| 108 | function validateErrorCodes(): void { |
| 109 | // The compiled JavaScript object has both forward and backward mappings of the enum, |
| 110 | // for example: { '0': 'ERROR_CODE_UNSPECIFIED', 'ERROR_CODE_UNSPECIFIED': 0 }. |
| 111 | // This filters out the numeric keys. |
| 112 | const stringKeysOnly = Object.entries(ErrorCode).filter(([key]) => |
| 113 | isNaN(Number(key)), |
| 114 | ); |
| 115 | |
| 116 | let expectedIndex = 0; |
| 117 | for (const [key, index] of stringKeysOnly) { |
| 118 | if (index !== expectedIndex) { |
| 119 | throw new Error( |
| 120 | `Error: ErrorCode enums must be sequentially numbered from 0.`, |
| 121 | ); |
| 122 | } |
| 123 | if (/_\d/.test(key)) { |
| 124 | throw new Error( |
| 125 | `Error: ErrorCode enum ${key} is invalid. No numbers should be preceded with an underscore.`, |
| 126 | ); |
| 127 | } |
| 128 | expectedIndex++; |
| 129 | } |
| 130 | |
| 131 | console.log(`Successfully validated ${expectedIndex} ErrorCode enums.`); |
| 132 | } |
| 133 | |
| 134 | function main() { |
| 135 | validateErrorCodes(); |