| 2 | import { DEPRECATED_NETWORK_IDS, networks } from './defaults' |
| 3 | |
| 4 | export function validateType(options: { |
| 5 | name: string |
| 6 | value: any |
| 7 | type: string |
| 8 | optional?: boolean |
| 9 | customValidation?: (val: any) => boolean |
| 10 | }): never | void { |
| 11 | const { name, value, type, optional, customValidation } = options |
| 12 | |
| 13 | if (!optional && typeof value === 'undefined') { |
| 14 | throw new Error(`"${name}" is required`) |
| 15 | } |
| 16 | |
| 17 | if ( |
| 18 | typeof value !== 'undefined' && |
| 19 | (type === 'array' ? Array.isArray(type) : typeof value !== type) |
| 20 | ) { |
| 21 | throw new Error( |
| 22 | `"${name}" must be of type: ${type}, received type: ${typeof value} from value: ${value}` |
| 23 | ) |
| 24 | } |
| 25 | |
| 26 | if ( |
| 27 | typeof value !== 'undefined' && |
| 28 | customValidation && |
| 29 | !customValidation(value) |
| 30 | ) { |
| 31 | throw new Error(`"${value}" is not a valid "${name}"`) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | export function validateOptions(options: any): never | void { |
| 36 | validateType({ name: 'sdk options', value: options, type: 'object' }) |