@@ -660,12 +660,12 @@ export class WriteBatch implements firestore.WriteBatch {
660
660
* @internal
661
661
* @param arg The argument name or argument index (for varargs methods).
662
662
* @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 .
664
664
*/
665
665
function validatePrecondition (
666
666
arg : string | number ,
667
667
value : unknown ,
668
- allowExists : boolean
668
+ options ?: { allowedExistsValues ?: boolean [ ] }
669
669
) : void {
670
670
if ( typeof value !== 'object' || value === null ) {
671
671
throw new Error ( 'Input is not an object.' ) ;
@@ -677,20 +677,22 @@ function validatePrecondition(
677
677
678
678
if ( precondition . exists !== undefined ) {
679
679
++ conditions ;
680
- if ( ! allowExists ) {
680
+ if ( typeof precondition . exists !== 'boolean' ) {
681
681
throw new Error (
682
682
`${ invalidArgumentMessage (
683
683
arg ,
684
684
'precondition'
685
- ) } "exists" is not an allowed precondition. `
685
+ ) } "exists" is not a boolean.' `
686
686
) ;
687
687
}
688
- if ( typeof precondition . exists !== 'boolean' ) {
688
+ if (
689
+ options ?. allowedExistsValues &&
690
+ options . allowedExistsValues . indexOf ( precondition . exists ) < 0
691
+ ) {
689
692
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 ( ', ' ) } )`
694
696
) ;
695
697
}
696
698
}
@@ -733,7 +735,7 @@ function validateUpdatePrecondition(
733
735
options ?: RequiredArgumentOptions
734
736
) : asserts value is { lastUpdateTime ?: Timestamp } {
735
737
if ( ! validateOptional ( value , options ) ) {
736
- validatePrecondition ( arg , value , /* allowExists= */ false ) ;
738
+ validatePrecondition ( arg , value , { allowedExistsValues : [ true ] } ) ;
737
739
}
738
740
}
739
741
@@ -753,7 +755,7 @@ function validateDeletePrecondition(
753
755
options ?: RequiredArgumentOptions
754
756
) : void {
755
757
if ( ! validateOptional ( value , options ) ) {
756
- validatePrecondition ( arg , value , /* allowExists= */ true ) ;
758
+ validatePrecondition ( arg , value ) ;
757
759
}
758
760
}
759
761
0 commit comments