Skip to content

Commit fc1c382

Browse files
committed
revert file deletion
1 parent f1d3f74 commit fc1c382

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

packages/util/index.node.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ export * from './src/obj';
3333
export * from './src/query';
3434
export * from './src/sha1';
3535
export * from './src/subscribe';
36+
export * from './src/validation';
3637
export * from './src/utf8';

packages/util/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ export * from './src/obj';
2828
export * from './src/query';
2929
export * from './src/sha1';
3030
export * from './src/subscribe';
31+
export * from './src/validation';
3132
export * from './src/utf8';

packages/util/src/validation.ts

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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

Comments
 (0)