Skip to content

Commit

Permalink
Remove JS input validation for Storage (#3967)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Oct 20, 2020
1 parent 2b06694 commit b247ffa
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 815 deletions.
6 changes: 6 additions & 0 deletions .changeset/tiny-hounds-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"firebase: major
"@firebase/storage": major
---

This releases removes all input validation. Please use our TypeScript types to validate API usage.
165 changes: 0 additions & 165 deletions packages/storage/src/implementation/args.ts

This file was deleted.

29 changes: 0 additions & 29 deletions packages/storage/src/implementation/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/
import { Location } from './location';
import * as json from './json';
import * as type from './type';
import { ListResult } from '../list';
import { StorageService } from '../service';

Expand All @@ -43,9 +42,6 @@ interface ListResultResponse {
nextPageToken?: string;
}

const MAX_RESULTS_KEY = 'maxResults';
const MAX_MAX_RESULTS = 1000;
const PAGE_TOKEN_KEY = 'pageToken';
const PREFIXES_KEY = 'prefixes';
const ITEMS_KEY = 'items';

Expand Down Expand Up @@ -92,28 +88,3 @@ export function fromResponseString(
const resource = (obj as unknown) as ListResultResponse;
return fromBackendResponse(service, bucket, resource);
}

export function listOptionsValidator(p: unknown): void {
if (!type.isObject(p) || !p) {
throw 'Expected ListOptions object.';
}
for (const key in p) {
if (key === MAX_RESULTS_KEY) {
if (
!type.isInteger(p[MAX_RESULTS_KEY]) ||
(p[MAX_RESULTS_KEY] as number) <= 0
) {
throw 'Expected maxResults to be a positive number.';
}
if ((p[MAX_RESULTS_KEY] as number) > 1000) {
throw `Expected maxResults to be less than or equal to ${MAX_MAX_RESULTS}.`;
}
} else if (key === PAGE_TOKEN_KEY) {
if (p[PAGE_TOKEN_KEY] && !type.isString(p[PAGE_TOKEN_KEY])) {
throw 'Expected pageToken to be string.';
}
} else {
throw 'Unknown option: ' + key;
}
}
}
20 changes: 0 additions & 20 deletions packages/storage/src/implementation/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,23 +205,3 @@ export function toResourceString(
}
return JSON.stringify(resource);
}

export function metadataValidator(p: unknown): void {
if (!type.isObject(p) || !p) {
throw 'Expected Metadata object.';
}
for (const key in p) {
if (p.hasOwnProperty(key)) {
const val = p[key];
if (key === 'customMetadata') {
if (!type.isObject(val)) {
throw "Expected object for 'customMetadata' mapping.";
}
} else {
if (type.isNonNullObject(val)) {
throw "Mapping for '" + key + "' cannot be an object.";
}
}
}
}
}
22 changes: 0 additions & 22 deletions packages/storage/src/implementation/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,6 @@ export const StringFormat = {
DATA_URL: 'data_url'
};

export function formatValidator(stringFormat: unknown): void {
switch (stringFormat) {
case StringFormat.RAW:
case StringFormat.BASE64:
case StringFormat.BASE64URL:
case StringFormat.DATA_URL:
return;
default:
throw (
'Expected one of the event types: [' +
StringFormat.RAW +
', ' +
StringFormat.BASE64 +
', ' +
StringFormat.BASE64URL +
', ' +
StringFormat.DATA_URL +
'].'
);
}
}

/**
* @struct
*/
Expand Down
40 changes: 23 additions & 17 deletions packages/storage/src/implementation/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

import { Code, FirebaseStorageError } from './error';

/**
* @return False if the object is undefined or null, true otherwise.
*/
Expand All @@ -31,34 +33,38 @@ export function isFunction(p: unknown): p is Function {
return typeof p === 'function';
}

export function isObject(p: unknown): p is { [key: string]: unknown } | null {
return typeof p === 'object';
}

export function isNonNullObject(p: unknown): p is object {
return isObject(p) && p !== null;
}

export function isNonArrayObject(p: unknown): boolean {
return isObject(p) && !Array.isArray(p);
return typeof p === 'object' && !Array.isArray(p);
}

export function isString(p: unknown): p is string {
return typeof p === 'string' || p instanceof String;
}

export function isInteger(p: unknown): p is number {
return isNumber(p) && Number.isInteger(p);
}

export function isNumber(p: unknown): p is number {
return typeof p === 'number' || p instanceof Number;
}

export function isNativeBlob(p: unknown): p is Blob {
return isNativeBlobDefined() && p instanceof Blob;
}

export function isNativeBlobDefined(): boolean {
return typeof Blob !== 'undefined';
}

export function validateNumber(
argument: string,
minValue: number,
maxValue: number,
value: number
): void {
if (value < minValue) {
throw new FirebaseStorageError(
Code.INVALID_ARGUMENT,
`Invalid value for '${argument}'. Expected ${minValue} or greater.`
);
}
if (value > maxValue) {
throw new FirebaseStorageError(
Code.INVALID_ARGUMENT,
`Invalid value for '${argument}'. Expected ${maxValue} or less.`
);
}
}

0 comments on commit b247ffa

Please sign in to comment.