Skip to content

Commit 4b540f9

Browse files
Remove JS Input Validation (#3939)
1 parent a5768b0 commit 4b540f9

File tree

13 files changed

+114
-1062
lines changed

13 files changed

+114
-1062
lines changed

.changeset/silver-dolls-wave.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"firebase": major
3+
"@firebase/firestore": major
4+
---
5+
6+
This releases removes all input validation. Please use our TypeScript types to validate API usage.

packages/firestore/exp/src/api/reference.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
parseUpdateVarargs
2525
} from '../../../src/api/user_data_reader';
2626
import { debugAssert } from '../../../src/util/assert';
27-
import { cast } from '../../../lite/src/api/util';
27+
import { cast } from '../../../src/util/input_validation';
2828
import { DocumentSnapshot, QuerySnapshot } from './snapshot';
2929
import {
3030
applyFirestoreDataConverter,

packages/firestore/exp/test/shim.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ import {
6565
} from '../../exp/index';
6666
import { UntypedFirestoreDataConverter } from '../../src/api/user_data_reader';
6767
import { isPartialObserver, PartialObserver } from '../../src/api/observer';
68-
import { isPlainObject } from '../../src/util/input_validation';
68+
import {
69+
isPlainObject,
70+
validateSetOptions
71+
} from '../../src/util/input_validation';
6972
import { Compat } from '../../src/compat/compat';
7073

7174
export { GeoPoint, Timestamp } from '../index';
@@ -193,6 +196,7 @@ export class Transaction
193196
options?: legacy.SetOptions
194197
): Transaction {
195198
if (options) {
199+
validateSetOptions('Transaction.set', options);
196200
this._delegate.set(documentRef._delegate, unwrap(data), options);
197201
} else {
198202
this._delegate.set(documentRef._delegate, unwrap(data));
@@ -245,6 +249,7 @@ export class WriteBatch
245249
options?: legacy.SetOptions
246250
): WriteBatch {
247251
if (options) {
252+
validateSetOptions('WriteBatch.set', options);
248253
this._delegate.set(documentRef._delegate, unwrap(data), options);
249254
} else {
250255
this._delegate.set(documentRef._delegate, unwrap(data));
@@ -324,6 +329,7 @@ export class DocumentReference<T = legacy.DocumentData>
324329

325330
set(data: Partial<T>, options?: legacy.SetOptions): Promise<void> {
326331
if (options) {
332+
validateSetOptions('DocumentReference.set', options);
327333
return setDoc(this._delegate, unwrap(data), options);
328334
} else {
329335
return setDoc(this._delegate, unwrap(data));

packages/firestore/lite/src/api/field_value.ts

-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { validateAtLeastNumberOfArgs } from '../../../src/util/input_validation';
1918
import {
2019
ArrayRemoveFieldValueImpl,
2120
ArrayUnionFieldValueImpl,
@@ -69,7 +68,6 @@ export function serverTimestamp(): FieldValue {
6968
* `updateDoc()`.
7069
*/
7170
export function arrayUnion(...elements: unknown[]): FieldValue {
72-
validateAtLeastNumberOfArgs('arrayUnion()', arguments, 1);
7371
// NOTE: We don't actually parse the data until it's used in set() or
7472
// update() since we'd need the Firestore instance to do this.
7573
return new ArrayUnionFieldValueImpl('arrayUnion', elements);
@@ -87,7 +85,6 @@ export function arrayUnion(...elements: unknown[]): FieldValue {
8785
* `updateDoc()`
8886
*/
8987
export function arrayRemove(...elements: unknown[]): FieldValue {
90-
validateAtLeastNumberOfArgs('arrayRemove()', arguments, 1);
9188
// NOTE: We don't actually parse the data until it's used in set() or
9289
// update() since we'd need the Firestore instance to do this.
9390
return new ArrayRemoveFieldValueImpl('arrayRemove', elements);

packages/firestore/lite/src/api/reference.ts

+3-21
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ import { FieldPath } from './field_path';
7373
import {
7474
validateCollectionPath,
7575
validateDocumentPath,
76-
validateExactNumberOfArgs,
76+
validateNonEmptyArgument,
7777
validatePositiveNumber
7878
} from '../../../src/util/input_validation';
7979
import { newSerializer } from '../../../src/platform/serializer';
@@ -335,8 +335,6 @@ export function where(
335335
opStr: WhereFilterOp,
336336
value: unknown
337337
): QueryConstraint {
338-
// TODO(firestorelite): Consider validating the enum strings (note that
339-
// TypeScript does not support passing invalid values).
340338
const op = opStr as Operator;
341339
const field = fieldPathFromArgument('where', fieldPath);
342340
return new QueryFilterConstraint(field, op, value);
@@ -381,8 +379,6 @@ export function orderBy(
381379
fieldPath: string | FieldPath,
382380
directionStr: OrderByDirection = 'asc'
383381
): QueryConstraint {
384-
// TODO(firestorelite): Consider validating the enum strings (note that
385-
// TypeScript does not support passing invalid values).
386382
const direction = directionStr as Direction;
387383
const path = fieldPathFromArgument('orderBy', fieldPath);
388384
return new QueryOrderByConstraint(path, direction);
@@ -413,7 +409,7 @@ class QueryLimitConstraint extends QueryConstraint {
413409
* @return The created `Query`.
414410
*/
415411
export function limit(limit: number): QueryConstraint {
416-
validatePositiveNumber('limit', 1, limit);
412+
validatePositiveNumber('limit', limit);
417413
return new QueryLimitConstraint('limit', limit, LimitType.First);
418414
}
419415

@@ -427,7 +423,7 @@ export function limit(limit: number): QueryConstraint {
427423
* @return The created `Query`.
428424
*/
429425
export function limitToLast(limit: number): QueryConstraint {
430-
validatePositiveNumber('limitToLast', 1, limit);
426+
validatePositiveNumber('limitToLast', limit);
431427
return new QueryLimitConstraint('limitToLast', limit, LimitType.Last);
432428
}
433429

@@ -597,7 +593,6 @@ function newQueryBoundFromDocOrFields<T>(
597593
before: boolean
598594
): Bound {
599595
if (docOrFields[0] instanceof DocumentSnapshot) {
600-
validateExactNumberOfArgs(methodName, docOrFields, 1);
601596
return newQueryBoundFromDocument(
602597
query._query,
603598
query.firestore._databaseId,
@@ -1234,16 +1229,3 @@ export function newUserDataReader(
12341229
serializer
12351230
);
12361231
}
1237-
1238-
function validateNonEmptyArgument(
1239-
functionName: string,
1240-
argumentName: string,
1241-
argument?: string
1242-
): asserts argument is string {
1243-
if (!argument) {
1244-
throw new FirestoreError(
1245-
Code.INVALID_ARGUMENT,
1246-
`Function ${functionName}() cannot be called with an empty ${argumentName}.`
1247-
);
1248-
}
1249-
}

packages/firestore/lite/src/api/util.ts

-46
This file was deleted.

packages/firestore/src/api/blob.ts

-13
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717

1818
import { isBase64Available } from '../platform/base64';
1919
import { Code, FirestoreError } from '../util/error';
20-
import {
21-
invalidClassError,
22-
validateArgType,
23-
validateExactNumberOfArgs
24-
} from '../util/input_validation';
2520
import { ByteString } from '../util/byte_string';
2621
import { Bytes } from '../../lite/src/api/bytes';
2722

@@ -57,8 +52,6 @@ function assertBase64Available(): void {
5752
*/
5853
export class Blob extends Bytes {
5954
static fromBase64String(base64: string): Blob {
60-
validateExactNumberOfArgs('Blob.fromBase64String', arguments, 1);
61-
validateArgType('Blob.fromBase64String', 'string', 1, base64);
6255
assertBase64Available();
6356
try {
6457
return new Blob(ByteString.fromBase64String(base64));
@@ -71,22 +64,16 @@ export class Blob extends Bytes {
7164
}
7265

7366
static fromUint8Array(array: Uint8Array): Blob {
74-
validateExactNumberOfArgs('Blob.fromUint8Array', arguments, 1);
7567
assertUint8ArrayAvailable();
76-
if (!(array instanceof Uint8Array)) {
77-
throw invalidClassError('Blob.fromUint8Array', 'Uint8Array', 1, array);
78-
}
7968
return new Blob(ByteString.fromUint8Array(array));
8069
}
8170

8271
toBase64(): string {
83-
validateExactNumberOfArgs('Blob.toBase64', arguments, 0);
8472
assertBase64Available();
8573
return super.toBase64();
8674
}
8775

8876
toUint8Array(): Uint8Array {
89-
validateExactNumberOfArgs('Blob.toUint8Array', arguments, 0);
9077
assertUint8ArrayAvailable();
9178
return super.toUint8Array();
9279
}

0 commit comments

Comments
 (0)