@@ -19,70 +19,112 @@ import { isPlainObject } from '../util/input_validation';
1919
2020import { Code , FirestoreError } from './error' ;
2121
22- /** A list of data types Firestore objects may serialize in their toJSON implemenetations. */
23- export type JsonTypeDesc = "string" | "number" | "boolean" | "null" | "undefined" ;
22+ /**
23+ * A list of data types Firestore objects may serialize in their toJSON implemenetations.
24+ * @private
25+ * @internal
26+ */
27+ export type JsonTypeDesc =
28+ | 'string'
29+ | 'number'
30+ | 'boolean'
31+ | 'null'
32+ | 'undefined' ;
2433
25- /** An association of JsonTypeDesc values to their native types. */
26- type TSType < T extends JsonTypeDesc > =
27- T extends "string" ? string :
28- T extends "number" ? number :
29- T extends "boolean" ? boolean :
30- T extends "null" ? null :
31- T extends "undefined" ? undefined :
32- never ;
34+ /**
35+ * An association of JsonTypeDesc values to their native types.
36+ * @private
37+ * @internal
38+ */
39+ export type TSType < T extends JsonTypeDesc > = T extends 'string'
40+ ? string
41+ : T extends 'number'
42+ ? number
43+ : T extends 'boolean'
44+ ? boolean
45+ : T extends 'null'
46+ ? null
47+ : T extends 'undefined'
48+ ? undefined
49+ : never ;
3350
34- /** The representation of a JSON object property name and its type value. */
51+ /**
52+ * The representation of a JSON object property name and its type value.
53+ * @private
54+ * @internal
55+ */
3556export interface Property < T extends JsonTypeDesc > {
3657 value ?: TSType < T > ;
3758 typeString : JsonTypeDesc ;
38- } ;
59+ }
3960
40- /** A type Firestore data types may use to define the fields used in their JSON serialization. */
61+ /**
62+ * A type Firestore data types may use to define the fields used in their JSON serialization.
63+ * @private
64+ * @internal
65+ */
4166export interface JsonSchema {
4267 [ key : string ] : Property < JsonTypeDesc > ;
43- } ;
68+ }
4469
45- /** Associates the JSON property type to the native type and sets them to be Required. */
70+ /**
71+ * Associates the JSON property type to the native type and sets them to be Required.
72+ * @private
73+ * @internal
74+ */
4675export type Json < T extends JsonSchema > = {
47- [ K in keyof T ] : Required < T [ K ] > [ 'value' ]
76+ [ K in keyof T ] : Required < T [ K ] > [ 'value' ] ;
4877} ;
4978
50- /** Helper function to define a JSON schema {@link Property} */
51- export function property < T extends JsonTypeDesc > ( typeString : T , optionalValue ?: TSType < T > ) : Property < T > {
79+ /**
80+ * Helper function to define a JSON schema {@link Property}.
81+ * @private
82+ * @internal
83+ */
84+ export function property < T extends JsonTypeDesc > (
85+ typeString : T ,
86+ optionalValue ?: TSType < T >
87+ ) : Property < T > {
5288 const result : Property < T > = {
5389 typeString
5490 } ;
5591 if ( optionalValue ) {
5692 result . value = optionalValue ;
5793 }
5894 return result ;
59- } ;
95+ }
6096
6197/** Validates the JSON object based on the provided schema, and narrows the type to the provided
6298 * JSON schaem.
63- *
99+ * @private
100+ * @internal
101+ *
64102 * @param json A JSON object to validate.
65103 * @param scheme a {@link JsonSchema} that defines the properties to validate.
66104 * @returns true if the JSON schema exists within the object. Throws a FirestoreError otherwise.
67105 */
68- export function validateJSON < S extends JsonSchema > ( json : object , schema : S ) : json is Json < S > {
106+ export function validateJSON < S extends JsonSchema > (
107+ json : object ,
108+ schema : S
109+ ) : json is Json < S > {
69110 if ( ! isPlainObject ( json ) ) {
70- throw new FirestoreError ( Code . INVALID_ARGUMENT , " json must be an object" ) ;
111+ throw new FirestoreError ( Code . INVALID_ARGUMENT , ' json must be an object' ) ;
71112 }
72113 let error : string | undefined = undefined ;
73114 for ( const key in schema ) {
74115 if ( schema [ key ] ) {
75116 const typeString = schema [ key ] . typeString ;
76- const value : { value : unknown } | undefined = ( 'value' in schema [ key ] ) ? { value : schema [ key ] . value } : undefined ;
117+ const value : { value : unknown } | undefined =
118+ 'value' in schema [ key ] ? { value : schema [ key ] . value } : undefined ;
77119 if ( ! ( key in json ) ) {
78120 error = `json missing required field: ${ key } ` ;
79121 }
80122 // eslint-disable-next-line @typescript-eslint/no-explicit-any
81123 const fieldValue = ( json as any ) [ key ] ;
82- if ( typeString && ( ( typeof fieldValue ) !== typeString ) ) {
124+ if ( typeString && typeof fieldValue !== typeString ) {
83125 error = `json field '${ key } ' must be a ${ typeString } .` ;
84126 break ;
85- } else if ( ( value !== undefined ) && fieldValue !== value . value ) {
127+ } else if ( value !== undefined && fieldValue !== value . value ) {
86128 error = `Expected '${ key } ' field to equal '${ value . value } '` ;
87129 break ;
88130 }
@@ -92,4 +134,4 @@ export function validateJSON<S extends JsonSchema>(json: object, schema: S): jso
92134 throw new FirestoreError ( Code . INVALID_ARGUMENT , error ) ;
93135 }
94136 return true ;
95- }
137+ }
0 commit comments