|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2017 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * Check to make sure the appropriate number of arguments are provided for a public function. |
| 20 | + * Throws an error if it fails. |
| 21 | + * |
| 22 | + * @param fnName The function name |
| 23 | + * @param minCount The minimum number of arguments to allow for the function call |
| 24 | + * @param maxCount The maximum number of argument to allow for the function call |
| 25 | + * @param argCount The actual number of arguments provided. |
| 26 | + */ |
| 27 | +export const validateArgCount = function ( |
| 28 | + fnName: string, |
| 29 | + minCount: number, |
| 30 | + maxCount: number, |
| 31 | + argCount: number |
| 32 | +): void { |
| 33 | + let argError; |
| 34 | + if (argCount < minCount) { |
| 35 | + argError = 'at least ' + minCount; |
| 36 | + } else if (argCount > maxCount) { |
| 37 | + argError = maxCount === 0 ? 'none' : 'no more than ' + maxCount; |
| 38 | + } |
| 39 | + if (argError) { |
| 40 | + const error = |
| 41 | + fnName + |
| 42 | + ' failed: Was called with ' + |
| 43 | + argCount + |
| 44 | + (argCount === 1 ? ' argument.' : ' arguments.') + |
| 45 | + ' Expects ' + |
| 46 | + argError + |
| 47 | + '.'; |
| 48 | + throw new Error(error); |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +/** |
| 53 | + * Generates a string to prefix an error message about failed argument validation |
| 54 | + * |
| 55 | + * @param fnName The function name |
| 56 | + * @param argumentNumber The index of the argument |
| 57 | + * @param optional Whether or not the argument is optional |
| 58 | + * @return The prefix to add to the error thrown for validation. |
| 59 | + */ |
| 60 | +export function errorPrefix( |
| 61 | + fnName: string, |
| 62 | + argumentNumber: number, |
| 63 | + optional: boolean |
| 64 | +): string { |
| 65 | + let argName = ''; |
| 66 | + switch (argumentNumber) { |
| 67 | + case 1: |
| 68 | + argName = optional ? 'first' : 'First'; |
| 69 | + break; |
| 70 | + case 2: |
| 71 | + argName = optional ? 'second' : 'Second'; |
| 72 | + break; |
| 73 | + case 3: |
| 74 | + argName = optional ? 'third' : 'Third'; |
| 75 | + break; |
| 76 | + case 4: |
| 77 | + argName = optional ? 'fourth' : 'Fourth'; |
| 78 | + break; |
| 79 | + default: |
| 80 | + throw new Error( |
| 81 | + 'errorPrefix called with argumentNumber > 4. Need to update it?' |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + let error = fnName + ' failed: '; |
| 86 | + |
| 87 | + error += argName + ' argument '; |
| 88 | + return error; |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * @param fnName |
| 93 | + * @param argumentNumber |
| 94 | + * @param namespace |
| 95 | + * @param optional |
| 96 | + */ |
| 97 | +export function validateNamespace( |
| 98 | + fnName: string, |
| 99 | + argumentNumber: number, |
| 100 | + namespace: string, |
| 101 | + optional: boolean |
| 102 | +): void { |
| 103 | + if (optional && !namespace) { |
| 104 | + return; |
| 105 | + } |
| 106 | + if (typeof namespace !== 'string') { |
| 107 | + //TODO: I should do more validation here. We only allow certain chars in namespaces. |
| 108 | + throw new Error( |
| 109 | + errorPrefix(fnName, argumentNumber, optional) + |
| 110 | + 'must be a valid firebase namespace.' |
| 111 | + ); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +export function validateCallback( |
| 116 | + fnName: string, |
| 117 | + argumentNumber: number, |
| 118 | + // eslint-disable-next-line @typescript-eslint/ban-types |
| 119 | + callback: Function, |
| 120 | + optional: boolean |
| 121 | +): void { |
| 122 | + if (optional && !callback) { |
| 123 | + return; |
| 124 | + } |
| 125 | + if (typeof callback !== 'function') { |
| 126 | + throw new Error( |
| 127 | + errorPrefix(fnName, argumentNumber, optional) + |
| 128 | + 'must be a valid function.' |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +export function validateContextObject( |
| 134 | + fnName: string, |
| 135 | + argumentNumber: number, |
| 136 | + context: unknown, |
| 137 | + optional: boolean |
| 138 | +): void { |
| 139 | + if (optional && !context) { |
| 140 | + return; |
| 141 | + } |
| 142 | + if (typeof context !== 'object' || context === null) { |
| 143 | + throw new Error( |
| 144 | + errorPrefix(fnName, argumentNumber, optional) + |
| 145 | + 'must be a valid context object.' |
| 146 | + ); |
| 147 | + } |
| 148 | +} |
0 commit comments