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