Skip to content

Commit 4cf66f2

Browse files
committed
convert DocumentReference to new JSON validator.
1 parent 00a2423 commit 4cf66f2

File tree

1 file changed

+20
-36
lines changed

1 file changed

+20
-36
lines changed

packages/firestore/src/lite-api/reference.ts

+20-36
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ import {
3333
validateNonEmptyArgument
3434
} from '../util/input_validation';
3535
import { AutoId } from '../util/misc';
36+
// API extractor fails importing property unless we also explicitly import Property.
37+
// eslint-disable-next-line @typescript-eslint/no-unused-vars, unused-imports/no-unused-imports-ts
38+
import { Property, property, validateJSON } from '../util/json_validation';
3639

3740
import { Firestore } from './database';
3841
import { FieldPath } from './field_path';
@@ -279,10 +282,16 @@ export class DocumentReference<
279282
);
280283
}
281284

285+
static _jsonSchemaVersion: string = 'firestore/documentReference/1.0';
286+
static _jsonSchema = {
287+
type: property('string', DocumentReference._jsonSchemaVersion),
288+
referencePath: property('string')
289+
};
290+
282291
/** Returns a JSON-serializable representation of this DocumentReference. */
283292
toJSON(): object {
284293
return {
285-
type: 'firestore/documentReference/1.0',
294+
type: DocumentReference._jsonSchemaVersion,
286295
referencePath: this._key.toString()
287296
};
288297
}
@@ -296,42 +305,17 @@ export class DocumentReference<
296305
json: object,
297306
converter?: FirestoreDataConverter<NewAppModelType, NewDbModelType>
298307
): DocumentReference<NewAppModelType, NewDbModelType> {
299-
const requiredFields = ['type', 'referencePath'];
300-
let error: string | undefined = undefined;
301-
let path: string = '';
302-
for (const key of requiredFields) {
303-
if (!(key in json)) {
304-
error = `json missing required field: ${key}`;
305-
}
306-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
307-
const value = (json as any)[key];
308-
if (key === 'type') {
309-
if (typeof value !== 'string') {
310-
error = `json field 'type' must be a string.`;
311-
break;
312-
} else if (value !== 'firestore/documentReference/1.0') {
313-
error =
314-
"Expected 'type' field to equal 'firestore/documentReference/1.0'";
315-
break;
316-
}
317-
} else if (key === 'referencePath') {
318-
if (typeof value !== 'string') {
319-
error = `json field 'referencePath' must be a string.`;
320-
break;
321-
}
322-
path = value;
323-
}
324-
}
325-
if (error) {
326-
throw new FirestoreError(Code.INVALID_ARGUMENT, error);
308+
if(validateJSON(json, DocumentReference._jsonSchema)) {
309+
return new DocumentReference<NewAppModelType, NewDbModelType>(
310+
firestore,
311+
converter ? converter : null,
312+
new DocumentKey(ResourcePath.fromString(json.referencePath))
313+
);
327314
}
328-
const resourcePath = ResourcePath.fromString(path);
329-
const documentKey = new DocumentKey(resourcePath);
330-
return new DocumentReference<NewAppModelType, NewDbModelType>(
331-
firestore,
332-
converter ? converter : null,
333-
documentKey
334-
);
315+
throw new FirestoreError(
316+
Code.INTERNAL,
317+
'Unexpected error creating Bytes from JSON.'
318+
);
335319
}
336320
}
337321

0 commit comments

Comments
 (0)