Skip to content

Validate operator enum in Query.where() calls #1677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import {
validateOptionalArgType,
validateOptionalArrayElements,
validateOptionNames,
validateStringEnum,
valueDescription
} from '../util/input_validation';
import * as log from '../util/log';
Expand Down Expand Up @@ -1352,8 +1353,10 @@ export class Query implements firestore.Query {
value: unknown
): firestore.Query {
validateExactNumberOfArgs('Query.where', arguments, 3);
validateArgType('Query.where', 'non-empty string', 2, opStr);
validateDefined('Query.where', 3, value);
// Enumerated from the WhereFilterOp type in index.d.ts.
const whereFilterOpEnums = ['<', '<=', '==', '>=', '>', 'array-contains'];
validateStringEnum('Query.where', whereFilterOpEnums, 2, opStr);
let fieldValue;
const fieldPath = fieldPathFromArgument('Query.where', field);
const relationOp = RelationOp.fromString(opStr);
Expand Down
25 changes: 24 additions & 1 deletion packages/firestore/src/util/input_validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { fail } from './assert';
import { Code, FirestoreError } from './error';
import * as obj from './obj';
Expand Down Expand Up @@ -292,6 +291,30 @@ export function validateNamedOptionalPropertyEquals<T>(
}
}

/**
* Validates that the provided argument is a valid enum.
*
* @param functionName Function making the validation call.
* @param enums Array containing all possible values for the enum.
* @param position Position of the argument in `functionName`.
* @param argument Arugment to validate.
*/
export function validateStringEnum<T>(
functionName: string,
enums: string[],
position: number,
argument: unknown
): void {
if (!enums.some(element => element === argument)) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
`Invalid value ${valueDescription(argument)} provided to function ` +
`${functionName}() for its ${ordinal(position)} argument. Acceptable ` +
`values: ${enums.join(', ')}`
);
}
}

/** Helper to validate the type of a provided input. */
function validateType(
functionName: string,
Expand Down
8 changes: 8 additions & 0 deletions packages/firestore/test/integration/api/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,14 @@ apiDescribe('Validation:', persistence => {
);
});

validationIt(persistence, 'enum', db => {
const collection = db.collection('test') as any;
expect(() => collection.where('a', 'foo' as any, 'b')).to.throw(
'Invalid value "foo" provided to function Query.where() for its second argument. ' +
'Acceptable values: <, <=, ==, >=, >, array-contains'
);
});

validationIt(
persistence,
'with null or NaN non-equality filters fail',
Expand Down