Skip to content

Commit 2fb5df3

Browse files
committed
Test for specific exception text.
1 parent 8359b95 commit 2fb5df3

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

packages/firestore/src/util/json_validation.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export function validateJSON<S extends JsonSchema>(
112112
schema: S
113113
): json is Json<S> {
114114
if (!isPlainObject(json)) {
115-
throw new FirestoreError(Code.INVALID_ARGUMENT, 'json must be an object');
115+
throw new FirestoreError(Code.INVALID_ARGUMENT, 'JSON must be an object');
116116
}
117117
let error: string | undefined = undefined;
118118
for (const key in schema) {
@@ -121,12 +121,13 @@ export function validateJSON<S extends JsonSchema>(
121121
const value: { value: unknown } | undefined =
122122
'value' in schema[key] ? { value: schema[key].value } : undefined;
123123
if (!(key in json)) {
124-
error = `json missing required field: ${key}`;
124+
error = `JSON missing required field: '${key}'`;
125+
break;
125126
}
126127
// eslint-disable-next-line @typescript-eslint/no-explicit-any
127128
const fieldValue = (json as any)[key];
128129
if (typeString && typeof fieldValue !== typeString) {
129-
error = `json field '${key}' must be a ${typeString}.`;
130+
error = `JSON field '${key}' must be a ${typeString}.`;
130131
break;
131132
} else if (value !== undefined && fieldValue !== value.value) {
132133
error = `Expected '${key}' field to equal '${value.value}'`;

packages/firestore/test/unit/api/database.test.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ describe('DocumentReference', () => {
9797
const db = newTestFirestore();
9898
expect(() => {
9999
DocumentReference.fromJSON(db, {});
100-
}).to.throw;
100+
}).to.throw("JSON missing required field: 'type'");
101101
});
102102

103103
it('fromJSON() throws with missing type data', () => {
@@ -108,7 +108,7 @@ describe('DocumentReference', () => {
108108
bundleName: 'test name',
109109
bundle: 'test bundle'
110110
});
111-
}).to.throw;
111+
}).to.throw("JSON missing required field: 'type'");
112112
});
113113

114114
it('fromJSON() throws with invalid type data', () => {
@@ -120,7 +120,7 @@ describe('DocumentReference', () => {
120120
bundleName: 'test name',
121121
bundle: 'test bundle'
122122
});
123-
}).to.throw;
123+
}).to.throw("JSON field 'type' must be a string");
124124
});
125125

126126
it('fromJSON() throws with missing bundleSource', () => {
@@ -131,7 +131,7 @@ describe('DocumentReference', () => {
131131
bundleName: 'test name',
132132
bundle: 'test bundle'
133133
});
134-
}).to.throw;
134+
}).to.throw("JSON missing required field: 'bundleSource'");
135135
});
136136

137137
it('fromJSON() throws with invalid bundleSource type', () => {
@@ -143,7 +143,7 @@ describe('DocumentReference', () => {
143143
bundleName: 'test name',
144144
bundle: 'test bundle'
145145
});
146-
}).to.throw;
146+
}).to.throw("JSON field 'bundleSource' must be a string");
147147
});
148148

149149
it('fromJSON() throws with invalid bundleSource value', () => {
@@ -155,7 +155,7 @@ describe('DocumentReference', () => {
155155
bundleName: 'test name',
156156
bundle: 'test bundle'
157157
});
158-
}).to.throw;
158+
}).to.throw("Expected 'bundleSource' field to equal 'DocumentSnapshot'");
159159
});
160160

161161
it('fromJSON() throws with missing bundleName', () => {
@@ -166,7 +166,7 @@ describe('DocumentReference', () => {
166166
bundleSource: 'DocumentSnapshot',
167167
bundle: 'test bundle'
168168
});
169-
}).to.throw;
169+
}).to.throw("JSON missing required field: 'bundleName'");
170170
});
171171

172172
it('fromJSON() throws with invalid bundleName', () => {
@@ -178,7 +178,7 @@ describe('DocumentReference', () => {
178178
bundleName: 1,
179179
bundle: 'test bundle'
180180
});
181-
}).to.throw;
181+
}).to.throw("JSON field 'bundleName' must be a string");
182182
});
183183

184184
it('fromJSON() throws with missing bundle', () => {
@@ -189,7 +189,7 @@ describe('DocumentReference', () => {
189189
bundleSource: 'DocumentSnapshot',
190190
bundleName: 'test name'
191191
});
192-
}).to.throw;
192+
}).to.throw("JSON missing required field: 'bundle'");
193193
});
194194

