Skip to content

Commit

Permalink
Reduce size of 'Cleanup: Remove validator'
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Dec 28, 2018
1 parent 00c7dee commit 1c33538
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 73 deletions.
2 changes: 1 addition & 1 deletion dev/src/document.ts
Expand Up @@ -381,7 +381,7 @@ export class DocumentSnapshot {
* console.log(`Retrieved field value: ${field}`);
* });
*/
get(field: string|FieldPath): any { // tslint:disable-line no-any
get(field: string|FieldPath): UserInput
validateFieldPath('field', field);

const protoField = this.protoField(field);
Expand Down
2 changes: 1 addition & 1 deletion dev/src/field-value.ts
Expand Up @@ -391,5 +391,5 @@ function validateArrayElement(arg: string|number, value: unknown): void {
arg, value, 'array element',
/*path=*/{allowEmpty: true, allowDeletes: 'none', allowTransforms: false},
/*path=*/undefined,
/*level=*/0, /* inArray=*/true);
/*level=*/0, /*inArray=*/true);
}
33 changes: 33 additions & 0 deletions dev/src/index.ts
Expand Up @@ -1319,6 +1319,39 @@ function validateFieldValue(
return true;
}

/**
* Validates the use of 'options' as ReadOptions and enforces that 'fieldMask'
* is an array of strings or field paths.
*
* @private
* @param options.fieldMask - The subset of fields to return from a read
* operation.
*/
export function validateReadOptions(options: ReadOptions): boolean {
if (!is.object(options)) {
throw new Error('Input is not an object.');
}

if (options.fieldMask === undefined) {
return true;
}

if (!Array.isArray(options.fieldMask)) {
throw new Error('"fieldMask" is not an array.');
}

for (let i = 0; i < options.fieldMask.length; ++i) {
try {
FieldPath.validateFieldPath(options.fieldMask[i]);
} catch (err) {
throw new Error(
`Element at index ${i} is not a valid FieldPath. ${err.message}`);
}
}

return true;
}

/**
* A logging function that takes a single string.
*
Expand Down
2 changes: 1 addition & 1 deletion dev/src/path.ts
Expand Up @@ -395,7 +395,7 @@ export class ResourcePath extends Path<ResourcePath> {
}

/**
* Returns true if the given string can be used as a relative or absolute
* Validates that the given string can be used as a relative or absolute
* resource path.
*
* @private
Expand Down
40 changes: 23 additions & 17 deletions dev/src/reference.ts
Expand Up @@ -45,7 +45,9 @@ import {Firestore} from './index';
*/
const directionOperators: {[k: string]: api.StructuredQuery.Direction} = {
asc: 'ASCENDING',
ASC: 'ASCENDING',
desc: 'DESCENDING',
DESC: 'DESCENDING',
};

