@@ -33,6 +33,9 @@ import {
33
33
validateNonEmptyArgument
34
34
} from '../util/input_validation' ;
35
35
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' ;
36
39
37
40
import { Firestore } from './database' ;
38
41
import { FieldPath } from './field_path' ;
@@ -279,10 +282,16 @@ export class DocumentReference<
279
282
) ;
280
283
}
281
284
285
+ static _jsonSchemaVersion : string = 'firestore/documentReference/1.0' ;
286
+ static _jsonSchema = {
287
+ type : property ( 'string' , DocumentReference . _jsonSchemaVersion ) ,
288
+ referencePath : property ( 'string' )
289
+ } ;
290
+
282
291
/** Returns a JSON-serializable representation of this DocumentReference. */
283
292
toJSON ( ) : object {
284
293
return {
285
- type : 'firestore/documentReference/1.0' ,
294
+ type : DocumentReference . _jsonSchemaVersion ,
286
295
referencePath : this . _key . toString ( )
287
296
} ;
288
297
}
@@ -296,42 +305,17 @@ export class DocumentReference<
296
305
json : object ,
297
306
converter ?: FirestoreDataConverter < NewAppModelType , NewDbModelType >
298
307
) : 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
+ ) ;
327
314
}
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
+ ) ;
335
319
}
336
320
}
337
321
0 commit comments