Skip to content

Commit 0e515c7

Browse files
Remove argument numbers from validation
This PR removes the argument index from validaiton messages, as the database@exp API uses a different argument order.
1 parent 9e7602e commit 0e515c7

File tree

7 files changed

+122
-168
lines changed

7 files changed

+122
-168
lines changed

packages/database/src/api/Reference.ts

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -123,30 +123,27 @@ export class DataSnapshot implements Compat<ExpDataSnapshot> {
123123
/**
124124
* Returns a DataSnapshot of the specified child node's contents.
125125
*
126-
* @param childPathString Path to a child.
126+
* @param path Path to a child.
127127
* @return DataSnapshot for child node.
128128
*/
129-
child(childPathString: string): DataSnapshot {
129+
child(path: string): DataSnapshot {
130130
validateArgCount('DataSnapshot.child', 0, 1, arguments.length);
131131
// Ensure the childPath is a string (can be a number)
132-
childPathString = String(childPathString);
133-
validatePathString('DataSnapshot.child', 1, childPathString, false);
134-
return new DataSnapshot(
135-
this._database,
136-
this._delegate.child(childPathString)
137-
);
132+
path = String(path);
133+
validatePathString('DataSnapshot.child', 'path', path, false);
134+
return new DataSnapshot(this._database, this._delegate.child(path));
138135
}
139136

140137
/**
141138
* Returns whether the snapshot contains a child at the specified path.
142139
*
143-
* @param childPathString Path to a child.
140+
* @param path Path to a child.
144141
* @return Whether the child exists.
145142
*/
146-
hasChild(childPathString: string): boolean {
143+
hasChild(path: string): boolean {
147144
validateArgCount('DataSnapshot.hasChild', 1, 1, arguments.length);
148-
validatePathString('DataSnapshot.hasChild', 1, childPathString, false);
149-
return this._delegate.hasChild(childPathString);
145+
validatePathString('DataSnapshot.hasChild', 'path', path, false);
146+
return this._delegate.hasChild(path);
150147
}
151148

152149
/**
@@ -169,7 +166,7 @@ export class DataSnapshot implements Compat<ExpDataSnapshot> {
169166
*/
170167
forEach(action: (snapshot: DataSnapshot) => boolean | void): boolean {
171168
validateArgCount('DataSnapshot.forEach', 1, 1, arguments.length);
172-
validateCallback('DataSnapshot.forEach', 1, action, false);
169+
validateCallback('DataSnapshot.forEach', 'action', action, false);
173170
return this._delegate.forEach(expDataSnapshot =>
174171
action(new DataSnapshot(this._database, expDataSnapshot))
175172
);
@@ -231,7 +228,7 @@ export class Query implements Compat<QueryImpl> {
231228
context?: object | null
232229
): SnapshotCallback {
233230
validateArgCount('Query.on', 2, 4, arguments.length);
234-
validateCallback('Query.on', 2, callback, false);
231+
validateCallback('Query.on', 'callback', callback, false);
235232

236233
const ret = Query.getCancelAndContextArgs_(
237234
'Query.on',
@@ -267,7 +264,7 @@ export class Query implements Compat<QueryImpl> {
267264
return callback;
268265
default:
269266
throw new Error(
270-
errorPrefix('Query.on', 1, false) +
267+
errorPrefix('Query.on', 'eventType') +
271268
'must be a valid event type = "value", "child_added", "child_removed", ' +
272269
'"child_changed", or "child_moved".'
273270
);
@@ -280,9 +277,9 @@ export class Query implements Compat<QueryImpl> {
280277
context?: object | null
281278
): void {
282279
validateArgCount('Query.off', 0, 3, arguments.length);
283-
validateEventType('Query.off', 1, eventType, true);
284-
validateCallback('Query.off', 2, callback, true);
285-
validateContextObject('Query.off', 3, context, true);
280+
validateEventType('Query.off', eventType, true);
281+
validateCallback('Query.off', 'callback', callback, true);
282+
validateContextObject('Query.off', 'context', context, true);
286283
if (callback) {
287284
const valueCallback: UserCallback = () => {};
288285
valueCallback.userCallback = callback;
@@ -307,12 +304,12 @@ export class Query implements Compat<QueryImpl> {
307304
*/
308305
once(
309306
eventType: string,
310-
userCallback?: SnapshotCallback,
307+
callback?: SnapshotCallback,
311308
failureCallbackOrContext?: ((a: Error) => void) | object | null,
312309
context?: object | null
313310
): Promise<DataSnapshot> {
314311
validateArgCount('Query.once', 1, 4, arguments.length);
315-
validateCallback('Query.once', 2, userCallback, true);
312+
validateCallback('Query.once', 'callback', callback, true);
316313

317314
const ret = Query.getCancelAndContextArgs_(
318315
'Query.on',
@@ -322,12 +319,12 @@ export class Query implements Compat<QueryImpl> {
322319
const deferred = new Deferred<DataSnapshot>();
323320
const valueCallback: UserCallback = (expSnapshot, previousChildName?) => {
324321
const result = new DataSnapshot(this.database, expSnapshot);
325-
if (userCallback) {
326-
userCallback.call(ret.context, result, previousChildName);
322+
if (callback) {
323+
callback.call(ret.context, result, previousChildName);
327324
}
328325
deferred.resolve(result);
329326
};
330-
valueCallback.userCallback = userCallback;
327+
valueCallback.userCallback = callback;
331328
valueCallback.context = ret.context;
332329
const cancelCallback = (error: Error) => {
333330
if (ret.cancel) {
@@ -364,7 +361,7 @@ export class Query implements Compat<QueryImpl> {
364361
break;
365362
default:
366363
throw new Error(
367-
errorPrefix('Query.once', 1, false) +
364+
errorPrefix('Query.once', 'eventType') +
368365
'must be a valid event type = "value", "child_added", "child_removed", ' +
369366
'"child_changed", or "child_moved".'
370367
);
@@ -519,10 +516,10 @@ export class Query implements Compat<QueryImpl> {
519516
} = { cancel: undefined, context: undefined };
520517
if (cancelOrContext && context) {
521518
ret.cancel = cancelOrContext as (a: Error) => void;
522-
validateCallback(fnName, 3, ret.cancel, true);
519+
validateCallback(fnName, 'cancel', ret.cancel, true);
523520

524521
ret.context = context;
525-
validateContextObject(fnName, 4, ret.context, true);
522+
validateContextObject(fnName, 'context', ret.context, true);
526523
} else if (cancelOrContext) {
527524
// we have either a cancel callback or a context.
528525
if (typeof cancelOrContext === 'object' && cancelOrContext !== null) {
@@ -532,7 +529,7 @@ export class Query implements Compat<QueryImpl> {
532529
ret.cancel = cancelOrContext as (a: Error) => void;
533530
} else {
534531
throw new Error(
535-
errorPrefix(fnName, 3, true) +
532+
errorPrefix(fnName, 'cancelOrContext') +
536533
' must either be a cancel callback or a context object.'
537534
);
538535
}
@@ -598,7 +595,7 @@ export class Reference extends Query implements Compat<ReferenceImpl> {
598595
onComplete?: (error: Error | null) => void
599596
): Promise<unknown> {
600597
validateArgCount('Reference.set', 1, 2, arguments.length);
601-
validateCallback('Reference.set', 2, onComplete, true);
598+
validateCallback('Reference.set', 'onComplete', onComplete, true);
602599
const result = set(this._delegate, newVal);
603600
if (onComplete) {
604601
result.then(
@@ -610,17 +607,17 @@ export class Reference extends Query implements Compat<ReferenceImpl> {
610607
}
611608

612609
update(
613-
objectToMerge: object,
610+
values: object,
614611
onComplete?: (a: Error | null) => void
615612
): Promise<unknown> {
616613
validateArgCount('Reference.update', 1, 2, arguments.length);
617614

618-
if (Array.isArray(objectToMerge)) {
615+
if (Array.isArray(values)) {
619616
const newObjectToMerge: { [k: string]: unknown } = {};
620-
for (let i = 0; i < objectToMerge.length; ++i) {
621-
newObjectToMerge['' + i] = objectToMerge[i];
617+
for (let i = 0; i < values.length; ++i) {
618+
newObjectToMerge['' + i] = values[i];
622619
}
623-
objectToMerge = newObjectToMerge;
620+
values = newObjectToMerge;
624621
warn(
625622
'Passing an Array to Firebase.update() is deprecated. ' +
626623
'Use set() if you want to overwrite the existing data, or ' +
@@ -629,9 +626,9 @@ export class Reference extends Query implements Compat<ReferenceImpl> {
629626
);
630627
}
631628
validateWritablePath('Reference.update', this._delegate._path);
632-
validateCallback('Reference.update', 2, onComplete, true);
629+
validateCallback('Reference.update', 'onComplete', onComplete, true);
633630

634-
const result = update(this._delegate, objectToMerge);
631+
const result = update(this._delegate, values);
635632
if (onComplete) {
636633
result.then(
637634
() => onComplete(null),
@@ -647,7 +644,12 @@ export class Reference extends Query implements Compat<ReferenceImpl> {
647644
onComplete?: (a: Error | null) => void
648645
): Promise<unknown> {
649646
validateArgCount('Reference.setWithPriority', 2, 3, arguments.length);
650-
validateCallback('Reference.setWithPriority', 3, onComplete, true);
647+
validateCallback(
648+
'Reference.setWithPriority',
649+
'onComplete',
650+
onComplete,
651+
true
652+
);
651653

652654
const result = setWithPriority(this._delegate, newVal, newPriority);
653655
if (onComplete) {
@@ -661,7 +663,7 @@ export class Reference extends Query implements Compat<ReferenceImpl> {
661663

662664
remove(onComplete?: (a: Error | null) => void): Promise<unknown> {
663665
validateArgCount('Reference.remove', 0, 1, arguments.length);
664-
validateCallback('Reference.remove', 1, onComplete, true);
666+
validateCallback('Reference.remove', 'onComplete', onComplete, true);
665667

666668
const result = remove(this._delegate);
667669
if (onComplete) {
@@ -683,9 +685,19 @@ export class Reference extends Query implements Compat<ReferenceImpl> {
683685
applyLocally?: boolean
684686
): Promise<TransactionResult> {
685687
validateArgCount('Reference.transaction', 1, 3, arguments.length);
686-
validateCallback('Reference.transaction', 1, transactionUpdate, false);
687-
validateCallback('Reference.transaction', 2, onComplete, true);
688-
validateBoolean('Reference.transaction', 3, applyLocally, true);
688+
validateCallback(
689+
'Reference.transaction',
690+
'transactionUpdate',
691+
transactionUpdate,
692+
false
693+
);
694+
validateCallback('Reference.transaction', 'onComplete', onComplete, true);
695+
validateBoolean(
696+
'Reference.transaction',
697+
'applyLocally',
698+
applyLocally,
699+
true
700+
);
689701

690702
const result = runTransaction(this._delegate, transactionUpdate, {
691703
applyLocally
@@ -715,7 +727,7 @@ export class Reference extends Query implements Compat<ReferenceImpl> {
715727
onComplete?: (a: Error | null) => void
716728
): Promise<unknown> {
717729
validateArgCount('Reference.setPriority', 1, 2, arguments.length);
718-
validateCallback('Reference.setPriority', 2, onComplete, true);
730+
validateCallback('Reference.setPriority', 'onComplete', onComplete, true);
719731

720732
const result = setPriority(this._delegate, priority);
721733
if (onComplete) {
@@ -729,7 +741,7 @@ export class Reference extends Query implements Compat<ReferenceImpl> {
729741

730742
push(value?: unknown, onComplete?: (a: Error | null) => void): Reference {
731743
validateArgCount('Reference.push', 0, 2, arguments.length);
732-
validateCallback('Reference.push', 2, onComplete, true);
744+
validateCallback('Reference.push', 'onComplete', onComplete, true);
733745

734746
const expPromise = push(this._delegate, value);
735747
const promise = expPromise.then(

packages/database/src/api/onDisconnect.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class OnDisconnect implements Compat<ExpOnDisconnect> {
2626

2727
cancel(onComplete?: (a: Error | null) => void): Promise<void> {
2828
validateArgCount('OnDisconnect.cancel', 0, 1, arguments.length);
29-
validateCallback('OnDisconnect.cancel', 1, onComplete, true);
29+
validateCallback('OnDisconnect.cancel', 'onComplete', onComplete, true);
3030
const result = this._delegate.cancel();
3131
if (onComplete) {
3232
result.then(
@@ -39,7 +39,7 @@ export class OnDisconnect implements Compat<ExpOnDisconnect> {
3939

4040
remove(onComplete?: (a: Error | null) => void): Promise<void> {
4141
validateArgCount('OnDisconnect.remove', 0, 1, arguments.length);
42-
validateCallback('OnDisconnect.remove', 1, onComplete, true);
42+
validateCallback('OnDisconnect.remove', 'onComplete', onComplete, true);
4343
const result = this._delegate.remove();
4444
if (onComplete) {
4545
result.then(
@@ -52,7 +52,7 @@ export class OnDisconnect implements Compat<ExpOnDisconnect> {
5252

5353
set(value: unknown, onComplete?: (a: Error | null) => void): Promise<void> {
5454
validateArgCount('OnDisconnect.set', 1, 2, arguments.length);
55-
validateCallback('OnDisconnect.set', 2, onComplete, true);
55+
validateCallback('OnDisconnect.set', 'onComplete', onComplete, true);
5656
const result = this._delegate.set(value);
5757
if (onComplete) {
5858
result.then(
@@ -69,7 +69,12 @@ export class OnDisconnect implements Compat<ExpOnDisconnect> {
6969
onComplete?: (a: Error | null) => void
7070
): Promise<void> {
7171
validateArgCount('OnDisconnect.setWithPriority', 2, 3, arguments.length);
72-
validateCallback('OnDisconnect.setWithPriority', 3, onComplete, true);
72+
validateCallback(
73+
'OnDisconnect.setWithPriority',
74+
'onComplete',
75+
onComplete,
76+
true
77+
);
7378
const result = this._delegate.setWithPriority(value, priority);
7479
if (onComplete) {
7580
result.then(
@@ -96,7 +101,7 @@ export class OnDisconnect implements Compat<ExpOnDisconnect> {
96101
'existing data, or an Object with integer keys if you really do want to only update some of the children.'
97102
);
98103
}
99-
validateCallback('OnDisconnect.update', 2, onComplete, true);
104+
validateCallback('OnDisconnect.update', 'onComplete', onComplete, true);
100105
const result = this._delegate.update(objectToMerge);
101106
if (onComplete) {
102107
result.then(

0 commit comments

Comments
 (0)