@@ -19,70 +19,112 @@ import { isPlainObject } from '../util/input_validation';
19
19
20
20
import { Code , FirestoreError } from './error' ;
21
21
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' ;
24
33
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 ;
33
50
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
+ */
35
56
export interface Property < T extends JsonTypeDesc > {
36
57
value ?: TSType < T > ;
37
58
typeString : JsonTypeDesc ;
38
- } ;
59
+ }
39
60
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
+ */
41
66
export interface JsonSchema {
42
67
[ key : string ] : Property < JsonTypeDesc > ;
43
- } ;
68
+ }
44
69
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
+ */
46
75
export type Json < T extends JsonSchema > = {
47
- [ K in keyof T ] : Required < T [ K ] > [ 'value' ]
76
+ [ K in keyof T ] : Required < T [ K ] > [ 'value' ] ;
48
77
} ;
49
78
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 > {
52
88
const result : Property < T > = {
53
89
typeString
54
90
} ;
55
91
if ( optionalValue ) {
56
92
result . value = optionalValue ;
57
93
}
58
94
return result ;
59
- } ;
95
+ }
60
96
61
97
/** Validates the JSON object based on the provided schema, and narrows the type to the provided
62
98
* JSON schaem.
63
- *
99
+ * @private
100
+ * @internal
101
+ *
64
102
* @param json A JSON object to validate.
65
103
* @param scheme a {@link JsonSchema} that defines the properties to validate.
66
104
* @returns true if the JSON schema exists within the object. Throws a FirestoreError otherwise.
67
105
*/
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 > {
69
110
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' ) ;
71
112
}
72
113
let error : string | undefined = undefined ;
73
114
for ( const key in schema ) {
74
115
if ( schema [ key ] ) {
75
116
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 ;
77
119
if ( ! ( key in json ) ) {
78
120
error = `json missing required field: ${ key } ` ;
79
121
}
80
122
// eslint-disable-next-line @typescript-eslint/no-explicit-any
81
123
const fieldValue = ( json as any ) [ key ] ;
82
- if ( typeString && ( ( typeof fieldValue ) !== typeString ) ) {
124
+ if ( typeString && typeof fieldValue !== typeString ) {
83
125
error = `json field '${ key } ' must be a ${ typeString } .` ;
84
126
break ;
85
- } else if ( ( value !== undefined ) && fieldValue !== value . value ) {
127
+ } else if ( value !== undefined && fieldValue !== value . value ) {
86
128
error = `Expected '${ key } ' field to equal '${ value . value } '` ;
87
129
break ;
88
130
}
@@ -92,4 +134,4 @@ export function validateJSON<S extends JsonSchema>(json: object, schema: S): jso
92
134
throw new FirestoreError ( Code . INVALID_ARGUMENT , error ) ;
93
135
}
94
136
return true ;
95
- }
137
+ }
0 commit comments