Skip to content

Commit

Permalink
Remove JS Input Validation
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Oct 14, 2020
1 parent 00c963b commit ff78c48
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 944 deletions.
3 changes: 0 additions & 3 deletions packages/firestore/lite/src/api/field_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* limitations under the License.
*/

import { validateAtLeastNumberOfArgs } from '../../../src/util/input_validation';
import {
ArrayRemoveFieldValueImpl,
ArrayUnionFieldValueImpl,
Expand Down Expand Up @@ -69,7 +68,6 @@ export function serverTimestamp(): FieldValue {
* `updateDoc()`.
*/
export function arrayUnion(...elements: unknown[]): FieldValue {
validateAtLeastNumberOfArgs('arrayUnion()', arguments, 1);
// NOTE: We don't actually parse the data until it's used in set() or
// update() since we'd need the Firestore instance to do this.
return new ArrayUnionFieldValueImpl('arrayUnion', elements);
Expand All @@ -87,7 +85,6 @@ export function arrayUnion(...elements: unknown[]): FieldValue {
* `updateDoc()`
*/
export function arrayRemove(...elements: unknown[]): FieldValue {
validateAtLeastNumberOfArgs('arrayRemove()', arguments, 1);
// NOTE: We don't actually parse the data until it's used in set() or
// update() since we'd need the Firestore instance to do this.
return new ArrayRemoveFieldValueImpl('arrayRemove', elements);
Expand Down
26 changes: 4 additions & 22 deletions packages/firestore/lite/src/api/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import { FieldPath } from './field_path';
import {
validateCollectionPath,
validateDocumentPath,
validateExactNumberOfArgs,
validateNonEmptyString,
validatePositiveNumber
} from '../../../src/util/input_validation';
import { newSerializer } from '../../../src/platform/serializer';
Expand Down Expand Up @@ -335,8 +335,6 @@ export function where(
opStr: WhereFilterOp,
value: unknown
): QueryConstraint {
// TODO(firestorelite): Consider validating the enum strings (note that
// TypeScript does not support passing invalid values).
const op = opStr as Operator;
const field = fieldPathFromArgument('where', fieldPath);
return new QueryFilterConstraint(field, op, value);
Expand Down Expand Up @@ -381,8 +379,6 @@ export function orderBy(
fieldPath: string | FieldPath,
directionStr: OrderByDirection = 'asc'
): QueryConstraint {
// TODO(firestorelite): Consider validating the enum strings (note that
// TypeScript does not support passing invalid values).
const direction = directionStr as Direction;
const path = fieldPathFromArgument('orderBy', fieldPath);
return new QueryOrderByConstraint(path, direction);
Expand Down Expand Up @@ -597,7 +593,6 @@ function newQueryBoundFromDocOrFields<T>(
before: boolean
): Bound {
if (docOrFields[0] instanceof DocumentSnapshot) {
validateExactNumberOfArgs(methodName, docOrFields, 1);
return newQueryBoundFromDocument(
query._query,
query.firestore._databaseId,
Expand Down Expand Up @@ -738,7 +733,7 @@ export function collection(
path: string,
...pathSegments: string[]
): CollectionReference<DocumentData> {
validateNonEmptyArgument('collection', 'path', path);
validateNonEmptyString('collection', 'path', path);
if (parent instanceof FirebaseFirestore) {
const absolutePath = ResourcePath.fromString(path, ...pathSegments);
validateCollectionPath(absolutePath);
Expand Down Expand Up @@ -785,7 +780,7 @@ export function collectionGroup(
firestore: FirebaseFirestore,
collectionId: string
): Query<DocumentData> {
validateNonEmptyArgument('collectionGroup', 'collection id', collectionId);
validateNonEmptyString('collectionGroup', 'collection id', collectionId);
if (collectionId.indexOf('/') >= 0) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
Expand Down Expand Up @@ -868,7 +863,7 @@ export function doc<T>(
if (arguments.length === 1) {
path = AutoId.newId();
}
validateNonEmptyArgument('doc', 'path', path);
validateNonEmptyString('doc', 'path', path);

if (parent instanceof FirebaseFirestore) {
const absolutePath = ResourcePath.fromString(path, ...pathSegments);
Expand Down Expand Up @@ -1234,16 +1229,3 @@ export function newUserDataReader(
serializer
);
}

function validateNonEmptyArgument(
functionName: string,
argumentName: string,
argument?: string
): asserts argument is string {
if (!argument) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
`Function ${functionName}() cannot be called with an empty ${argumentName}.`
);
}
}
11 changes: 1 addition & 10 deletions packages/firestore/src/api/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@

import { isBase64Available } from '../platform/base64';
import { Code, FirestoreError } from '../util/error';
import {
invalidClassError,
validateArgType,
validateExactNumberOfArgs
} from '../util/input_validation';
import { invalidClassError } from '../util/input_validation';
import { ByteString } from '../util/byte_string';
import { Bytes } from '../../lite/src/api/bytes';

Expand Down Expand Up @@ -57,8 +53,6 @@ function assertBase64Available(): void {
*/
export class Blob extends Bytes {
static fromBase64String(base64: string): Blob {
validateExactNumberOfArgs('Blob.fromBase64String', arguments, 1);
validateArgType('Blob.fromBase64String', 'string', 1, base64);
assertBase64Available();
try {
return new Blob(ByteString.fromBase64String(base64));
Expand All @@ -71,7 +65,6 @@ export class Blob extends Bytes {
}

static fromUint8Array(array: Uint8Array): Blob {
validateExactNumberOfArgs('Blob.fromUint8Array', arguments, 1);
assertUint8ArrayAvailable();
if (!(array instanceof Uint8Array)) {
throw invalidClassError('Blob.fromUint8Array', 'Uint8Array', 1, array);
Expand All @@ -80,13 +73,11 @@ export class Blob extends Bytes {
}

toBase64(): string {
validateExactNumberOfArgs('Blob.toBase64', arguments, 0);
assertBase64Available();
return super.toBase64();
}

toUint8Array(): Uint8Array {
validateExactNumberOfArgs('Blob.toUint8Array', arguments, 0);
assertUint8ArrayAvailable();
return super.toUint8Array();
}
Expand Down
Loading

0 comments on commit ff78c48

Please sign in to comment.