/*!
Expand All @@ -58,6 +60,7 @@ const comparisonOperators:
{[k: string]: api.StructuredQuery.FieldFilter.Operator} = {
'<': 'LESS_THAN',
'<=': 'LESS_THAN_OR_EQUAL',
'=': 'EQUAL',
'==': 'EQUAL',
'>': 'GREATER_THAN',
'>=': 'GREATER_THAN_OR_EQUAL',
Expand Down Expand Up @@ -1003,8 +1006,7 @@ export class Query {
* });
* });
*/
where(fieldPath: string|FieldPath, opStr: WhereFilterOp, value: unknown):
Query {
where(fieldPath: string|FieldPath, opStr: string, value: UserInput): Query {
validateFieldPath('fieldPath', fieldPath);
validateQueryOperator('opStr', opStr, value);
validateQueryValue('value', value);
Expand Down Expand Up @@ -1094,7 +1096,7 @@ export class Query {
* });
* });
*/
orderBy(fieldPath: string|FieldPath, directionStr?: OrderByDirection): Query {
orderBy(fieldPath: string|FieldPath, directionStr?: string): Query {
validateFieldPath('fieldPath', fieldPath);
validateQueryOrder('directionStr', directionStr);

Expand Down Expand Up @@ -1703,9 +1705,8 @@ export class Query {
(s1: QueryDocumentSnapshot, s2: QueryDocumentSnapshot) => number {
return (doc1, doc2) => {
// Add implicit sorting by name, using the last specified direction.
const lastDirection: api.StructuredQuery.Direction =
this._fieldOrders.length === 0 ?
'ASCENDING' :
const lastDirection = this._fieldOrders.length === 0 ?
directionOperators.ASC :
this._fieldOrders[this._fieldOrders.length - 1].direction;
const orderBys = this._fieldOrders.concat(
new FieldOrder(FieldPath.documentId(), lastDirection));
Expand All @@ -1727,7 +1728,8 @@ export class Query {
}

if (comp !== 0) {
const direction = orderBy.direction === 'ASCENDING' ? 1 : -1;
const direction =
orderBy.direction === directionOperators.ASC ? 1 : -1;
return direction * comp;
}
}
Expand Down Expand Up @@ -1943,7 +1945,9 @@ function createCollectionReference(firestore, path): CollectionReference {
* @throws when the direction is invalid
*/
export function validateQueryOrder(arg: string|number, op: unknown): void {
validateEnumValue(arg, op, Object.keys(directionOperators), {optional: true});
if (!is.string(str) || !is.defined(directionOperators[str])) {
throw new Error('Order must be one of "asc" or "desc".');
}
}

/**
Expand All @@ -1957,16 +1961,18 @@ export function validateQueryOrder(arg: string|number, op: unknown): void {
*/
export function validateQueryOperator(
arg: string|number, op: unknown, fieldValue: unknown): void {
validateEnumValue(arg, op, Object.keys(comparisonOperators));
if (is.string(str) && comparisonOperators[str]) {
const op = comparisonOperators[str];

if (typeof fieldValue === 'number' && isNaN(fieldValue) && op !== '==') {
throw new Error(
'Invalid query. You can only perform equals comparisons on NaN.');
}
if (typeof val === 'number' && isNaN(val) && op !== 'EQUAL') {
throw new Error(
'Invalid query. You can only perform equals comparisons on NaN.');
}

if (fieldValue === null && op !== '==') {
throw new Error(
'Invalid query. You can only perform equals comparisons on Null.');
if (val === null && op !== 'EQUAL') {
throw new Error(
'Invalid query. You can only perform equals comparisons on Null.');
}
}
}

Expand Down Expand Up @@ -1994,7 +2000,7 @@ export function validateDocumentReference(
function validateQueryValue(arg: string|number, value: unknown): void {
validateUserInput(
arg, value, 'query constraint',
{allowEmpty: true, allowDeletes: 'none', allowTransforms: false});
{allowDeletes: 'none', allowTransforms: false});
}

/**
Expand Down
15 changes: 0 additions & 15 deletions dev/src/types.ts
Expand Up @@ -107,18 +107,6 @@ export type UpdateData = {
[fieldPath: string]: UserInput
};

/**
* The direction of a `Query.orderBy()` clause is specified as 'desc' or 'asc'
* (descending or ascending).
*/
export type OrderByDirection = 'desc'|'asc';

/**
* Filter conditions in a `Query.where()` clause are specified using the
* strings '<', '<=', '==', '>=', '>', and 'array-contains'.
*/
export type WhereFilterOp = '<'|'<='|'=='|'>='|'>'|'array-contains';

/**
* An options object that configures conditional behavior of `update()` and
* `delete()` calls in `DocumentReference`, `WriteBatch`, and `Transaction`.
Expand Down Expand Up @@ -183,9 +171,6 @@ export interface ValidationOptions {

/** Whether server transforms are supported. */
allowTransforms: boolean;

/** Whether empty documents are supported. */
allowEmpty: boolean;
}

/**
Expand Down
7 changes: 1 addition & 6 deletions dev/src/write-batch.ts
Expand Up @@ -725,14 +725,10 @@ export function validateDocumentData(
throw new Error(customObjectMessage(arg, obj));
}

let isEmpty = true;

for (const prop in obj) {
if (obj.hasOwnProperty(prop)) {
isEmpty = false;
validateUserInput(
arg, obj[prop], 'Firestore document', {
allowEmpty: true,
allowDeletes: allowDeletes ? 'all' : 'none',
allowTransforms: true,
},
Expand Down Expand Up @@ -786,7 +782,6 @@ function validateUpdateMap(arg: string|number, obj: unknown): void {
isEmpty = false;
validateUserInput(
arg, obj[prop], 'Firestore document', {
allowEmpty: true,
allowDeletes: 'root',
allowTransforms: true,
},
Expand All @@ -797,4 +792,4 @@ function validateUpdateMap(arg: string|number, obj: unknown): void {
if (isEmpty) {
throw new Error('At least one field must be updated.');
}
}
}

0 comments on commit 1c33538

Please sign in to comment.