@@ -19,23 +19,16 @@ const deepEqual = require('deep-equal');
1919import * as assert from 'assert' ;
2020
2121import { google } from '../protos/firestore_proto_api' ;
22- import { DeleteTransform , FieldTransform } from './field-value' ;
23- import { GeoPoint } from './geo-point' ;
22+ import { FieldTransform } from './field-value' ;
2423import { FieldPath , validateFieldPath } from './path' ;
2524import { DocumentReference } from './reference' ;
2625import { isPlainObject , Serializer } from './serializer' ;
2726import { Timestamp } from './timestamp' ;
28- import { ApiMapValue , DocumentData , UpdateMap , ValidationOptions } from './types' ;
27+ import { ApiMapValue , DocumentData , UpdateMap } from './types' ;
2928import { isEmpty , isObject } from './util' ;
30- import { customObjectMessage , invalidArgumentMessage } from './validate' ;
3129
3230import api = google . firestore . v1beta1 ;
3331
34- /*!
35- * The maximum depth of a Firestore object.
36- */
37- const MAX_DEPTH = 20 ;
38-
3932/**
4033 * Returns a builder for DocumentSnapshot and QueryDocumentSnapshot instances.
4134 * Invoke `.build()' to assemble the final snapshot.
@@ -1016,107 +1009,3 @@ export class Precondition {
10161009 return this . _exists === undefined && ! this . _lastUpdateTime ;
10171010 }
10181011}
1019-
1020- /**
1021- * Validates a JavaScript value for usage as a Firestore value.
1022- *
1023- * @private
1024- * @param arg The argument name or argument index (for varargs methods).
1025- * @param value JavaScript value to validate.
1026- * @param desc A description of the expected type.
1027- * @param path The field path to validate.
1028- * @param options Validation options
1029- * @param level The current depth of the traversal. This is used to decide
1030- * whether deletes are allowed in conjunction with `allowDeletes: root`.
1031- * @param inArray Whether we are inside an array.
1032- * @throws when the object is invalid.
1033- */
1034- export function validateUserInput (
1035- arg : string | number , value : unknown , desc : string ,
1036- options : ValidationOptions , path ?: FieldPath , level ?: number ,
1037- inArray ?: boolean ) : void {
1038- if ( path && path . size > MAX_DEPTH ) {
1039- throw new Error (
1040- `${ invalidArgumentMessage ( arg , desc ) } Input object is deeper than ${
1041- MAX_DEPTH } levels or contains a cycle.`) ;
1042- }
1043-
1044- options = options || { } ;
1045- level = level || 0 ;
1046- inArray = inArray || false ;
1047-
1048- const fieldPathMessage = path ? ` (found in field ${ path . toString ( ) } )` : '' ;
1049-
1050- if ( Array . isArray ( value ) ) {
1051- const arr = value as unknown [ ] ;
1052- for ( let i = 0 ; i < arr . length ; ++ i ) {
1053- validateUserInput (
1054- arg , arr [ i ] ! , desc , options ,
1055- path ? path . append ( String ( i ) ) : new FieldPath ( String ( i ) ) , level + 1 ,
1056- /* inArray= */ true ) ;
1057- }
1058- } else if ( isPlainObject ( value ) ) {
1059- const obj = value as object ;
1060- for ( const prop in obj ) {
1061- if ( obj . hasOwnProperty ( prop ) ) {
1062- validateUserInput (
1063- arg , obj [ prop ] ! , desc , options ,
1064- path ? path . append ( new FieldPath ( prop ) ) : new FieldPath ( prop ) ,
1065- level + 1 , inArray ) ;
1066- }
1067- }
1068- } else if ( value === undefined ) {
1069- throw new Error ( `${
1070- invalidArgumentMessage (
1071- arg , desc ) } Cannot use "undefined" as a Firestore value${
1072- fieldPathMessage } .`) ;
1073- } else if ( value instanceof DeleteTransform ) {
1074- if ( inArray ) {
1075- throw new Error ( `${ invalidArgumentMessage ( arg , desc ) } ${
1076- value . methodName } () cannot be used inside of an array${
1077- fieldPathMessage } .`) ;
1078- } else if (
1079- ( options . allowDeletes === 'root' && level !== 0 ) ||
1080- options . allowDeletes === 'none' ) {
1081- throw new Error ( `${ invalidArgumentMessage ( arg , desc ) } ${
1082- value
1083- . methodName } () must appear at the top-level and can only be used in update() or set() with {merge:true}${
1084- fieldPathMessage } .`) ;
1085- }
1086- } else if ( value instanceof FieldTransform ) {
1087- if ( inArray ) {
1088- throw new Error ( `${ invalidArgumentMessage ( arg , desc ) } ${
1089- value . methodName } () cannot be used inside of an array${
1090- fieldPathMessage } .`) ;
1091- } else if ( ! options . allowTransforms ) {
1092- throw new Error ( `${ invalidArgumentMessage ( arg , desc ) } ${
1093- value . methodName } () can only be used in set(), create() or update()${
1094- fieldPathMessage } .`) ;
1095- }
1096- } else if ( value instanceof FieldPath ) {
1097- throw new Error ( `${
1098- invalidArgumentMessage (
1099- arg ,
1100- desc ) } Cannot use object of type "FieldPath" as a Firestore value${
1101- fieldPathMessage } .`) ;
1102- } else if ( value instanceof DocumentReference ) {
1103- // Ok.
1104- } else if ( value instanceof GeoPoint ) {
1105- // Ok.
1106- } else if ( value instanceof Timestamp || value instanceof Date ) {
1107- // Ok.
1108- } else if ( value instanceof Buffer || value instanceof Uint8Array ) {
1109- // Ok.
1110- } else if ( value === null ) {
1111- // Ok.
1112- } else if ( typeof value === 'object' ) {
1113- throw new Error ( customObjectMessage ( arg , value , path ) ) ;
1114- }
1115- }
1116-
1117- export function validateFieldValue (
1118- arg : string | number , val : unknown , path ?: FieldPath ) : void {
1119- validateUserInput (
1120- arg , val , 'Firestore value' ,
1121- { allowDeletes : 'root' , allowTransforms : true } , path ) ;
1122- }
0 commit comments