Skip to content

Rejecting 'null' and 'undefined' as arguments for CollectionReference.doc() #188

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 1 commit into from
Oct 6, 2017
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
8 changes: 5 additions & 3 deletions src/firestore/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1639,11 +1639,13 @@ export class CollectionReference extends Query

doc(pathString?: string): firestore.DocumentReference {
validateBetweenNumberOfArgs('CollectionReference.doc', arguments, 0, 1);
validateOptionalArgType('CollectionReference.doc', 'string', 1, pathString);
if (pathString === undefined) {
// We allow omission of 'pathString' but explicitly prohibit passing in both
// 'undefined' and 'null'.
if (arguments.length === 0) {
pathString = AutoId.newId();
}
if (typeof pathString !== 'string' || pathString === '') {
validateArgType('CollectionReference.doc', 'string', 1, pathString);
if (pathString === '') {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
'Document path must be a non-empty string'
Expand Down
4 changes: 4 additions & 0 deletions tests/firestore/integration/api/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ apiDescribe('Validation:', persistence => {
'Function CollectionReference.doc() requires its first ' +
'argument to be of type string, but it was: null'
);
expect(() => baseCollectionRef.doc(undefined as any)).to.throw(
'Function CollectionReference.doc() requires its first ' +
'argument to be of type string, but it was: undefined'
);
expect(() => (baseCollectionRef.doc as any)('foo', 'bar')).to.throw(
'Function CollectionReference.doc() requires between 0 and ' +
'1 arguments, but was called with 2 arguments.'
Expand Down