1
- import deepEquals from ' ./deepEquals' ;
1
+ import deepEquals from " ./deepEquals" ;
2
2
import {
3
3
ErrorSchema ,
4
4
Experimental_DefaultFormStateBehavior ,
@@ -12,11 +12,11 @@ import {
12
12
UiSchema ,
13
13
ValidationData ,
14
14
ValidatorType ,
15
- } from ' ./types' ;
15
+ } from " ./types" ;
16
16
import {
17
+ getClosestMatchingOption ,
17
18
getDefaultFormState ,
18
19
getDisplayLabel ,
19
- getClosestMatchingOption ,
20
20
getFirstMatchingOption ,
21
21
getMatchingOption ,
22
22
isFilesArray ,
@@ -27,16 +27,18 @@ import {
27
27
sanitizeDataForNewSchema ,
28
28
toIdSchema ,
29
29
toPathSchema ,
30
- } from ' ./schema' ;
30
+ } from " ./schema" ;
31
31
32
32
/** The `SchemaUtils` class provides a wrapper around the publicly exported APIs in the `utils/schema` directory such
33
33
* that one does not have to explicitly pass the `validator`, `rootSchema`, or `experimental_defaultFormStateBehavior` to each method.
34
34
* Since these generally do not change across a `Form`, this allows for providing a simplified set of APIs to the
35
35
* `@rjsf/core` components and the various themes as well. This class implements the `SchemaUtilsType` interface.
36
36
*/
37
- class SchemaUtils < T = any , S extends StrictRJSFSchema = RJSFSchema , F extends FormContextType = any >
38
- implements SchemaUtilsType < T , S , F >
39
- {
37
+ class SchemaUtils <
38
+ T = any ,
39
+ S extends StrictRJSFSchema = RJSFSchema ,
40
+ F extends FormContextType = any ,
41
+ > implements SchemaUtilsType < T , S , F > {
40
42
rootSchema : S ;
41
43
validator : ValidatorType < T , S , F > ;
42
44
experimental_defaultFormStateBehavior : Experimental_DefaultFormStateBehavior ;
@@ -50,11 +52,13 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
50
52
constructor (
51
53
validator : ValidatorType < T , S , F > ,
52
54
rootSchema : S ,
53
- experimental_defaultFormStateBehavior : Experimental_DefaultFormStateBehavior
55
+ experimental_defaultFormStateBehavior :
56
+ Experimental_DefaultFormStateBehavior ,
54
57
) {
55
58
this . rootSchema = rootSchema ;
56
59
this . validator = validator ;
57
- this . experimental_defaultFormStateBehavior = experimental_defaultFormStateBehavior ;
60
+ this . experimental_defaultFormStateBehavior =
61
+ experimental_defaultFormStateBehavior ;
58
62
}
59
63
60
64
/** Returns the `ValidatorType` in the `SchemaUtilsType`
@@ -77,15 +81,18 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
77
81
doesSchemaUtilsDiffer (
78
82
validator : ValidatorType < T , S , F > ,
79
83
rootSchema : S ,
80
- experimental_defaultFormStateBehavior = { }
84
+ experimental_defaultFormStateBehavior = { } ,
81
85
) : boolean {
82
86
if ( ! validator || ! rootSchema ) {
83
87
return false ;
84
88
}
85
89
return (
86
90
this . validator !== validator ||
87
91
! deepEquals ( this . rootSchema , rootSchema ) ||
88
- ! deepEquals ( this . experimental_defaultFormStateBehavior , experimental_defaultFormStateBehavior )
92
+ ! deepEquals (
93
+ this . experimental_defaultFormStateBehavior ,
94
+ experimental_defaultFormStateBehavior ,
95
+ )
89
96
) ;
90
97
}
91
98
@@ -102,15 +109,15 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
102
109
getDefaultFormState (
103
110
schema : S ,
104
111
formData ?: T ,
105
- includeUndefinedValues : boolean | ' excludeObjectChildren' = false
112
+ includeUndefinedValues : boolean | " excludeObjectChildren" = false ,
106
113
) : T | T [ ] | undefined {
107
114
return getDefaultFormState < T , S , F > (
108
115
this . validator ,
109
116
schema ,
110
117
formData ,
111
118
this . rootSchema ,
112
119
includeUndefinedValues ,
113
- this . experimental_defaultFormStateBehavior
120
+ this . experimental_defaultFormStateBehavior ,
114
121
) ;
115
122
}
116
123
@@ -122,8 +129,18 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
122
129
* @param [globalOptions={ }] - The optional Global UI Schema from which to get any fallback `xxx` options
123
130
* @returns - True if the label should be displayed or false if it should not
124
131
*/
125
- getDisplayLabel ( schema : S , uiSchema ?: UiSchema < T , S , F > , globalOptions ?: GlobalUISchemaOptions ) {
126
- return getDisplayLabel < T , S , F > ( this . validator , schema , uiSchema , this . rootSchema , globalOptions ) ;
132
+ getDisplayLabel (
133
+ schema : S ,
134
+ uiSchema ?: UiSchema < T , S , F > ,
135
+ globalOptions ?: GlobalUISchemaOptions ,
136
+ ) {
137
+ return getDisplayLabel < T , S , F > (
138
+ this . validator ,
139
+ schema ,
140
+ uiSchema ,
141
+ this . rootSchema ,
142
+ globalOptions ,
143
+ ) ;
127
144
}
128
145
129
146
/** Determines which of the given `options` provided most closely matches the `formData`.
@@ -143,15 +160,15 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
143
160
formData : T | undefined ,
144
161
options : S [ ] ,
145
162
selectedOption ?: number ,
146
- discriminatorField ?: string
163
+ discriminatorField ?: string ,
147
164
) : number {
148
165
return getClosestMatchingOption < T , S , F > (
149
166
this . validator ,
150
167
this . rootSchema ,
151
168
formData ,
152
169
options ,
153
170
selectedOption ,
154
- discriminatorField
171
+ discriminatorField ,
155
172
) ;
156
173
}
157
174
@@ -164,8 +181,18 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
164
181
* determine which option is selected
165
182
* @returns - The firstindex of the matched option or 0 if none is available
166
183
*/
167
- getFirstMatchingOption ( formData : T | undefined , options : S [ ] , discriminatorField ?: string ) : number {
168
- return getFirstMatchingOption < T , S , F > ( this . validator , formData , options , this . rootSchema , discriminatorField ) ;
184
+ getFirstMatchingOption (
185
+ formData : T | undefined ,
186
+ options : S [ ] ,
187
+ discriminatorField ?: string ,
188
+ ) : number {
189
+ return getFirstMatchingOption < T , S , F > (
190
+ this . validator ,
191
+ formData ,
192
+ options ,
193
+ this . rootSchema ,
194
+ discriminatorField ,
195
+ ) ;
169
196
}
170
197
171
198
/** Given the `formData` and list of `options`, attempts to find the index of the option that best matches the data.
@@ -178,8 +205,18 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
178
205
* @returns - The index of the matched option or 0 if none is available
179
206
* @deprecated
180
207
*/
181
- getMatchingOption ( formData : T | undefined , options : S [ ] , discriminatorField ?: string ) {
182
- return getMatchingOption < T , S , F > ( this . validator , formData , options , this . rootSchema , discriminatorField ) ;
208
+ getMatchingOption (
209
+ formData : T | undefined ,
210
+ options : S [ ] ,
211
+ discriminatorField ?: string ,
212
+ ) {
213
+ return getMatchingOption < T , S , F > (
214
+ this . validator ,
215
+ formData ,
216
+ options ,
217
+ this . rootSchema ,
218
+ discriminatorField ,
219
+ ) ;
183
220
}
184
221
185
222
/** Checks to see if the `schema` and `uiSchema` combination represents an array of files
@@ -189,7 +226,12 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
189
226
* @returns - True if schema/uiSchema contains an array of files, otherwise false
190
227
*/
191
228
isFilesArray ( schema : S , uiSchema ?: UiSchema < T , S , F > ) {
192
- return isFilesArray < T , S , F > ( this . validator , schema , uiSchema , this . rootSchema ) ;
229
+ return isFilesArray < T , S , F > (
230
+ this . validator ,
231
+ schema ,
232
+ uiSchema ,
233
+ this . rootSchema ,
234
+ ) ;
193
235
}
194
236
195
237
/** Checks to see if the `schema` combination represents a multi-select
@@ -221,8 +263,15 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
221
263
* @deprecated - Use the `validationDataMerge()` function exported from `@rjsf/utils` instead. This function will be
222
264
* removed in the next major release.
223
265
*/
224
- mergeValidationData ( validationData : ValidationData < T > , additionalErrorSchema ?: ErrorSchema < T > ) : ValidationData < T > {
225
- return mergeValidationData < T , S , F > ( this . validator , validationData , additionalErrorSchema ) ;
266
+ mergeValidationData (
267
+ validationData : ValidationData < T > ,
268
+ additionalErrorSchema ?: ErrorSchema < T > ,
269
+ ) : ValidationData < T > {
270
+ return mergeValidationData < T , S , F > (
271
+ this . validator ,
272
+ validationData ,
273
+ additionalErrorSchema ,
274
+ ) ;
226
275
}
227
276
228
277
/** Retrieves an expanded schema that has had all of its conditions, additional properties, references and
@@ -234,7 +283,12 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
234
283
* @returns - The schema having its conditions, additional properties, references and dependencies resolved
235
284
*/
236
285
retrieveSchema ( schema : S , rawFormData ?: T ) {
237
- return retrieveSchema < T , S , F > ( this . validator , schema , this . rootSchema , rawFormData ) ;
286
+ return retrieveSchema < T , S , F > (
287
+ this . validator ,
288
+ schema ,
289
+ this . rootSchema ,
290
+ rawFormData ,
291
+ ) ;
238
292
}
239
293
240
294
/** Sanitize the `data` associated with the `oldSchema` so it is considered appropriate for the `newSchema`. If the
@@ -249,7 +303,13 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
249
303
* to `undefined`. Will return `undefined` if the new schema is not an object containing properties.
250
304
*/
251
305
sanitizeDataForNewSchema ( newSchema ?: S , oldSchema ?: S , data ?: any ) : T {
252
- return sanitizeDataForNewSchema ( this . validator , this . rootSchema , newSchema , oldSchema , data ) ;
306
+ return sanitizeDataForNewSchema (
307
+ this . validator ,
308
+ this . rootSchema ,
309
+ newSchema ,
310
+ oldSchema ,
311
+ data ,
312
+ ) ;
253
313
}
254
314
255
315
/** Generates an `IdSchema` object for the `schema`, recursively
@@ -261,8 +321,22 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
261
321
* @param [idSeparator='_'] - The separator to use for the path segments in the id
262
322
* @returns - The `IdSchema` object for the `schema`
263
323
*/
264
- toIdSchema ( schema : S , id ?: string | null , formData ?: T , idPrefix = 'root' , idSeparator = '_' ) : IdSchema < T > {
265
- return toIdSchema < T , S , F > ( this . validator , schema , id , this . rootSchema , formData , idPrefix , idSeparator ) ;
324
+ toIdSchema (
325
+ schema : S ,
326
+ id ?: string | null ,
327
+ formData ?: T ,
328
+ idPrefix = "root" ,
329
+ idSeparator = "_" ,
330
+ ) : IdSchema < T > {
331
+ return toIdSchema < T , S , F > (
332
+ this . validator ,
333
+ schema ,
334
+ id ,
335
+ this . rootSchema ,
336
+ formData ,
337
+ idPrefix ,
338
+ idSeparator ,
339
+ ) ;
266
340
}
267
341
268
342
/** Generates an `PathSchema` object for the `schema`, recursively
@@ -273,7 +347,13 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
273
347
* @returns - The `PathSchema` object for the `schema`
274
348
*/
275
349
toPathSchema ( schema : S , name ?: string , formData ?: T ) : PathSchema < T > {
276
- return toPathSchema < T , S , F > ( this . validator , schema , name , this . rootSchema , formData ) ;
350
+ return toPathSchema < T , S , F > (
351
+ this . validator ,
352
+ schema ,
353
+ name ,
354
+ this . rootSchema ,
355
+ formData ,
356
+ ) ;
277
357
}
278
358
}
279
359
@@ -288,11 +368,15 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
288
368
export default function createSchemaUtils <
289
369
T = any ,
290
370
S extends StrictRJSFSchema = RJSFSchema ,
291
- F extends FormContextType = any
371
+ F extends FormContextType = any ,
292
372
> (
293
373
validator : ValidatorType < T , S , F > ,
294
374
rootSchema : S ,
295
- experimental_defaultFormStateBehavior = { }
375
+ experimental_defaultFormStateBehavior = { } ,
296
376
) : SchemaUtilsType < T , S , F > {
297
- return new SchemaUtils < T , S , F > ( validator , rootSchema , experimental_defaultFormStateBehavior ) ;
377
+ return new SchemaUtils < T , S , F > (
378
+ validator ,
379
+ rootSchema ,
380
+ experimental_defaultFormStateBehavior ,
381
+ ) ;
298
382
}
0 commit comments