195195
it('fromJSON() throws with invalid bundle', () => {
@@ -201,7 +201,7 @@ describe('DocumentReference', () => {
201201
bundleName: 'test name',
202202
bundle: 1
203203
});
204-
}).to.throw;
204+
}).to.throw("JSON field 'bundle' must be a string");
205205
});
206206

207207
it('fromJSON() does not throw', () => {
@@ -500,7 +500,7 @@ describe('QuerySnapshot', () => {
500500
const db = newTestFirestore();
501501
expect(() => {
502502
querySnapshotFromJSON(db, {});
503-
}).to.throw;
503+
}).to.throw("JSON missing required field: 'type'");
504504
});
505505

506506
it('fromJSON() throws with missing type data', () => {
@@ -511,7 +511,7 @@ describe('QuerySnapshot', () => {
511511
bundleName: 'test name',
512512
bundle: 'test bundle'
513513
});
514-
}).to.throw;
514+
}).to.throw("JSON missing required field: 'type'");
515515
});
516516

517517
it('fromJSON() throws with invalid type data', () => {
@@ -523,18 +523,18 @@ describe('QuerySnapshot', () => {
523523
bundleName: 'test name',
524524
bundle: 'test bundle'
525525
});
526-
}).to.throw;
526+
}).to.throw("JSON field 'type' must be a string");
527527
});
528528

529-
it('fromJSON() throws with invalid type data', () => {
529+
it('fromJSON() throws with missing bundle source data', () => {
530530
const db = newTestFirestore();
531531
expect(() => {
532532
querySnapshotFromJSON(db, {
533533
type: QuerySnapshot._jsonSchemaVersion,
534534
bundleName: 'test name',
535535
bundle: 'test bundle'
536536
});
537-
}).to.throw;
537+
}).to.throw("JSON missing required field: 'bundleSource'");
538538
});
539539

540540
it('fromJSON() throws with invalid bundleSource type', () => {
@@ -546,7 +546,7 @@ describe('QuerySnapshot', () => {
546546
bundleName: 'test name',
547547
bundle: 'test bundle'
548548
});
549-
}).to.throw;
549+
}).to.throw("JSON field 'bundleSource' must be a string");
550550
});
551551

552552
it('fromJSON() throws with invalid bundleSource value', () => {
@@ -558,7 +558,7 @@ describe('QuerySnapshot', () => {
558558
bundleName: 'test name',
559559
bundle: 'test bundle'
560560
});
561-
}).to.throw;
561+
}).to.throw("Expected 'bundleSource' field to equal 'QuerySnapshot'");
562562
});
563563

564564
it('fromJSON() throws with missing bundleName', () => {
@@ -569,7 +569,7 @@ describe('QuerySnapshot', () => {
569569
bundleSource: 'QuerySnapshot',
570570
bundle: 'test bundle'
571571
});
572-
}).to.throw;
572+
}).to.throw("JSON missing required field: 'bundleName'");
573573
});
574574

575575
it('fromJSON() throws with invalid bundleName', () => {
@@ -581,21 +581,21 @@ describe('QuerySnapshot', () => {
581581
bundleName: 1,
582582
bundle: 'test bundle'
583583
});
584-
}).to.throw;
584+
}).to.throw("JSON field 'bundleName' must be a string");
585585
});
586586

587-
it('fromJSON() throws with missing bundle data', () => {
587+
it('fromJSON() throws with missing bundle field', () => {
588588
const db = newTestFirestore();
589589
expect(() => {
590590
querySnapshotFromJSON(db, {
591591
type: QuerySnapshot._jsonSchemaVersion,
592592
bundleSource: 'QuerySnapshot',
593593
bundleName: 'test name'
594594
});
595-
}).to.throw;
595+
}).to.throw("JSON missing required field: 'bundle'");
596596
});
597597

598-
it('fromJSON() throws with invalid bundle data', () => {
598+
it('fromJSON() throws with invalid bundle field', () => {
599599
const db = newTestFirestore();
600600
expect(() => {
601601
querySnapshotFromJSON(db, {
@@ -604,7 +604,7 @@ describe('QuerySnapshot', () => {
604604
bundleName: 'test name',
605605
bundle: 1
606606
});
607-
}).to.throw;
607+
}).to.throw("JSON field 'bundle' must be a string");
608608
});
609609

610610
it('fromJSON does not throw', () => {

0 commit comments

Comments
 (0)