@@ -660,12 +660,12 @@ export class WriteBatch implements firestore.WriteBatch {
660660 * @internal
661661 * @param arg The argument name or argument index (for varargs methods).
662662 * @param value The object to validate
663- * @param allowExists Whether to allow the 'exists' preconditions .
663+ * @param options Options describing other things for this function to validate .
664664 */
665665function validatePrecondition (
666666 arg : string | number ,
667667 value : unknown ,
668- allowExists : boolean
668+ options ?: { allowedExistsValues ?: boolean [ ] }
669669) : void {
670670 if ( typeof value !== 'object' || value === null ) {
671671 throw new Error ( 'Input is not an object.' ) ;
@@ -677,20 +677,22 @@ function validatePrecondition(
677677
678678 if ( precondition . exists !== undefined ) {
679679 ++ conditions ;
680- if ( ! allowExists ) {
680+ if ( typeof precondition . exists !== 'boolean' ) {
681681 throw new Error (
682682 `${ invalidArgumentMessage (
683683 arg ,
684684 'precondition'
685- ) } "exists" is not an allowed precondition. `
685+ ) } "exists" is not a boolean.' `
686686 ) ;
687687 }
688- if ( typeof precondition . exists !== 'boolean' ) {
688+ if (
689+ options ?. allowedExistsValues &&
690+ options . allowedExistsValues . indexOf ( precondition . exists ) < 0
691+ ) {
689692 throw new Error (
690- `${ invalidArgumentMessage (
691- arg ,
692- 'precondition'
693- ) } "exists" is not a boolean.'`
693+ `${ invalidArgumentMessage ( arg , 'precondition' ) } ` +
694+ `"exists" is not allowed to have the value ${ precondition . exists } ` +
695+ `(allowed values: ${ options . allowedExistsValues . join ( ', ' ) } )`
694696 ) ;
695697 }
696698 }
@@ -733,7 +735,7 @@ function validateUpdatePrecondition(
733735 options ?: RequiredArgumentOptions
734736) : asserts value is { lastUpdateTime ?: Timestamp } {
735737 if ( ! validateOptional ( value , options ) ) {
736- validatePrecondition ( arg , value , /* allowExists= */ false ) ;
738+ validatePrecondition ( arg , value , { allowedExistsValues : [ true ] } ) ;
737739 }
738740}
739741
@@ -753,7 +755,7 @@ function validateDeletePrecondition(
753755 options ?: RequiredArgumentOptions
754756) : void {
755757 if ( ! validateOptional ( value , options ) ) {
756- validatePrecondition ( arg , value , /* allowExists= */ true ) ;
758+ validatePrecondition ( arg , value ) ;
757759 }
758760}
759761
0 commit comments