@rjsf/utils 5.10.0 → 5.11.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/utils.cjs.development.js +782 -969
- package/dist/utils.cjs.development.js.map +1 -1
- package/dist/utils.cjs.production.min.js +1 -1
- package/dist/utils.cjs.production.min.js.map +1 -1
- package/dist/utils.esm.js +781 -969
- package/dist/utils.esm.js.map +1 -1
- package/dist/utils.umd.development.js +785 -973
- package/dist/utils.umd.development.js.map +1 -1
- package/dist/utils.umd.production.min.js +1 -1
- package/dist/utils.umd.production.min.js.map +1 -1
- package/package.json +9 -8
|
@@ -14,6 +14,7 @@ var isString = require('lodash/isString');
|
|
|
14
14
|
var reduce = require('lodash/reduce');
|
|
15
15
|
var times = require('lodash/times');
|
|
16
16
|
var set = require('lodash/set');
|
|
17
|
+
var transform = require('lodash/transform');
|
|
17
18
|
var mergeAllOf = require('json-schema-merge-allof');
|
|
18
19
|
var union = require('lodash/union');
|
|
19
20
|
var isEqual = require('lodash/isEqual');
|
|
@@ -39,6 +40,7 @@ var isString__default = /*#__PURE__*/_interopDefaultLegacy(isString);
|
|
|
39
40
|
var reduce__default = /*#__PURE__*/_interopDefaultLegacy(reduce);
|
|
40
41
|
var times__default = /*#__PURE__*/_interopDefaultLegacy(times);
|
|
41
42
|
var set__default = /*#__PURE__*/_interopDefaultLegacy(set);
|
|
43
|
+
var transform__default = /*#__PURE__*/_interopDefaultLegacy(transform);
|
|
42
44
|
var mergeAllOf__default = /*#__PURE__*/_interopDefaultLegacy(mergeAllOf);
|
|
43
45
|
var union__default = /*#__PURE__*/_interopDefaultLegacy(union);
|
|
44
46
|
var isEqual__default = /*#__PURE__*/_interopDefaultLegacy(isEqual);
|
|
@@ -107,103 +109,42 @@ function asNumber(value) {
|
|
|
107
109
|
// specific precision or number of significant digits)
|
|
108
110
|
return value;
|
|
109
111
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
+
const n = Number(value);
|
|
113
|
+
const valid = typeof n === 'number' && !Number.isNaN(n);
|
|
112
114
|
return valid ? n : value;
|
|
113
115
|
}
|
|
114
116
|
|
|
115
|
-
function _defineProperties(target, props) {
|
|
116
|
-
for (var i = 0; i < props.length; i++) {
|
|
117
|
-
var descriptor = props[i];
|
|
118
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
|
119
|
-
descriptor.configurable = true;
|
|
120
|
-
if ("value" in descriptor) descriptor.writable = true;
|
|
121
|
-
Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
function _createClass(Constructor, protoProps, staticProps) {
|
|
125
|
-
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
126
|
-
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
127
|
-
Object.defineProperty(Constructor, "prototype", {
|
|
128
|
-
writable: false
|
|
129
|
-
});
|
|
130
|
-
return Constructor;
|
|
131
|
-
}
|
|
132
|
-
function _extends() {
|
|
133
|
-
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
134
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
135
|
-
var source = arguments[i];
|
|
136
|
-
for (var key in source) {
|
|
137
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
138
|
-
target[key] = source[key];
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
return target;
|
|
143
|
-
};
|
|
144
|
-
return _extends.apply(this, arguments);
|
|
145
|
-
}
|
|
146
|
-
function _objectDestructuringEmpty(obj) {
|
|
147
|
-
if (obj == null) throw new TypeError("Cannot destructure " + obj);
|
|
148
|
-
}
|
|
149
|
-
function _objectWithoutPropertiesLoose(source, excluded) {
|
|
150
|
-
if (source == null) return {};
|
|
151
|
-
var target = {};
|
|
152
|
-
var sourceKeys = Object.keys(source);
|
|
153
|
-
var key, i;
|
|
154
|
-
for (i = 0; i < sourceKeys.length; i++) {
|
|
155
|
-
key = sourceKeys[i];
|
|
156
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
157
|
-
target[key] = source[key];
|
|
158
|
-
}
|
|
159
|
-
return target;
|
|
160
|
-
}
|
|
161
|
-
function _toPrimitive(input, hint) {
|
|
162
|
-
if (typeof input !== "object" || input === null) return input;
|
|
163
|
-
var prim = input[Symbol.toPrimitive];
|
|
164
|
-
if (prim !== undefined) {
|
|
165
|
-
var res = prim.call(input, hint || "default");
|
|
166
|
-
if (typeof res !== "object") return res;
|
|
167
|
-
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
168
|
-
}
|
|
169
|
-
return (hint === "string" ? String : Number)(input);
|
|
170
|
-
}
|
|
171
|
-
function _toPropertyKey(arg) {
|
|
172
|
-
var key = _toPrimitive(arg, "string");
|
|
173
|
-
return typeof key === "symbol" ? key : String(key);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
117
|
/** Below are the list of all the keys into various elements of a RJSFSchema or UiSchema that are used by the various
|
|
177
118
|
* utility functions. In addition to those keys, there are the special `ADDITIONAL_PROPERTY_FLAG` and
|
|
178
119
|
* `RJSF_ADDITONAL_PROPERTIES_FLAG` flags that is added to a schema under certain conditions by the `retrieveSchema()`
|
|
179
120
|
* utility.
|
|
180
121
|
*/
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
122
|
+
const ADDITIONAL_PROPERTY_FLAG = '__additional_property';
|
|
123
|
+
const ADDITIONAL_PROPERTIES_KEY = 'additionalProperties';
|
|
124
|
+
const ALL_OF_KEY = 'allOf';
|
|
125
|
+
const ANY_OF_KEY = 'anyOf';
|
|
126
|
+
const CONST_KEY = 'const';
|
|
127
|
+
const DEFAULT_KEY = 'default';
|
|
128
|
+
const DEFINITIONS_KEY = 'definitions';
|
|
129
|
+
const DEPENDENCIES_KEY = 'dependencies';
|
|
130
|
+
const ENUM_KEY = 'enum';
|
|
131
|
+
const ERRORS_KEY = '__errors';
|
|
132
|
+
const ID_KEY = '$id';
|
|
133
|
+
const IF_KEY = 'if';
|
|
134
|
+
const ITEMS_KEY = 'items';
|
|
135
|
+
const JUNK_OPTION_ID = '_$junk_option_schema_id$_';
|
|
136
|
+
const NAME_KEY = '$name';
|
|
137
|
+
const ONE_OF_KEY = 'oneOf';
|
|
138
|
+
const PROPERTIES_KEY = 'properties';
|
|
139
|
+
const REQUIRED_KEY = 'required';
|
|
140
|
+
const SUBMIT_BTN_OPTIONS_KEY = 'submitButtonOptions';
|
|
141
|
+
const REF_KEY = '$ref';
|
|
142
|
+
const RJSF_ADDITONAL_PROPERTIES_FLAG = '__rjsf_additionalProperties';
|
|
143
|
+
const ROOT_SCHEMA_PREFIX = '__rjsf_rootSchema';
|
|
144
|
+
const UI_FIELD_KEY = 'ui:field';
|
|
145
|
+
const UI_WIDGET_KEY = 'ui:widget';
|
|
146
|
+
const UI_OPTIONS_KEY = 'ui:options';
|
|
147
|
+
const UI_GLOBAL_OPTIONS_KEY = 'ui:globalOptions';
|
|
207
148
|
|
|
208
149
|
/** Get all passed options from ui:options, and ui:<optionName>, returning them in an object with the `ui:`
|
|
209
150
|
* stripped off. Any `globalOptions` will always be returned, unless they are overridden by options in the `uiSchema`.
|
|
@@ -212,27 +153,26 @@ var UI_GLOBAL_OPTIONS_KEY = 'ui:globalOptions';
|
|
|
212
153
|
* @param [globalOptions={}] - The optional Global UI Schema from which to get any fallback `xxx` options
|
|
213
154
|
* @returns - An object containing all the `ui:xxx` options with the `ui:` stripped off along with all `globalOptions`
|
|
214
155
|
*/
|
|
215
|
-
function getUiOptions(uiSchema, globalOptions) {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
219
|
-
if (globalOptions === void 0) {
|
|
220
|
-
globalOptions = {};
|
|
221
|
-
}
|
|
222
|
-
return Object.keys(uiSchema).filter(function (key) {
|
|
223
|
-
return key.indexOf('ui:') === 0;
|
|
224
|
-
}).reduce(function (options, key) {
|
|
225
|
-
var _extends2;
|
|
226
|
-
var value = uiSchema[key];
|
|
156
|
+
function getUiOptions(uiSchema = {}, globalOptions = {}) {
|
|
157
|
+
return Object.keys(uiSchema).filter(key => key.indexOf('ui:') === 0).reduce((options, key) => {
|
|
158
|
+
const value = uiSchema[key];
|
|
227
159
|
if (key === UI_WIDGET_KEY && isObject(value)) {
|
|
228
160
|
console.error('Setting options via ui:widget object is no longer supported, use ui:options instead');
|
|
229
161
|
return options;
|
|
230
162
|
}
|
|
231
163
|
if (key === UI_OPTIONS_KEY && isObject(value)) {
|
|
232
|
-
return
|
|
164
|
+
return {
|
|
165
|
+
...options,
|
|
166
|
+
...value
|
|
167
|
+
};
|
|
233
168
|
}
|
|
234
|
-
return
|
|
235
|
-
|
|
169
|
+
return {
|
|
170
|
+
...options,
|
|
171
|
+
[key.substring(3)]: value
|
|
172
|
+
};
|
|
173
|
+
}, {
|
|
174
|
+
...globalOptions
|
|
175
|
+
});
|
|
236
176
|
}
|
|
237
177
|
|
|
238
178
|
/** Checks whether the field described by `schema`, having the `uiSchema` and `formData` supports expanding. The UI for
|
|
@@ -244,16 +184,13 @@ function getUiOptions(uiSchema, globalOptions) {
|
|
|
244
184
|
* @param [formData] - The formData for the field
|
|
245
185
|
* @returns - True if the schema element has additionalProperties, is expandable, and not at the maxProperties limit
|
|
246
186
|
*/
|
|
247
|
-
function canExpand(schema, uiSchema, formData) {
|
|
248
|
-
if (uiSchema === void 0) {
|
|
249
|
-
uiSchema = {};
|
|
250
|
-
}
|
|
187
|
+
function canExpand(schema, uiSchema = {}, formData) {
|
|
251
188
|
if (!schema.additionalProperties) {
|
|
252
189
|
return false;
|
|
253
190
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
191
|
+
const {
|
|
192
|
+
expandable = true
|
|
193
|
+
} = getUiOptions(uiSchema);
|
|
257
194
|
if (expandable === false) {
|
|
258
195
|
return expandable;
|
|
259
196
|
}
|
|
@@ -271,21 +208,30 @@ function canExpand(schema, uiSchema, formData) {
|
|
|
271
208
|
* @returns - A `FormValidation` object based on the `formData` structure
|
|
272
209
|
*/
|
|
273
210
|
function createErrorHandler(formData) {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
211
|
+
const handler = {
|
|
212
|
+
// We store the list of errors for this node in a property named __errors
|
|
213
|
+
// to avoid name collision with a possible sub schema field named
|
|
214
|
+
// 'errors' (see `utils.toErrorSchema`).
|
|
215
|
+
[ERRORS_KEY]: [],
|
|
216
|
+
addError(message) {
|
|
217
|
+
this[ERRORS_KEY].push(message);
|
|
218
|
+
}
|
|
219
|
+
};
|
|
278
220
|
if (Array.isArray(formData)) {
|
|
279
|
-
return formData.reduce(
|
|
280
|
-
|
|
281
|
-
|
|
221
|
+
return formData.reduce((acc, value, key) => {
|
|
222
|
+
return {
|
|
223
|
+
...acc,
|
|
224
|
+
[key]: createErrorHandler(value)
|
|
225
|
+
};
|
|
282
226
|
}, handler);
|
|
283
227
|
}
|
|
284
228
|
if (isPlainObject__default["default"](formData)) {
|
|
285
|
-
|
|
286
|
-
return Object.keys(formObject).reduce(
|
|
287
|
-
|
|
288
|
-
|
|
229
|
+
const formObject = formData;
|
|
230
|
+
return Object.keys(formObject).reduce((acc, key) => {
|
|
231
|
+
return {
|
|
232
|
+
...acc,
|
|
233
|
+
[key]: createErrorHandler(formObject[key])
|
|
234
|
+
};
|
|
289
235
|
}, handler);
|
|
290
236
|
}
|
|
291
237
|
return handler;
|
|
@@ -299,7 +245,7 @@ function createErrorHandler(formData) {
|
|
|
299
245
|
* @returns - True if the `a` and `b` are deeply equal, false otherwise
|
|
300
246
|
*/
|
|
301
247
|
function deepEquals(a, b) {
|
|
302
|
-
return isEqualWith__default["default"](a, b,
|
|
248
|
+
return isEqualWith__default["default"](a, b, (obj, other) => {
|
|
303
249
|
if (typeof obj === 'function' && typeof other === 'function') {
|
|
304
250
|
// Assume all functions are equivalent
|
|
305
251
|
// see https://github.com/rjsf-team/react-jsonschema-form/issues/255
|
|
@@ -318,8 +264,8 @@ function deepEquals(a, b) {
|
|
|
318
264
|
* value from `object[key]`
|
|
319
265
|
*/
|
|
320
266
|
function splitKeyElementFromObject(key, object) {
|
|
321
|
-
|
|
322
|
-
|
|
267
|
+
const value = object[key];
|
|
268
|
+
const remaining = omit__default["default"](object, [key]);
|
|
323
269
|
return [remaining, value];
|
|
324
270
|
}
|
|
325
271
|
/** Given the name of a `$ref` from within a schema, using the `rootSchema`, look up and return the sub-schema using the
|
|
@@ -331,28 +277,26 @@ function splitKeyElementFromObject(key, object) {
|
|
|
331
277
|
* @returns - The sub-schema within the `rootSchema` which matches the `$ref` if it exists
|
|
332
278
|
* @throws - Error indicating that no schema for that reference exists
|
|
333
279
|
*/
|
|
334
|
-
function findSchemaDefinition($ref, rootSchema) {
|
|
335
|
-
|
|
336
|
-
rootSchema = {};
|
|
337
|
-
}
|
|
338
|
-
var ref = $ref || '';
|
|
280
|
+
function findSchemaDefinition($ref, rootSchema = {}) {
|
|
281
|
+
let ref = $ref || '';
|
|
339
282
|
if (ref.startsWith('#')) {
|
|
340
283
|
// Decode URI fragment representation.
|
|
341
284
|
ref = decodeURIComponent(ref.substring(1));
|
|
342
285
|
} else {
|
|
343
|
-
throw new Error(
|
|
286
|
+
throw new Error(`Could not find a definition for ${$ref}.`);
|
|
344
287
|
}
|
|
345
|
-
|
|
288
|
+
const current = jsonpointer__default["default"].get(rootSchema, ref);
|
|
346
289
|
if (current === undefined) {
|
|
347
|
-
throw new Error(
|
|
290
|
+
throw new Error(`Could not find a definition for ${$ref}.`);
|
|
348
291
|
}
|
|
349
292
|
if (current[REF_KEY]) {
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
theRef = _splitKeyElementFromO[1];
|
|
353
|
-
var subSchema = findSchemaDefinition(theRef, rootSchema);
|
|
293
|
+
const [remaining, theRef] = splitKeyElementFromObject(REF_KEY, current);
|
|
294
|
+
const subSchema = findSchemaDefinition(theRef, rootSchema);
|
|
354
295
|
if (Object.keys(remaining).length > 0) {
|
|
355
|
-
return
|
|
296
|
+
return {
|
|
297
|
+
...remaining,
|
|
298
|
+
...subSchema
|
|
299
|
+
};
|
|
356
300
|
}
|
|
357
301
|
return subSchema;
|
|
358
302
|
}
|
|
@@ -377,12 +321,12 @@ function getMatchingOption(validator, formData, options, rootSchema, discriminat
|
|
|
377
321
|
if (formData === undefined) {
|
|
378
322
|
return 0;
|
|
379
323
|
}
|
|
380
|
-
for (
|
|
381
|
-
|
|
324
|
+
for (let i = 0; i < options.length; i++) {
|
|
325
|
+
const option = options[i];
|
|
382
326
|
// If we have a discriminator field, then we will use this to make the determination
|
|
383
327
|
if (discriminatorField && has__default["default"](option, [PROPERTIES_KEY, discriminatorField])) {
|
|
384
|
-
|
|
385
|
-
|
|
328
|
+
const value = get__default["default"](formData, discriminatorField);
|
|
329
|
+
const discriminator = get__default["default"](option, [PROPERTIES_KEY, discriminatorField], {});
|
|
386
330
|
if (validator.isValid(discriminator, value, rootSchema)) {
|
|
387
331
|
return i;
|
|
388
332
|
}
|
|
@@ -397,18 +341,18 @@ function getMatchingOption(validator, formData, options, rootSchema, discriminat
|
|
|
397
341
|
//
|
|
398
342
|
// Create an "anyOf" schema that requires at least one of the keys in the
|
|
399
343
|
// "properties" object
|
|
400
|
-
|
|
401
|
-
anyOf: Object.keys(option[PROPERTIES_KEY]).map(
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
};
|
|
405
|
-
})
|
|
344
|
+
const requiresAnyOf = {
|
|
345
|
+
anyOf: Object.keys(option[PROPERTIES_KEY]).map(key => ({
|
|
346
|
+
required: [key]
|
|
347
|
+
}))
|
|
406
348
|
};
|
|
407
|
-
|
|
349
|
+
let augmentedSchema;
|
|
408
350
|
// If the "anyOf" keyword already exists, wrap the augmentation in an "allOf"
|
|
409
351
|
if (option.anyOf) {
|
|
410
352
|
// Create a shallow clone of the option
|
|
411
|
-
|
|
353
|
+
const {
|
|
354
|
+
...shallowClone
|
|
355
|
+
} = option;
|
|
412
356
|
if (!shallowClone.allOf) {
|
|
413
357
|
shallowClone.allOf = [];
|
|
414
358
|
} else {
|
|
@@ -455,12 +399,12 @@ function getFirstMatchingOption(validator, formData, options, rootSchema, discri
|
|
|
455
399
|
* @returns - The `discriminator.propertyName` if it exists in the schema, otherwise `undefined`
|
|
456
400
|
*/
|
|
457
401
|
function getDiscriminatorFieldFromSchema(schema) {
|
|
458
|
-
|
|
459
|
-
|
|
402
|
+
let discriminator;
|
|
403
|
+
const maybeString = get__default["default"](schema, 'discriminator.propertyName', undefined);
|
|
460
404
|
if (isString__default["default"](maybeString)) {
|
|
461
405
|
discriminator = maybeString;
|
|
462
406
|
} else if (maybeString !== undefined) {
|
|
463
|
-
console.warn(
|
|
407
|
+
console.warn(`Expecting discriminator to be a string, got "${typeof maybeString}" instead`);
|
|
464
408
|
}
|
|
465
409
|
return discriminator;
|
|
466
410
|
}
|
|
@@ -506,20 +450,20 @@ function guessType(value) {
|
|
|
506
450
|
* @returns - The type of the schema
|
|
507
451
|
*/
|
|
508
452
|
function getSchemaType(schema) {
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
453
|
+
let {
|
|
454
|
+
type
|
|
455
|
+
} = schema;
|
|
456
|
+
if (!type && schema.const) {
|
|
457
|
+
return guessType(schema.const);
|
|
512
458
|
}
|
|
513
|
-
if (!type && schema
|
|
459
|
+
if (!type && schema.enum) {
|
|
514
460
|
return 'string';
|
|
515
461
|
}
|
|
516
462
|
if (!type && (schema.properties || schema.additionalProperties)) {
|
|
517
463
|
return 'object';
|
|
518
464
|
}
|
|
519
465
|
if (Array.isArray(type) && type.length === 2 && type.includes('null')) {
|
|
520
|
-
type = type.find(
|
|
521
|
-
return type !== 'null';
|
|
522
|
-
});
|
|
466
|
+
type = type.find(type => type !== 'null');
|
|
523
467
|
}
|
|
524
468
|
return type;
|
|
525
469
|
}
|
|
@@ -533,9 +477,9 @@ function getSchemaType(schema) {
|
|
|
533
477
|
* @returns - The merged schema object
|
|
534
478
|
*/
|
|
535
479
|
function mergeSchemas(obj1, obj2) {
|
|
536
|
-
|
|
537
|
-
return Object.keys(obj2).reduce(
|
|
538
|
-
|
|
480
|
+
const acc = Object.assign({}, obj1); // Prevent mutation of source object.
|
|
481
|
+
return Object.keys(obj2).reduce((acc, key) => {
|
|
482
|
+
const left = obj1 ? obj1[key] : {},
|
|
539
483
|
right = obj2[key];
|
|
540
484
|
if (obj1 && key in obj1 && isObject(right)) {
|
|
541
485
|
acc[key] = mergeSchemas(left, right);
|
|
@@ -549,12 +493,6 @@ function mergeSchemas(obj1, obj2) {
|
|
|
549
493
|
}, acc);
|
|
550
494
|
}
|
|
551
495
|
|
|
552
|
-
var _excluded$2 = ["if", "then", "else"],
|
|
553
|
-
_excluded2$1 = ["$ref"],
|
|
554
|
-
_excluded3 = ["allOf"],
|
|
555
|
-
_excluded4 = ["oneOf", "anyOf"],
|
|
556
|
-
_excluded5 = ["dependencies"],
|
|
557
|
-
_excluded6 = ["oneOf"];
|
|
558
496
|
/** Retrieves an expanded schema that has had all of its conditions, additional properties, references and dependencies
|
|
559
497
|
* resolved and merged into the `schema` given a `validator`, `rootSchema` and `rawFormData` that is used to do the
|
|
560
498
|
* potentially recursive resolution.
|
|
@@ -565,10 +503,7 @@ var _excluded$2 = ["if", "then", "else"],
|
|
|
565
503
|
* @param [rawFormData] - The current formData, if any, to assist retrieving a schema
|
|
566
504
|
* @returns - The schema having its conditions, additional properties, references and dependencies resolved
|
|
567
505
|
*/
|
|
568
|
-
function retrieveSchema(validator, schema, rootSchema, rawFormData) {
|
|
569
|
-
if (rootSchema === void 0) {
|
|
570
|
-
rootSchema = {};
|
|
571
|
-
}
|
|
506
|
+
function retrieveSchema(validator, schema, rootSchema = {}, rawFormData) {
|
|
572
507
|
return retrieveSchemaInternal(validator, schema, rootSchema, rawFormData)[0];
|
|
573
508
|
}
|
|
574
509
|
/** Resolves a conditional block (if/else/then) by removing the condition and merging the appropriate conditional branch
|
|
@@ -584,13 +519,15 @@ function retrieveSchema(validator, schema, rootSchema, rawFormData) {
|
|
|
584
519
|
* @returns - A list of schemas with the appropriate conditions resolved, possibly with all branches expanded
|
|
585
520
|
*/
|
|
586
521
|
function resolveCondition(validator, schema, rootSchema, expandAllBranches, formData) {
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
522
|
+
const {
|
|
523
|
+
if: expression,
|
|
524
|
+
then,
|
|
525
|
+
else: otherwise,
|
|
526
|
+
...resolvedSchemaLessConditional
|
|
527
|
+
} = schema;
|
|
528
|
+
const conditionValue = validator.isValid(expression, formData || {}, rootSchema);
|
|
529
|
+
let resolvedSchemas = [resolvedSchemaLessConditional];
|
|
530
|
+
let schemas = [];
|
|
594
531
|
if (expandAllBranches) {
|
|
595
532
|
if (then && typeof then !== 'boolean') {
|
|
596
533
|
schemas = schemas.concat(retrieveSchemaInternal(validator, then, rootSchema, formData, expandAllBranches));
|
|
@@ -599,19 +536,15 @@ function resolveCondition(validator, schema, rootSchema, expandAllBranches, form
|
|
|
599
536
|
schemas = schemas.concat(retrieveSchemaInternal(validator, otherwise, rootSchema, formData, expandAllBranches));
|
|
600
537
|
}
|
|
601
538
|
} else {
|
|
602
|
-
|
|
539
|
+
const conditionalSchema = conditionValue ? then : otherwise;
|
|
603
540
|
if (conditionalSchema && typeof conditionalSchema !== 'boolean') {
|
|
604
541
|
schemas = schemas.concat(retrieveSchemaInternal(validator, conditionalSchema, rootSchema, formData, expandAllBranches));
|
|
605
542
|
}
|
|
606
543
|
}
|
|
607
544
|
if (schemas.length) {
|
|
608
|
-
resolvedSchemas = schemas.map(
|
|
609
|
-
return mergeSchemas(resolvedSchemaLessConditional, s);
|
|
610
|
-
});
|
|
545
|
+
resolvedSchemas = schemas.map(s => mergeSchemas(resolvedSchemaLessConditional, s));
|
|
611
546
|
}
|
|
612
|
-
return resolvedSchemas.flatMap(
|
|
613
|
-
return retrieveSchemaInternal(validator, s, rootSchema, formData, expandAllBranches);
|
|
614
|
-
});
|
|
547
|
+
return resolvedSchemas.flatMap(s => retrieveSchemaInternal(validator, s, rootSchema, formData, expandAllBranches));
|
|
615
548
|
}
|
|
616
549
|
/** Given a list of lists of allOf, anyOf or oneOf values, create a list of lists of all permutations of the values. The
|
|
617
550
|
* `listOfLists` is expected to be all resolved values of the 1st...nth schemas within an `allOf`, `anyOf` or `oneOf`.
|
|
@@ -628,19 +561,13 @@ function resolveCondition(validator, schema, rootSchema, expandAllBranches, form
|
|
|
628
561
|
* @returns - The list of all permutations of schemas for a set of `xxxOf`s
|
|
629
562
|
*/
|
|
630
563
|
function getAllPermutationsOfXxxOf(listOfLists) {
|
|
631
|
-
|
|
564
|
+
const allPermutations = listOfLists.reduce((permutations, list) => {
|
|
632
565
|
// When there are more than one set of schemas for a row, duplicate the set of permutations and add in the values
|
|
633
566
|
if (list.length > 1) {
|
|
634
|
-
return list.flatMap(
|
|
635
|
-
return times__default["default"](permutations.length, function (i) {
|
|
636
|
-
return [].concat(permutations[i]).concat(element);
|
|
637
|
-
});
|
|
638
|
-
});
|
|
567
|
+
return list.flatMap(element => times__default["default"](permutations.length, i => [...permutations[i]].concat(element)));
|
|
639
568
|
}
|
|
640
569
|
// Otherwise just push in the single value into the current set of permutations
|
|
641
|
-
permutations.forEach(
|
|
642
|
-
return permutation.push(list[0]);
|
|
643
|
-
});
|
|
570
|
+
permutations.forEach(permutation => permutation.push(list[0]));
|
|
644
571
|
return permutations;
|
|
645
572
|
}, [[]] // Start with an empty list
|
|
646
573
|
);
|
|
@@ -664,21 +591,18 @@ function resolveSchema(validator, schema, rootSchema, expandAllBranches, formDat
|
|
|
664
591
|
return resolveReference(validator, schema, rootSchema, expandAllBranches, formData);
|
|
665
592
|
}
|
|
666
593
|
if (DEPENDENCIES_KEY in schema) {
|
|
667
|
-
|
|
668
|
-
return resolvedSchemas.flatMap(
|
|
594
|
+
const resolvedSchemas = resolveDependencies(validator, schema, rootSchema, expandAllBranches, formData);
|
|
595
|
+
return resolvedSchemas.flatMap(s => {
|
|
669
596
|
return retrieveSchemaInternal(validator, s, rootSchema, formData, expandAllBranches);
|
|
670
597
|
});
|
|
671
598
|
}
|
|
672
599
|
if (ALL_OF_KEY in schema && Array.isArray(schema.allOf)) {
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
allOf: permutation
|
|
680
|
-
});
|
|
681
|
-
});
|
|
600
|
+
const allOfSchemaElements = schema.allOf.map(allOfSubschema => retrieveSchemaInternal(validator, allOfSubschema, rootSchema, formData, expandAllBranches));
|
|
601
|
+
const allPermutations = getAllPermutationsOfXxxOf(allOfSchemaElements);
|
|
602
|
+
return allPermutations.map(permutation => ({
|
|
603
|
+
...schema,
|
|
604
|
+
allOf: permutation
|
|
605
|
+
}));
|
|
682
606
|
}
|
|
683
607
|
// No $ref or dependencies or allOf attribute was found, returning the original schema.
|
|
684
608
|
return [schema];
|
|
@@ -696,12 +620,55 @@ function resolveSchema(validator, schema, rootSchema, expandAllBranches, formDat
|
|
|
696
620
|
*/
|
|
697
621
|
function resolveReference(validator, schema, rootSchema, expandAllBranches, formData) {
|
|
698
622
|
// Drop the $ref property of the source schema.
|
|
699
|
-
|
|
700
|
-
|
|
623
|
+
const {
|
|
624
|
+
$ref,
|
|
625
|
+
...localSchema
|
|
626
|
+
} = schema;
|
|
701
627
|
// Retrieve the referenced schema definition.
|
|
702
|
-
|
|
628
|
+
const refSchema = findSchemaDefinition($ref, rootSchema);
|
|
703
629
|
// Update referenced schema definition with local schema properties.
|
|
704
|
-
return retrieveSchemaInternal(validator,
|
|
630
|
+
return retrieveSchemaInternal(validator, {
|
|
631
|
+
...refSchema,
|
|
632
|
+
...localSchema
|
|
633
|
+
}, rootSchema, formData, expandAllBranches);
|
|
634
|
+
}
|
|
635
|
+
/** Resolves all references within a schema's properties and array items.
|
|
636
|
+
*
|
|
637
|
+
* @param schema - The schema for which resolving all references is desired
|
|
638
|
+
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
639
|
+
* @returns - given schema will all references resolved
|
|
640
|
+
*/
|
|
641
|
+
function resolveAllReferences(schema, rootSchema) {
|
|
642
|
+
let resolvedSchema = schema;
|
|
643
|
+
// resolve top level ref
|
|
644
|
+
if (REF_KEY in resolvedSchema) {
|
|
645
|
+
const {
|
|
646
|
+
$ref,
|
|
647
|
+
...localSchema
|
|
648
|
+
} = resolvedSchema;
|
|
649
|
+
// Retrieve the referenced schema definition.
|
|
650
|
+
const refSchema = findSchemaDefinition($ref, rootSchema);
|
|
651
|
+
resolvedSchema = {
|
|
652
|
+
...refSchema,
|
|
653
|
+
...localSchema
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
if (PROPERTIES_KEY in resolvedSchema) {
|
|
657
|
+
const updatedProps = transform__default["default"](resolvedSchema[PROPERTIES_KEY], (result, value, key) => {
|
|
658
|
+
result[key] = resolveAllReferences(value, rootSchema);
|
|
659
|
+
}, {});
|
|
660
|
+
resolvedSchema = {
|
|
661
|
+
...resolvedSchema,
|
|
662
|
+
[PROPERTIES_KEY]: updatedProps
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
if (ITEMS_KEY in resolvedSchema && !Array.isArray(resolvedSchema.items) && typeof resolvedSchema.items !== 'boolean') {
|
|
666
|
+
resolvedSchema = {
|
|
667
|
+
...resolvedSchema,
|
|
668
|
+
items: resolveAllReferences(resolvedSchema.items, rootSchema)
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
return resolvedSchema;
|
|
705
672
|
}
|
|
706
673
|
/** Creates new 'properties' items for each key in the `formData`
|
|
707
674
|
*
|
|
@@ -713,28 +680,34 @@ function resolveReference(validator, schema, rootSchema, expandAllBranches, form
|
|
|
713
680
|
*/
|
|
714
681
|
function stubExistingAdditionalProperties(validator, theSchema, rootSchema, aFormData) {
|
|
715
682
|
// Clone the schema so that we don't ruin the consumer's original
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
683
|
+
const schema = {
|
|
684
|
+
...theSchema,
|
|
685
|
+
properties: {
|
|
686
|
+
...theSchema.properties
|
|
687
|
+
}
|
|
688
|
+
};
|
|
719
689
|
// make sure formData is an object
|
|
720
|
-
|
|
721
|
-
Object.keys(formData).forEach(
|
|
690
|
+
const formData = aFormData && isObject(aFormData) ? aFormData : {};
|
|
691
|
+
Object.keys(formData).forEach(key => {
|
|
722
692
|
if (key in schema.properties) {
|
|
723
693
|
// No need to stub, our schema already has the property
|
|
724
694
|
return;
|
|
725
695
|
}
|
|
726
|
-
|
|
696
|
+
let additionalProperties = {};
|
|
727
697
|
if (typeof schema.additionalProperties !== 'boolean') {
|
|
728
698
|
if (REF_KEY in schema.additionalProperties) {
|
|
729
699
|
additionalProperties = retrieveSchema(validator, {
|
|
730
700
|
$ref: get__default["default"](schema.additionalProperties, [REF_KEY])
|
|
731
701
|
}, rootSchema, formData);
|
|
732
702
|
} else if ('type' in schema.additionalProperties) {
|
|
733
|
-
additionalProperties =
|
|
703
|
+
additionalProperties = {
|
|
704
|
+
...schema.additionalProperties
|
|
705
|
+
};
|
|
734
706
|
} else if (ANY_OF_KEY in schema.additionalProperties || ONE_OF_KEY in schema.additionalProperties) {
|
|
735
|
-
additionalProperties =
|
|
736
|
-
type: 'object'
|
|
737
|
-
|
|
707
|
+
additionalProperties = {
|
|
708
|
+
type: 'object',
|
|
709
|
+
...schema.additionalProperties
|
|
710
|
+
};
|
|
738
711
|
} else {
|
|
739
712
|
additionalProperties = {
|
|
740
713
|
type: guessType(get__default["default"](formData, [key]))
|
|
@@ -766,36 +739,35 @@ function stubExistingAdditionalProperties(validator, theSchema, rootSchema, aFor
|
|
|
766
739
|
* @returns - The schema(s) resulting from having its conditions, additional properties, references and dependencies
|
|
767
740
|
* resolved. Multiple schemas may be returned if `expandAllBranches` is true.
|
|
768
741
|
*/
|
|
769
|
-
function retrieveSchemaInternal(validator, schema, rootSchema, rawFormData, expandAllBranches) {
|
|
770
|
-
if (expandAllBranches === void 0) {
|
|
771
|
-
expandAllBranches = false;
|
|
772
|
-
}
|
|
742
|
+
function retrieveSchemaInternal(validator, schema, rootSchema, rawFormData, expandAllBranches = false) {
|
|
773
743
|
if (!isObject(schema)) {
|
|
774
744
|
return [{}];
|
|
775
745
|
}
|
|
776
|
-
|
|
777
|
-
return resolvedSchemas.flatMap(
|
|
778
|
-
|
|
746
|
+
const resolvedSchemas = resolveSchema(validator, schema, rootSchema, expandAllBranches, rawFormData);
|
|
747
|
+
return resolvedSchemas.flatMap(s => {
|
|
748
|
+
let resolvedSchema = s;
|
|
779
749
|
if (IF_KEY in resolvedSchema) {
|
|
780
750
|
return resolveCondition(validator, resolvedSchema, rootSchema, expandAllBranches, rawFormData);
|
|
781
751
|
}
|
|
782
752
|
if (ALL_OF_KEY in resolvedSchema) {
|
|
753
|
+
// resolve allOf schemas
|
|
754
|
+
if (expandAllBranches) {
|
|
755
|
+
return [...resolvedSchema.allOf];
|
|
756
|
+
}
|
|
783
757
|
try {
|
|
784
758
|
resolvedSchema = mergeAllOf__default["default"](resolvedSchema, {
|
|
785
759
|
deep: false
|
|
786
760
|
});
|
|
787
761
|
} catch (e) {
|
|
788
762
|
console.warn('could not merge subschemas in allOf:\n', e);
|
|
789
|
-
|
|
790
|
-
allOf
|
|
791
|
-
resolvedSchemaWithoutAllOf
|
|
792
|
-
|
|
793
|
-
return [resolvedSchemaWithoutAllOf].concat(allOf);
|
|
794
|
-
}
|
|
763
|
+
const {
|
|
764
|
+
allOf,
|
|
765
|
+
...resolvedSchemaWithoutAllOf
|
|
766
|
+
} = resolvedSchema;
|
|
795
767
|
return resolvedSchemaWithoutAllOf;
|
|
796
768
|
}
|
|
797
769
|
}
|
|
798
|
-
|
|
770
|
+
const hasAdditionalProperties = ADDITIONAL_PROPERTIES_KEY in resolvedSchema && resolvedSchema.additionalProperties !== false;
|
|
799
771
|
if (hasAdditionalProperties) {
|
|
800
772
|
return stubExistingAdditionalProperties(validator, resolvedSchema, rootSchema, rawFormData);
|
|
801
773
|
}
|
|
@@ -815,11 +787,12 @@ function retrieveSchemaInternal(validator, schema, rootSchema, rawFormData, expa
|
|
|
815
787
|
* @returns - Either an array containing the best matching option or all options if `expandAllBranches` is true
|
|
816
788
|
*/
|
|
817
789
|
function resolveAnyOrOneOfSchemas(validator, schema, rootSchema, expandAllBranches, rawFormData) {
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
oneOf
|
|
821
|
-
anyOf
|
|
822
|
-
remaining
|
|
790
|
+
let anyOrOneOf;
|
|
791
|
+
const {
|
|
792
|
+
oneOf,
|
|
793
|
+
anyOf,
|
|
794
|
+
...remaining
|
|
795
|
+
} = schema;
|
|
823
796
|
if (Array.isArray(oneOf)) {
|
|
824
797
|
anyOrOneOf = oneOf;
|
|
825
798
|
} else if (Array.isArray(anyOf)) {
|
|
@@ -827,23 +800,17 @@ function resolveAnyOrOneOfSchemas(validator, schema, rootSchema, expandAllBranch
|
|
|
827
800
|
}
|
|
828
801
|
if (anyOrOneOf) {
|
|
829
802
|
// Ensure that during expand all branches we pass an object rather than undefined so that all options are interrogated
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
anyOrOneOf = anyOrOneOf.map(
|
|
833
|
-
|
|
834
|
-
// For this ref situation, don't expand all branches and just pick the first/only schema result
|
|
835
|
-
return resolveReference(validator, s, rootSchema, false, formData)[0];
|
|
836
|
-
}
|
|
837
|
-
return s;
|
|
803
|
+
const formData = rawFormData === undefined && expandAllBranches ? {} : rawFormData;
|
|
804
|
+
const discriminator = getDiscriminatorFieldFromSchema(schema);
|
|
805
|
+
anyOrOneOf = anyOrOneOf.map(s => {
|
|
806
|
+
return resolveAllReferences(s, rootSchema);
|
|
838
807
|
});
|
|
839
808
|
// Call this to trigger the set of isValid() calls that the schema parser will need
|
|
840
|
-
|
|
809
|
+
const option = getFirstMatchingOption(validator, formData, anyOrOneOf, rootSchema, discriminator);
|
|
841
810
|
if (expandAllBranches) {
|
|
842
|
-
return anyOrOneOf.map(
|
|
843
|
-
return _extends({}, remaining, item);
|
|
844
|
-
});
|
|
811
|
+
return anyOrOneOf.map(item => mergeSchemas(remaining, item));
|
|
845
812
|
}
|
|
846
|
-
schema =
|
|
813
|
+
schema = mergeSchemas(remaining, anyOrOneOf[option]);
|
|
847
814
|
}
|
|
848
815
|
return [schema];
|
|
849
816
|
}
|
|
@@ -860,12 +827,12 @@ function resolveAnyOrOneOfSchemas(validator, schema, rootSchema, expandAllBranch
|
|
|
860
827
|
*/
|
|
861
828
|
function resolveDependencies(validator, schema, rootSchema, expandAllBranches, formData) {
|
|
862
829
|
// Drop the dependencies from the source schema.
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
830
|
+
const {
|
|
831
|
+
dependencies,
|
|
832
|
+
...remainingSchema
|
|
833
|
+
} = schema;
|
|
834
|
+
const resolvedSchemas = resolveAnyOrOneOfSchemas(validator, remainingSchema, rootSchema, expandAllBranches, formData);
|
|
835
|
+
return resolvedSchemas.flatMap(resolvedSchema => processDependencies(validator, dependencies, resolvedSchema, rootSchema, expandAllBranches, formData));
|
|
869
836
|
}
|
|
870
837
|
/** Processes all the `dependencies` recursively into the list of `resolvedSchema`s as needed. Passes the
|
|
871
838
|
* `expandAllBranches` flag down to the `withDependentSchema()` and the recursive `processDependencies()` helper calls.
|
|
@@ -880,35 +847,24 @@ function resolveDependencies(validator, schema, rootSchema, expandAllBranches, f
|
|
|
880
847
|
* @returns - The schema with the `dependencies` resolved into it
|
|
881
848
|
*/
|
|
882
849
|
function processDependencies(validator, dependencies, resolvedSchema, rootSchema, expandAllBranches, formData) {
|
|
883
|
-
|
|
850
|
+
let schemas = [resolvedSchema];
|
|
884
851
|
// Process dependencies updating the local schema properties as appropriate.
|
|
885
|
-
|
|
852
|
+
for (const dependencyKey in dependencies) {
|
|
886
853
|
// Skip this dependency if its trigger property is not present.
|
|
887
854
|
if (!expandAllBranches && get__default["default"](formData, [dependencyKey]) === undefined) {
|
|
888
|
-
|
|
855
|
+
continue;
|
|
889
856
|
}
|
|
890
857
|
// Skip this dependency if it is not included in the schema (such as when dependencyKey is itself a hidden dependency.)
|
|
891
858
|
if (resolvedSchema.properties && !(dependencyKey in resolvedSchema.properties)) {
|
|
892
|
-
|
|
859
|
+
continue;
|
|
893
860
|
}
|
|
894
|
-
|
|
895
|
-
remainingDependencies = _splitKeyElementFromO[0],
|
|
896
|
-
dependencyValue = _splitKeyElementFromO[1];
|
|
861
|
+
const [remainingDependencies, dependencyValue] = splitKeyElementFromObject(dependencyKey, dependencies);
|
|
897
862
|
if (Array.isArray(dependencyValue)) {
|
|
898
863
|
schemas[0] = withDependentProperties(resolvedSchema, dependencyValue);
|
|
899
864
|
} else if (isObject(dependencyValue)) {
|
|
900
865
|
schemas = withDependentSchema(validator, resolvedSchema, rootSchema, dependencyKey, dependencyValue, expandAllBranches, formData);
|
|
901
866
|
}
|
|
902
|
-
return
|
|
903
|
-
v: schemas.flatMap(function (schema) {
|
|
904
|
-
return processDependencies(validator, remainingDependencies, schema, rootSchema, expandAllBranches, formData);
|
|
905
|
-
})
|
|
906
|
-
};
|
|
907
|
-
};
|
|
908
|
-
for (var dependencyKey in dependencies) {
|
|
909
|
-
var _ret = _loop();
|
|
910
|
-
if (_ret === "continue") continue;
|
|
911
|
-
if (typeof _ret === "object") return _ret.v;
|
|
867
|
+
return schemas.flatMap(schema => processDependencies(validator, remainingDependencies, schema, rootSchema, expandAllBranches, formData));
|
|
912
868
|
}
|
|
913
869
|
return schemas;
|
|
914
870
|
}
|
|
@@ -922,10 +878,11 @@ function withDependentProperties(schema, additionallyRequired) {
|
|
|
922
878
|
if (!additionallyRequired) {
|
|
923
879
|
return schema;
|
|
924
880
|
}
|
|
925
|
-
|
|
926
|
-
return
|
|
881
|
+
const required = Array.isArray(schema.required) ? Array.from(new Set([...schema.required, ...additionallyRequired])) : additionallyRequired;
|
|
882
|
+
return {
|
|
883
|
+
...schema,
|
|
927
884
|
required: required
|
|
928
|
-
}
|
|
885
|
+
};
|
|
929
886
|
}
|
|
930
887
|
/** Merges a dependent schema into the `schema` dealing with oneOfs and references. Passes the `expandAllBranches` flag
|
|
931
888
|
* down to the `retrieveSchemaInternal()`, `resolveReference()` and `withExactlyOneSubschema()` helper calls.
|
|
@@ -941,26 +898,26 @@ function withDependentProperties(schema, additionallyRequired) {
|
|
|
941
898
|
* @returns - The list of schemas with the dependent schema resolved into them
|
|
942
899
|
*/
|
|
943
900
|
function withDependentSchema(validator, schema, rootSchema, dependencyKey, dependencyValue, expandAllBranches, formData) {
|
|
944
|
-
|
|
945
|
-
return dependentSchemas.flatMap(
|
|
946
|
-
|
|
947
|
-
|
|
901
|
+
const dependentSchemas = retrieveSchemaInternal(validator, dependencyValue, rootSchema, formData, expandAllBranches);
|
|
902
|
+
return dependentSchemas.flatMap(dependent => {
|
|
903
|
+
const {
|
|
904
|
+
oneOf,
|
|
905
|
+
...dependentSchema
|
|
906
|
+
} = dependent;
|
|
948
907
|
schema = mergeSchemas(schema, dependentSchema);
|
|
949
908
|
// Since it does not contain oneOf, we return the original schema.
|
|
950
909
|
if (oneOf === undefined) {
|
|
951
910
|
return schema;
|
|
952
911
|
}
|
|
953
912
|
// Resolve $refs inside oneOf.
|
|
954
|
-
|
|
913
|
+
const resolvedOneOfs = oneOf.map(subschema => {
|
|
955
914
|
if (typeof subschema === 'boolean' || !(REF_KEY in subschema)) {
|
|
956
915
|
return [subschema];
|
|
957
916
|
}
|
|
958
917
|
return resolveReference(validator, subschema, rootSchema, expandAllBranches, formData);
|
|
959
918
|
});
|
|
960
|
-
|
|
961
|
-
return allPermutations.flatMap(
|
|
962
|
-
return withExactlyOneSubschema(validator, schema, rootSchema, dependencyKey, resolvedOneOf, expandAllBranches, formData);
|
|
963
|
-
});
|
|
919
|
+
const allPermutations = getAllPermutationsOfXxxOf(resolvedOneOfs);
|
|
920
|
+
return allPermutations.flatMap(resolvedOneOf => withExactlyOneSubschema(validator, schema, rootSchema, dependencyKey, resolvedOneOf, expandAllBranches, formData));
|
|
964
921
|
});
|
|
965
922
|
}
|
|
966
923
|
/** Returns a list of `schema`s with the best choice from the `oneOf` options merged into it. If `expandAllBranches` is
|
|
@@ -978,16 +935,19 @@ function withDependentSchema(validator, schema, rootSchema, dependencyKey, depen
|
|
|
978
935
|
* @returns - Either an array containing the best matching option or all options if `expandAllBranches` is true
|
|
979
936
|
*/
|
|
980
937
|
function withExactlyOneSubschema(validator, schema, rootSchema, dependencyKey, oneOf, expandAllBranches, formData) {
|
|
981
|
-
|
|
938
|
+
const validSubschemas = oneOf.filter(subschema => {
|
|
982
939
|
if (typeof subschema === 'boolean' || !subschema || !subschema.properties) {
|
|
983
940
|
return false;
|
|
984
941
|
}
|
|
985
|
-
|
|
942
|
+
const {
|
|
943
|
+
[dependencyKey]: conditionPropertySchema
|
|
944
|
+
} = subschema.properties;
|
|
986
945
|
if (conditionPropertySchema) {
|
|
987
|
-
|
|
988
|
-
var conditionSchema = {
|
|
946
|
+
const conditionSchema = {
|
|
989
947
|
type: 'object',
|
|
990
|
-
properties:
|
|
948
|
+
properties: {
|
|
949
|
+
[dependencyKey]: conditionPropertySchema
|
|
950
|
+
}
|
|
991
951
|
};
|
|
992
952
|
return validator.isValid(conditionSchema, formData, rootSchema) || expandAllBranches;
|
|
993
953
|
}
|
|
@@ -997,24 +957,22 @@ function withExactlyOneSubschema(validator, schema, rootSchema, dependencyKey, o
|
|
|
997
957
|
console.warn("ignoring oneOf in dependencies because there isn't exactly one subschema that is valid");
|
|
998
958
|
return [schema];
|
|
999
959
|
}
|
|
1000
|
-
return validSubschemas.flatMap(
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
960
|
+
return validSubschemas.flatMap(s => {
|
|
961
|
+
const subschema = s;
|
|
962
|
+
const [dependentSubschema] = splitKeyElementFromObject(dependencyKey, subschema.properties);
|
|
963
|
+
const dependentSchema = {
|
|
964
|
+
...subschema,
|
|
1005
965
|
properties: dependentSubschema
|
|
1006
|
-
}
|
|
1007
|
-
|
|
1008
|
-
return schemas.map(
|
|
1009
|
-
return mergeSchemas(schema, s);
|
|
1010
|
-
});
|
|
966
|
+
};
|
|
967
|
+
const schemas = retrieveSchemaInternal(validator, dependentSchema, rootSchema, formData, expandAllBranches);
|
|
968
|
+
return schemas.map(s => mergeSchemas(schema, s));
|
|
1011
969
|
});
|
|
1012
970
|
}
|
|
1013
971
|
|
|
1014
972
|
/** A junk option used to determine when the getFirstMatchingOption call really matches an option rather than returning
|
|
1015
973
|
* the first item
|
|
1016
974
|
*/
|
|
1017
|
-
|
|
975
|
+
const JUNK_OPTION = {
|
|
1018
976
|
type: 'object',
|
|
1019
977
|
$id: JUNK_OPTION_ID,
|
|
1020
978
|
properties: {
|
|
@@ -1043,41 +1001,38 @@ var JUNK_OPTION = {
|
|
|
1043
1001
|
* @param formData - The form data associated with the schema, used to calculate the score
|
|
1044
1002
|
* @returns - The score a schema against the formData
|
|
1045
1003
|
*/
|
|
1046
|
-
function calculateIndexScore(validator, rootSchema, schema, formData) {
|
|
1047
|
-
|
|
1048
|
-
formData = {};
|
|
1049
|
-
}
|
|
1050
|
-
var totalScore = 0;
|
|
1004
|
+
function calculateIndexScore(validator, rootSchema, schema, formData = {}) {
|
|
1005
|
+
let totalScore = 0;
|
|
1051
1006
|
if (schema) {
|
|
1052
1007
|
if (isObject__default["default"](schema.properties)) {
|
|
1053
|
-
totalScore += reduce__default["default"](schema.properties,
|
|
1054
|
-
|
|
1008
|
+
totalScore += reduce__default["default"](schema.properties, (score, value, key) => {
|
|
1009
|
+
const formValue = get__default["default"](formData, key);
|
|
1055
1010
|
if (typeof value === 'boolean') {
|
|
1056
1011
|
return score;
|
|
1057
1012
|
}
|
|
1058
1013
|
if (has__default["default"](value, REF_KEY)) {
|
|
1059
|
-
|
|
1014
|
+
const newSchema = retrieveSchema(validator, value, rootSchema, formValue);
|
|
1060
1015
|
return score + calculateIndexScore(validator, rootSchema, newSchema, formValue || {});
|
|
1061
1016
|
}
|
|
1062
1017
|
if ((has__default["default"](value, ONE_OF_KEY) || has__default["default"](value, ANY_OF_KEY)) && formValue) {
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
return score + getClosestMatchingOption(validator, rootSchema, formValue, get__default["default"](value,
|
|
1018
|
+
const key = has__default["default"](value, ONE_OF_KEY) ? ONE_OF_KEY : ANY_OF_KEY;
|
|
1019
|
+
const discriminator = getDiscriminatorFieldFromSchema(value);
|
|
1020
|
+
return score + getClosestMatchingOption(validator, rootSchema, formValue, get__default["default"](value, key), -1, discriminator);
|
|
1066
1021
|
}
|
|
1067
1022
|
if (value.type === 'object') {
|
|
1068
1023
|
return score + calculateIndexScore(validator, rootSchema, value, formValue || {});
|
|
1069
1024
|
}
|
|
1070
1025
|
if (value.type === guessType(formValue)) {
|
|
1071
1026
|
// If the types match, then we bump the score by one
|
|
1072
|
-
|
|
1073
|
-
if (value
|
|
1027
|
+
let newScore = score + 1;
|
|
1028
|
+
if (value.default) {
|
|
1074
1029
|
// If the schema contains a readonly default value score the value that matches the default higher and
|
|
1075
1030
|
// any non-matching value lower
|
|
1076
|
-
newScore += formValue === value
|
|
1077
|
-
} else if (value
|
|
1031
|
+
newScore += formValue === value.default ? 1 : -1;
|
|
1032
|
+
} else if (value.const) {
|
|
1078
1033
|
// If the schema contains a const value score the value that matches the default higher and
|
|
1079
1034
|
// any non-matching value lower
|
|
1080
|
-
newScore += formValue === value
|
|
1035
|
+
newScore += formValue === value.const ? 1 : -1;
|
|
1081
1036
|
}
|
|
1082
1037
|
// TODO eventually, deal with enums/arrays
|
|
1083
1038
|
return newScore;
|
|
@@ -1112,21 +1067,15 @@ function calculateIndexScore(validator, rootSchema, schema, formData) {
|
|
|
1112
1067
|
* determine which option is selected
|
|
1113
1068
|
* @returns - The index of the option that is the closest match to the `formData` or the `selectedOption` if no match
|
|
1114
1069
|
*/
|
|
1115
|
-
function getClosestMatchingOption(validator, rootSchema, formData, options, selectedOption, discriminatorField) {
|
|
1116
|
-
if (selectedOption === void 0) {
|
|
1117
|
-
selectedOption = -1;
|
|
1118
|
-
}
|
|
1070
|
+
function getClosestMatchingOption(validator, rootSchema, formData, options, selectedOption = -1, discriminatorField) {
|
|
1119
1071
|
// First resolve any refs in the options
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
return retrieveSchema(validator, option, rootSchema, formData);
|
|
1123
|
-
}
|
|
1124
|
-
return option;
|
|
1072
|
+
const resolvedOptions = options.map(option => {
|
|
1073
|
+
return resolveAllReferences(option, rootSchema);
|
|
1125
1074
|
});
|
|
1126
1075
|
// Reduce the array of options down to a list of the indexes that are considered matching options
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1076
|
+
const allValidIndexes = resolvedOptions.reduce((validList, option, index) => {
|
|
1077
|
+
const testOptions = [JUNK_OPTION, option];
|
|
1078
|
+
const match = getFirstMatchingOption(validator, formData, testOptions, rootSchema, discriminatorField);
|
|
1130
1079
|
// The match is the real option, so add its index to list of valid indexes
|
|
1131
1080
|
if (match === 1) {
|
|
1132
1081
|
validList.push(index);
|
|
@@ -1139,29 +1088,30 @@ function getClosestMatchingOption(validator, rootSchema, formData, options, sele
|
|
|
1139
1088
|
}
|
|
1140
1089
|
if (!allValidIndexes.length) {
|
|
1141
1090
|
// No indexes were valid, so we'll score all the options, add all the indexes
|
|
1142
|
-
times__default["default"](resolvedOptions.length,
|
|
1143
|
-
return allValidIndexes.push(i);
|
|
1144
|
-
});
|
|
1091
|
+
times__default["default"](resolvedOptions.length, i => allValidIndexes.push(i));
|
|
1145
1092
|
}
|
|
1146
|
-
|
|
1093
|
+
const scoreCount = new Set();
|
|
1147
1094
|
// Score all the options in the list of valid indexes and return the index with the best score
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1095
|
+
const {
|
|
1096
|
+
bestIndex
|
|
1097
|
+
} = allValidIndexes.reduce((scoreData, index) => {
|
|
1098
|
+
const {
|
|
1099
|
+
bestScore
|
|
1100
|
+
} = scoreData;
|
|
1101
|
+
const option = resolvedOptions[index];
|
|
1102
|
+
const score = calculateIndexScore(validator, rootSchema, option, formData);
|
|
1103
|
+
scoreCount.add(score);
|
|
1104
|
+
if (score > bestScore) {
|
|
1105
|
+
return {
|
|
1106
|
+
bestIndex: index,
|
|
1107
|
+
bestScore: score
|
|
1108
|
+
};
|
|
1109
|
+
}
|
|
1110
|
+
return scoreData;
|
|
1111
|
+
}, {
|
|
1112
|
+
bestIndex: selectedOption,
|
|
1113
|
+
bestScore: 0
|
|
1114
|
+
});
|
|
1165
1115
|
// if all scores are the same go with selectedOption
|
|
1166
1116
|
if (scoreCount.size === 1 && selectedOption >= 0) {
|
|
1167
1117
|
return selectedOption;
|
|
@@ -1176,9 +1126,7 @@ function getClosestMatchingOption(validator, rootSchema, formData, options, sele
|
|
|
1176
1126
|
* @returns - True if there are fixed items in the schema, false otherwise
|
|
1177
1127
|
*/
|
|
1178
1128
|
function isFixedItems(schema) {
|
|
1179
|
-
return Array.isArray(schema.items) && schema.items.length > 0 && schema.items.every(
|
|
1180
|
-
return isObject(item);
|
|
1181
|
-
});
|
|
1129
|
+
return Array.isArray(schema.items) && schema.items.length > 0 && schema.items.every(item => isObject(item));
|
|
1182
1130
|
}
|
|
1183
1131
|
|
|
1184
1132
|
/** Merges the `defaults` object of type `T` into the `formData` of type `T`
|
|
@@ -1197,13 +1145,10 @@ function isFixedItems(schema) {
|
|
|
1197
1145
|
* @param [mergeExtraArrayDefaults=false] - If true, any additional default array entries are appended onto the formData
|
|
1198
1146
|
* @returns - The resulting merged form data with defaults
|
|
1199
1147
|
*/
|
|
1200
|
-
function mergeDefaultsWithFormData(defaults, formData, mergeExtraArrayDefaults) {
|
|
1201
|
-
if (mergeExtraArrayDefaults === void 0) {
|
|
1202
|
-
mergeExtraArrayDefaults = false;
|
|
1203
|
-
}
|
|
1148
|
+
function mergeDefaultsWithFormData(defaults, formData, mergeExtraArrayDefaults = false) {
|
|
1204
1149
|
if (Array.isArray(formData)) {
|
|
1205
|
-
|
|
1206
|
-
|
|
1150
|
+
const defaultsArray = Array.isArray(defaults) ? defaults : [];
|
|
1151
|
+
const mapped = formData.map((value, idx) => {
|
|
1207
1152
|
if (defaultsArray[idx]) {
|
|
1208
1153
|
return mergeDefaultsWithFormData(defaultsArray[idx], value, mergeExtraArrayDefaults);
|
|
1209
1154
|
}
|
|
@@ -1211,13 +1156,13 @@ function mergeDefaultsWithFormData(defaults, formData, mergeExtraArrayDefaults)
|
|
|
1211
1156
|
});
|
|
1212
1157
|
// Merge any extra defaults when mergeExtraArrayDefaults is true
|
|
1213
1158
|
if (mergeExtraArrayDefaults && mapped.length < defaultsArray.length) {
|
|
1214
|
-
mapped.push
|
|
1159
|
+
mapped.push(...defaultsArray.slice(mapped.length));
|
|
1215
1160
|
}
|
|
1216
1161
|
return mapped;
|
|
1217
1162
|
}
|
|
1218
1163
|
if (isObject(formData)) {
|
|
1219
|
-
|
|
1220
|
-
return Object.keys(formData).reduce(
|
|
1164
|
+
const acc = Object.assign({}, defaults); // Prevent mutation of source object.
|
|
1165
|
+
return Object.keys(formData).reduce((acc, key) => {
|
|
1221
1166
|
acc[key] = mergeDefaultsWithFormData(defaults ? get__default["default"](defaults, key) : {}, get__default["default"](formData, key), mergeExtraArrayDefaults);
|
|
1222
1167
|
return acc;
|
|
1223
1168
|
}, acc);
|
|
@@ -1234,19 +1179,16 @@ function mergeDefaultsWithFormData(defaults, formData, mergeExtraArrayDefaults)
|
|
|
1234
1179
|
* NOTE: Uses shallow comparison for the duplicate checking.
|
|
1235
1180
|
* @returns - A new object that is the merge of the two given objects
|
|
1236
1181
|
*/
|
|
1237
|
-
function mergeObjects(obj1, obj2, concatArrays) {
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
}
|
|
1241
|
-
return Object.keys(obj2).reduce(function (acc, key) {
|
|
1242
|
-
var left = obj1 ? obj1[key] : {},
|
|
1182
|
+
function mergeObjects(obj1, obj2, concatArrays = false) {
|
|
1183
|
+
return Object.keys(obj2).reduce((acc, key) => {
|
|
1184
|
+
const left = obj1 ? obj1[key] : {},
|
|
1243
1185
|
right = obj2[key];
|
|
1244
1186
|
if (obj1 && key in obj1 && isObject(right)) {
|
|
1245
1187
|
acc[key] = mergeObjects(left, right, concatArrays);
|
|
1246
1188
|
} else if (concatArrays && Array.isArray(left) && Array.isArray(right)) {
|
|
1247
|
-
|
|
1189
|
+
let toMerge = right;
|
|
1248
1190
|
if (concatArrays === 'preventDuplicates') {
|
|
1249
|
-
toMerge = right.reduce(
|
|
1191
|
+
toMerge = right.reduce((result, value) => {
|
|
1250
1192
|
if (!left.includes(value)) {
|
|
1251
1193
|
result.push(value);
|
|
1252
1194
|
}
|
|
@@ -1268,7 +1210,7 @@ function mergeObjects(obj1, obj2, concatArrays) {
|
|
|
1268
1210
|
* @returns - True if the `schema` has a single constant value, false otherwise
|
|
1269
1211
|
*/
|
|
1270
1212
|
function isConstant(schema) {
|
|
1271
|
-
return Array.isArray(schema
|
|
1213
|
+
return Array.isArray(schema.enum) && schema.enum.length === 1 || CONST_KEY in schema;
|
|
1272
1214
|
}
|
|
1273
1215
|
|
|
1274
1216
|
/** Checks to see if the `schema` combination represents a select
|
|
@@ -1278,19 +1220,14 @@ function isConstant(schema) {
|
|
|
1278
1220
|
* @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
|
|
1279
1221
|
* @returns - True if schema contains a select, otherwise false
|
|
1280
1222
|
*/
|
|
1281
|
-
function isSelect(validator, theSchema, rootSchema) {
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
var schema = retrieveSchema(validator, theSchema, rootSchema, undefined);
|
|
1286
|
-
var altSchemas = schema.oneOf || schema.anyOf;
|
|
1287
|
-
if (Array.isArray(schema["enum"])) {
|
|
1223
|
+
function isSelect(validator, theSchema, rootSchema = {}) {
|
|
1224
|
+
const schema = retrieveSchema(validator, theSchema, rootSchema, undefined);
|
|
1225
|
+
const altSchemas = schema.oneOf || schema.anyOf;
|
|
1226
|
+
if (Array.isArray(schema.enum)) {
|
|
1288
1227
|
return true;
|
|
1289
1228
|
}
|
|
1290
1229
|
if (Array.isArray(altSchemas)) {
|
|
1291
|
-
return altSchemas.every(
|
|
1292
|
-
return typeof altSchemas !== 'boolean' && isConstant(altSchemas);
|
|
1293
|
-
});
|
|
1230
|
+
return altSchemas.every(altSchemas => typeof altSchemas !== 'boolean' && isConstant(altSchemas));
|
|
1294
1231
|
}
|
|
1295
1232
|
return false;
|
|
1296
1233
|
}
|
|
@@ -1309,8 +1246,6 @@ function isMultiSelect(validator, schema, rootSchema) {
|
|
|
1309
1246
|
return isSelect(validator, schema.items, rootSchema);
|
|
1310
1247
|
}
|
|
1311
1248
|
|
|
1312
|
-
var _excluded$1 = ["oneOf"],
|
|
1313
|
-
_excluded2 = ["anyOf"];
|
|
1314
1249
|
/** Enum that indicates how `schema.additionalItems` should be handled by the `getInnerSchemaForArrayItem()` function.
|
|
1315
1250
|
*/
|
|
1316
1251
|
var AdditionalItemsHandling;
|
|
@@ -1334,16 +1269,10 @@ var AdditionalItemsHandling;
|
|
|
1334
1269
|
* @param [idx=-1] - Index, if non-negative, will be used to return the idx-th element in a `schema.items` array
|
|
1335
1270
|
* @returns - The best fit schema object from the `schema` given the `additionalItems` and `idx` modifiers
|
|
1336
1271
|
*/
|
|
1337
|
-
function getInnerSchemaForArrayItem(schema, additionalItems, idx) {
|
|
1338
|
-
if (additionalItems === void 0) {
|
|
1339
|
-
additionalItems = AdditionalItemsHandling.Ignore;
|
|
1340
|
-
}
|
|
1341
|
-
if (idx === void 0) {
|
|
1342
|
-
idx = -1;
|
|
1343
|
-
}
|
|
1272
|
+
function getInnerSchemaForArrayItem(schema, additionalItems = AdditionalItemsHandling.Ignore, idx = -1) {
|
|
1344
1273
|
if (idx >= 0) {
|
|
1345
1274
|
if (Array.isArray(schema.items) && idx < schema.items.length) {
|
|
1346
|
-
|
|
1275
|
+
const item = schema.items[idx];
|
|
1347
1276
|
if (typeof item !== 'boolean') {
|
|
1348
1277
|
return item;
|
|
1349
1278
|
}
|
|
@@ -1375,23 +1304,17 @@ function getInnerSchemaForArrayItem(schema, additionalItems, idx) {
|
|
|
1375
1304
|
* @param experimental_defaultFormStateBehavior - Optional configuration object, if provided, allows users to override
|
|
1376
1305
|
* default form state behavior
|
|
1377
1306
|
*/
|
|
1378
|
-
function maybeAddDefaultToObject(obj, key, computedDefault, includeUndefinedValues, isParentRequired, requiredFields, experimental_defaultFormStateBehavior) {
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
}
|
|
1382
|
-
if (experimental_defaultFormStateBehavior === void 0) {
|
|
1383
|
-
experimental_defaultFormStateBehavior = {};
|
|
1384
|
-
}
|
|
1385
|
-
var _experimental_default = experimental_defaultFormStateBehavior,
|
|
1386
|
-
_experimental_default2 = _experimental_default.emptyObjectFields,
|
|
1387
|
-
emptyObjectFields = _experimental_default2 === void 0 ? 'populateAllDefaults' : _experimental_default2;
|
|
1307
|
+
function maybeAddDefaultToObject(obj, key, computedDefault, includeUndefinedValues, isParentRequired, requiredFields = [], experimental_defaultFormStateBehavior = {}) {
|
|
1308
|
+
const {
|
|
1309
|
+
emptyObjectFields = 'populateAllDefaults'
|
|
1310
|
+
} = experimental_defaultFormStateBehavior;
|
|
1388
1311
|
if (includeUndefinedValues) {
|
|
1389
1312
|
obj[key] = computedDefault;
|
|
1390
1313
|
} else if (emptyObjectFields !== 'skipDefaults') {
|
|
1391
1314
|
if (isObject(computedDefault)) {
|
|
1392
1315
|
// If isParentRequired is undefined, then we are at the root level of the schema so defer to the requiredness of
|
|
1393
1316
|
// the field key itself in the `requiredField` list
|
|
1394
|
-
|
|
1317
|
+
const isSelfOrParentRequired = isParentRequired === undefined ? requiredFields.includes(key) : isParentRequired;
|
|
1395
1318
|
// Store computedDefault if it's a non-empty object(e.g. not {}) and satisfies certain conditions
|
|
1396
1319
|
// Condition 1: If computedDefault is not empty or if the key is a required field
|
|
1397
1320
|
// Condition 2: If the parent object is required or emptyObjectFields is not 'populateRequiredDefaults'
|
|
@@ -1424,100 +1347,98 @@ function maybeAddDefaultToObject(obj, key, computedDefault, includeUndefinedValu
|
|
|
1424
1347
|
* @param [props.required] - Optional flag, if true, indicates this schema was required in the parent schema.
|
|
1425
1348
|
* @returns - The resulting `formData` with all the defaults provided
|
|
1426
1349
|
*/
|
|
1427
|
-
function computeDefaults(validator, rawSchema,
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
experimental_defaultFormStateBehavior = _ref$experimental_def === void 0 ? undefined : _ref$experimental_def,
|
|
1439
|
-
required = _ref.required;
|
|
1440
|
-
var formData = isObject(rawFormData) ? rawFormData : {};
|
|
1441
|
-
var schema = isObject(rawSchema) ? rawSchema : {};
|
|
1350
|
+
function computeDefaults(validator, rawSchema, {
|
|
1351
|
+
parentDefaults,
|
|
1352
|
+
rawFormData,
|
|
1353
|
+
rootSchema = {},
|
|
1354
|
+
includeUndefinedValues = false,
|
|
1355
|
+
_recurseList = [],
|
|
1356
|
+
experimental_defaultFormStateBehavior = undefined,
|
|
1357
|
+
required
|
|
1358
|
+
} = {}) {
|
|
1359
|
+
const formData = isObject(rawFormData) ? rawFormData : {};
|
|
1360
|
+
const schema = isObject(rawSchema) ? rawSchema : {};
|
|
1442
1361
|
// Compute the defaults recursively: give highest priority to deepest nodes.
|
|
1443
|
-
|
|
1362
|
+
let defaults = parentDefaults;
|
|
1444
1363
|
// If we get a new schema, then we need to recompute defaults again for the new schema found.
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
if (isObject(defaults) && isObject(schema
|
|
1364
|
+
let schemaToCompute = null;
|
|
1365
|
+
let updatedRecurseList = _recurseList;
|
|
1366
|
+
if (isObject(defaults) && isObject(schema.default)) {
|
|
1448
1367
|
// For object defaults, only override parent defaults that are defined in
|
|
1449
1368
|
// schema.default.
|
|
1450
|
-
defaults = mergeObjects(defaults, schema
|
|
1369
|
+
defaults = mergeObjects(defaults, schema.default);
|
|
1451
1370
|
} else if (DEFAULT_KEY in schema) {
|
|
1452
|
-
defaults = schema
|
|
1371
|
+
defaults = schema.default;
|
|
1453
1372
|
} else if (REF_KEY in schema) {
|
|
1454
|
-
|
|
1373
|
+
const refName = schema[REF_KEY];
|
|
1455
1374
|
// Use referenced schema defaults for this node.
|
|
1456
1375
|
if (!_recurseList.includes(refName)) {
|
|
1457
1376
|
updatedRecurseList = _recurseList.concat(refName);
|
|
1458
1377
|
schemaToCompute = findSchemaDefinition(refName, rootSchema);
|
|
1459
1378
|
}
|
|
1460
1379
|
} else if (DEPENDENCIES_KEY in schema) {
|
|
1461
|
-
|
|
1380
|
+
const resolvedSchema = resolveDependencies(validator, schema, rootSchema, false, formData);
|
|
1462
1381
|
schemaToCompute = resolvedSchema[0]; // pick the first element from resolve dependencies
|
|
1463
1382
|
} else if (isFixedItems(schema)) {
|
|
1464
|
-
defaults = schema.items.map(
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
});
|
|
1474
|
-
});
|
|
1383
|
+
defaults = schema.items.map((itemSchema, idx) => computeDefaults(validator, itemSchema, {
|
|
1384
|
+
rootSchema,
|
|
1385
|
+
includeUndefinedValues,
|
|
1386
|
+
_recurseList,
|
|
1387
|
+
experimental_defaultFormStateBehavior,
|
|
1388
|
+
parentDefaults: Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined,
|
|
1389
|
+
rawFormData: formData,
|
|
1390
|
+
required
|
|
1391
|
+
}));
|
|
1475
1392
|
} else if (ONE_OF_KEY in schema) {
|
|
1476
|
-
|
|
1477
|
-
|
|
1393
|
+
const {
|
|
1394
|
+
oneOf,
|
|
1395
|
+
...remaining
|
|
1396
|
+
} = schema;
|
|
1478
1397
|
if (oneOf.length === 0) {
|
|
1479
1398
|
return undefined;
|
|
1480
1399
|
}
|
|
1481
|
-
|
|
1400
|
+
const discriminator = getDiscriminatorFieldFromSchema(schema);
|
|
1482
1401
|
schemaToCompute = oneOf[getClosestMatchingOption(validator, rootSchema, isEmpty__default["default"](formData) ? undefined : formData, oneOf, 0, discriminator)];
|
|
1483
|
-
schemaToCompute =
|
|
1402
|
+
schemaToCompute = mergeSchemas(remaining, schemaToCompute);
|
|
1484
1403
|
} else if (ANY_OF_KEY in schema) {
|
|
1485
|
-
|
|
1486
|
-
|
|
1404
|
+
const {
|
|
1405
|
+
anyOf,
|
|
1406
|
+
...remaining
|
|
1407
|
+
} = schema;
|
|
1487
1408
|
if (anyOf.length === 0) {
|
|
1488
1409
|
return undefined;
|
|
1489
1410
|
}
|
|
1490
|
-
|
|
1491
|
-
schemaToCompute = anyOf[getClosestMatchingOption(validator, rootSchema, isEmpty__default["default"](formData) ? undefined : formData, anyOf, 0,
|
|
1492
|
-
schemaToCompute =
|
|
1411
|
+
const discriminator = getDiscriminatorFieldFromSchema(schema);
|
|
1412
|
+
schemaToCompute = anyOf[getClosestMatchingOption(validator, rootSchema, isEmpty__default["default"](formData) ? undefined : formData, anyOf, 0, discriminator)];
|
|
1413
|
+
schemaToCompute = mergeSchemas(remaining, schemaToCompute);
|
|
1493
1414
|
}
|
|
1494
1415
|
if (schemaToCompute) {
|
|
1495
1416
|
return computeDefaults(validator, schemaToCompute, {
|
|
1496
|
-
rootSchema
|
|
1497
|
-
includeUndefinedValues
|
|
1417
|
+
rootSchema,
|
|
1418
|
+
includeUndefinedValues,
|
|
1498
1419
|
_recurseList: updatedRecurseList,
|
|
1499
|
-
experimental_defaultFormStateBehavior
|
|
1420
|
+
experimental_defaultFormStateBehavior,
|
|
1500
1421
|
parentDefaults: defaults,
|
|
1501
1422
|
rawFormData: formData,
|
|
1502
|
-
required
|
|
1423
|
+
required
|
|
1503
1424
|
});
|
|
1504
1425
|
}
|
|
1505
1426
|
// No defaults defined for this node, fallback to generic typed ones.
|
|
1506
1427
|
if (defaults === undefined) {
|
|
1507
|
-
defaults = schema
|
|
1428
|
+
defaults = schema.default;
|
|
1508
1429
|
}
|
|
1509
1430
|
switch (getSchemaType(schema)) {
|
|
1510
1431
|
// We need to recurse for object schema inner default values.
|
|
1511
1432
|
case 'object':
|
|
1512
1433
|
{
|
|
1513
|
-
|
|
1434
|
+
const objectDefaults = Object.keys(schema.properties || {}).reduce((acc, key) => {
|
|
1514
1435
|
var _schema$required;
|
|
1515
1436
|
// Compute the defaults for this node, with the parent defaults we might
|
|
1516
1437
|
// have from a previous run: defaults[key].
|
|
1517
|
-
|
|
1518
|
-
rootSchema
|
|
1519
|
-
_recurseList
|
|
1520
|
-
experimental_defaultFormStateBehavior
|
|
1438
|
+
const computedDefault = computeDefaults(validator, get__default["default"](schema, [PROPERTIES_KEY, key]), {
|
|
1439
|
+
rootSchema,
|
|
1440
|
+
_recurseList,
|
|
1441
|
+
experimental_defaultFormStateBehavior,
|
|
1521
1442
|
includeUndefinedValues: includeUndefinedValues === true,
|
|
1522
1443
|
parentDefaults: get__default["default"](defaults, [key]),
|
|
1523
1444
|
rawFormData: get__default["default"](formData, [key]),
|
|
@@ -1528,31 +1449,25 @@ function computeDefaults(validator, rawSchema, _temp) {
|
|
|
1528
1449
|
}, {});
|
|
1529
1450
|
if (schema.additionalProperties) {
|
|
1530
1451
|
// as per spec additionalProperties may be either schema or boolean
|
|
1531
|
-
|
|
1532
|
-
|
|
1452
|
+
const additionalPropertiesSchema = isObject(schema.additionalProperties) ? schema.additionalProperties : {};
|
|
1453
|
+
const keys = new Set();
|
|
1533
1454
|
if (isObject(defaults)) {
|
|
1534
|
-
Object.keys(defaults).filter(
|
|
1535
|
-
return !schema.properties || !schema.properties[key];
|
|
1536
|
-
}).forEach(function (key) {
|
|
1537
|
-
return keys.add(key);
|
|
1538
|
-
});
|
|
1455
|
+
Object.keys(defaults).filter(key => !schema.properties || !schema.properties[key]).forEach(key => keys.add(key));
|
|
1539
1456
|
}
|
|
1540
|
-
|
|
1457
|
+
let formDataRequired;
|
|
1541
1458
|
if (isObject(formData)) {
|
|
1542
1459
|
formDataRequired = [];
|
|
1543
|
-
Object.keys(formData).filter(
|
|
1544
|
-
return !schema.properties || !schema.properties[key];
|
|
1545
|
-
}).forEach(function (key) {
|
|
1460
|
+
Object.keys(formData).filter(key => !schema.properties || !schema.properties[key]).forEach(key => {
|
|
1546
1461
|
keys.add(key);
|
|
1547
1462
|
formDataRequired.push(key);
|
|
1548
1463
|
});
|
|
1549
1464
|
}
|
|
1550
|
-
keys.forEach(
|
|
1465
|
+
keys.forEach(key => {
|
|
1551
1466
|
var _schema$required2;
|
|
1552
|
-
|
|
1553
|
-
rootSchema
|
|
1554
|
-
_recurseList
|
|
1555
|
-
experimental_defaultFormStateBehavior
|
|
1467
|
+
const computedDefault = computeDefaults(validator, additionalPropertiesSchema, {
|
|
1468
|
+
rootSchema,
|
|
1469
|
+
_recurseList,
|
|
1470
|
+
experimental_defaultFormStateBehavior,
|
|
1556
1471
|
includeUndefinedValues: includeUndefinedValues === true,
|
|
1557
1472
|
parentDefaults: get__default["default"](defaults, [key]),
|
|
1558
1473
|
rawFormData: get__default["default"](formData, [key]),
|
|
@@ -1566,54 +1481,54 @@ function computeDefaults(validator, rawSchema, _temp) {
|
|
|
1566
1481
|
}
|
|
1567
1482
|
case 'array':
|
|
1568
1483
|
{
|
|
1569
|
-
var
|
|
1484
|
+
var _experimental_default;
|
|
1570
1485
|
// Inject defaults into existing array defaults
|
|
1571
1486
|
if (Array.isArray(defaults)) {
|
|
1572
|
-
defaults = defaults.map(
|
|
1573
|
-
|
|
1487
|
+
defaults = defaults.map((item, idx) => {
|
|
1488
|
+
const schemaItem = getInnerSchemaForArrayItem(schema, AdditionalItemsHandling.Fallback, idx);
|
|
1574
1489
|
return computeDefaults(validator, schemaItem, {
|
|
1575
|
-
rootSchema
|
|
1576
|
-
_recurseList
|
|
1577
|
-
experimental_defaultFormStateBehavior
|
|
1490
|
+
rootSchema,
|
|
1491
|
+
_recurseList,
|
|
1492
|
+
experimental_defaultFormStateBehavior,
|
|
1578
1493
|
parentDefaults: item,
|
|
1579
|
-
required
|
|
1494
|
+
required
|
|
1580
1495
|
});
|
|
1581
1496
|
});
|
|
1582
1497
|
}
|
|
1583
1498
|
// Deeply inject defaults into already existing form data
|
|
1584
1499
|
if (Array.isArray(rawFormData)) {
|
|
1585
|
-
|
|
1586
|
-
defaults = rawFormData.map(
|
|
1500
|
+
const schemaItem = getInnerSchemaForArrayItem(schema);
|
|
1501
|
+
defaults = rawFormData.map((item, idx) => {
|
|
1587
1502
|
return computeDefaults(validator, schemaItem, {
|
|
1588
|
-
rootSchema
|
|
1589
|
-
_recurseList
|
|
1590
|
-
experimental_defaultFormStateBehavior
|
|
1503
|
+
rootSchema,
|
|
1504
|
+
_recurseList,
|
|
1505
|
+
experimental_defaultFormStateBehavior,
|
|
1591
1506
|
rawFormData: item,
|
|
1592
1507
|
parentDefaults: get__default["default"](defaults, [idx]),
|
|
1593
|
-
required
|
|
1508
|
+
required
|
|
1594
1509
|
});
|
|
1595
1510
|
});
|
|
1596
1511
|
}
|
|
1597
|
-
|
|
1512
|
+
const ignoreMinItemsFlagSet = (experimental_defaultFormStateBehavior === null || experimental_defaultFormStateBehavior === void 0 ? void 0 : (_experimental_default = experimental_defaultFormStateBehavior.arrayMinItems) === null || _experimental_default === void 0 ? void 0 : _experimental_default.populate) === 'requiredOnly';
|
|
1598
1513
|
if (ignoreMinItemsFlagSet && !required) {
|
|
1599
1514
|
// If no form data exists or defaults are set leave the field empty/non-existent, otherwise
|
|
1600
1515
|
// return form data/defaults
|
|
1601
1516
|
return defaults ? defaults : undefined;
|
|
1602
1517
|
}
|
|
1603
|
-
|
|
1518
|
+
const defaultsLength = Array.isArray(defaults) ? defaults.length : 0;
|
|
1604
1519
|
if (!schema.minItems || isMultiSelect(validator, schema, rootSchema) || schema.minItems <= defaultsLength) {
|
|
1605
1520
|
return defaults ? defaults : [];
|
|
1606
1521
|
}
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1522
|
+
const defaultEntries = defaults || [];
|
|
1523
|
+
const fillerSchema = getInnerSchemaForArrayItem(schema, AdditionalItemsHandling.Invert);
|
|
1524
|
+
const fillerDefault = fillerSchema.default;
|
|
1610
1525
|
// Calculate filler entries for remaining items (minItems - existing raw data/defaults)
|
|
1611
|
-
|
|
1526
|
+
const fillerEntries = new Array(schema.minItems - defaultsLength).fill(computeDefaults(validator, fillerSchema, {
|
|
1612
1527
|
parentDefaults: fillerDefault,
|
|
1613
|
-
rootSchema
|
|
1614
|
-
_recurseList
|
|
1615
|
-
experimental_defaultFormStateBehavior
|
|
1616
|
-
required
|
|
1528
|
+
rootSchema,
|
|
1529
|
+
_recurseList,
|
|
1530
|
+
experimental_defaultFormStateBehavior,
|
|
1531
|
+
required
|
|
1617
1532
|
}));
|
|
1618
1533
|
// then fill up the rest with either the item default or empty, up to minItems
|
|
1619
1534
|
return defaultEntries.concat(fillerEntries);
|
|
@@ -1634,26 +1549,24 @@ function computeDefaults(validator, rawSchema, _temp) {
|
|
|
1634
1549
|
* @param [experimental_defaultFormStateBehavior] Optional configuration object, if provided, allows users to override default form state behavior
|
|
1635
1550
|
* @returns - The resulting `formData` with all the defaults provided
|
|
1636
1551
|
*/
|
|
1637
|
-
function getDefaultFormState(validator, theSchema, formData, rootSchema, includeUndefinedValues, experimental_defaultFormStateBehavior) {
|
|
1638
|
-
if (includeUndefinedValues === void 0) {
|
|
1639
|
-
includeUndefinedValues = false;
|
|
1640
|
-
}
|
|
1552
|
+
function getDefaultFormState(validator, theSchema, formData, rootSchema, includeUndefinedValues = false, experimental_defaultFormStateBehavior) {
|
|
1641
1553
|
if (!isObject(theSchema)) {
|
|
1642
1554
|
throw new Error('Invalid schema: ' + theSchema);
|
|
1643
1555
|
}
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
rootSchema
|
|
1647
|
-
includeUndefinedValues
|
|
1648
|
-
experimental_defaultFormStateBehavior
|
|
1556
|
+
const schema = retrieveSchema(validator, theSchema, rootSchema, formData);
|
|
1557
|
+
const defaults = computeDefaults(validator, schema, {
|
|
1558
|
+
rootSchema,
|
|
1559
|
+
includeUndefinedValues,
|
|
1560
|
+
experimental_defaultFormStateBehavior,
|
|
1649
1561
|
rawFormData: formData
|
|
1650
1562
|
});
|
|
1651
1563
|
if (formData === undefined || formData === null || typeof formData === 'number' && isNaN(formData)) {
|
|
1652
1564
|
// No form data? Use schema defaults.
|
|
1653
1565
|
return defaults;
|
|
1654
1566
|
}
|
|
1655
|
-
|
|
1656
|
-
mergeExtraDefaults
|
|
1567
|
+
const {
|
|
1568
|
+
mergeExtraDefaults
|
|
1569
|
+
} = (experimental_defaultFormStateBehavior === null || experimental_defaultFormStateBehavior === void 0 ? void 0 : experimental_defaultFormStateBehavior.arrayMinItems) || {};
|
|
1657
1570
|
if (isObject(formData)) {
|
|
1658
1571
|
return mergeDefaultsWithFormData(defaults, formData, mergeExtraDefaults);
|
|
1659
1572
|
}
|
|
@@ -1668,10 +1581,7 @@ function getDefaultFormState(validator, theSchema, formData, rootSchema, include
|
|
|
1668
1581
|
* @param uiSchema - The UI Schema from which to detect if it is customized
|
|
1669
1582
|
* @returns - True if the `uiSchema` describes a custom widget, false otherwise
|
|
1670
1583
|
*/
|
|
1671
|
-
function isCustomWidget(uiSchema) {
|
|
1672
|
-
if (uiSchema === void 0) {
|
|
1673
|
-
uiSchema = {};
|
|
1674
|
-
}
|
|
1584
|
+
function isCustomWidget(uiSchema = {}) {
|
|
1675
1585
|
return (
|
|
1676
1586
|
// TODO: Remove the `&& uiSchema['ui:widget'] !== 'hidden'` once we support hidden widgets for arrays.
|
|
1677
1587
|
// https://rjsf-team.github.io/react-jsonschema-form/docs/usage/widgets/#hidden-widgets
|
|
@@ -1687,15 +1597,12 @@ function isCustomWidget(uiSchema) {
|
|
|
1687
1597
|
* @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
|
|
1688
1598
|
* @returns - True if schema/uiSchema contains an array of files, otherwise false
|
|
1689
1599
|
*/
|
|
1690
|
-
function isFilesArray(validator, schema, uiSchema, rootSchema) {
|
|
1691
|
-
if (uiSchema === void 0) {
|
|
1692
|
-
uiSchema = {};
|
|
1693
|
-
}
|
|
1600
|
+
function isFilesArray(validator, schema, uiSchema = {}, rootSchema) {
|
|
1694
1601
|
if (uiSchema[UI_WIDGET_KEY] === 'files') {
|
|
1695
1602
|
return true;
|
|
1696
1603
|
}
|
|
1697
1604
|
if (schema.items) {
|
|
1698
|
-
|
|
1605
|
+
const itemsSchema = retrieveSchema(validator, schema.items, rootSchema);
|
|
1699
1606
|
return itemsSchema.type === 'string' && itemsSchema.format === 'data-url';
|
|
1700
1607
|
}
|
|
1701
1608
|
return false;
|
|
@@ -1711,15 +1618,13 @@ function isFilesArray(validator, schema, uiSchema, rootSchema) {
|
|
|
1711
1618
|
* @param [globalOptions={}] - The optional Global UI Schema from which to get any fallback `xxx` options
|
|
1712
1619
|
* @returns - True if the label should be displayed or false if it should not
|
|
1713
1620
|
*/
|
|
1714
|
-
function getDisplayLabel(validator, schema, uiSchema, rootSchema, globalOptions) {
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
var displayLabel = !!label;
|
|
1722
|
-
var schemaType = getSchemaType(schema);
|
|
1621
|
+
function getDisplayLabel(validator, schema, uiSchema = {}, rootSchema, globalOptions) {
|
|
1622
|
+
const uiOptions = getUiOptions(uiSchema, globalOptions);
|
|
1623
|
+
const {
|
|
1624
|
+
label = true
|
|
1625
|
+
} = uiOptions;
|
|
1626
|
+
let displayLabel = !!label;
|
|
1627
|
+
const schemaType = getSchemaType(schema);
|
|
1723
1628
|
if (schemaType === 'array') {
|
|
1724
1629
|
displayLabel = isMultiSelect(validator, schema, rootSchema) || isFilesArray(validator, schema, uiSchema, rootSchema) || isCustomWidget(uiSchema);
|
|
1725
1630
|
}
|
|
@@ -1751,21 +1656,23 @@ function mergeValidationData(validator, validationData, additionalErrorSchema) {
|
|
|
1751
1656
|
if (!additionalErrorSchema) {
|
|
1752
1657
|
return validationData;
|
|
1753
1658
|
}
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1659
|
+
const {
|
|
1660
|
+
errors: oldErrors,
|
|
1661
|
+
errorSchema: oldErrorSchema
|
|
1662
|
+
} = validationData;
|
|
1663
|
+
let errors = validator.toErrorList(additionalErrorSchema);
|
|
1664
|
+
let errorSchema = additionalErrorSchema;
|
|
1758
1665
|
if (!isEmpty__default["default"](oldErrorSchema)) {
|
|
1759
1666
|
errorSchema = mergeObjects(oldErrorSchema, additionalErrorSchema, true);
|
|
1760
|
-
errors = [].concat(
|
|
1667
|
+
errors = [...oldErrors].concat(errors);
|
|
1761
1668
|
}
|
|
1762
1669
|
return {
|
|
1763
|
-
errorSchema
|
|
1764
|
-
errors
|
|
1670
|
+
errorSchema,
|
|
1671
|
+
errors
|
|
1765
1672
|
};
|
|
1766
1673
|
}
|
|
1767
1674
|
|
|
1768
|
-
|
|
1675
|
+
const NO_VALUE = /*#__PURE__*/Symbol('no Value');
|
|
1769
1676
|
/** Sanitize the `data` associated with the `oldSchema` so it is considered appropriate for the `newSchema`. If the new
|
|
1770
1677
|
* schema does not contain any properties, then `undefined` is returned to clear all the form data. Due to the nature
|
|
1771
1678
|
* of schemas, this sanitization happens recursively for nested objects of data. Also, any properties in the old schema
|
|
@@ -1813,31 +1720,28 @@ var NO_VALUE = /*#__PURE__*/Symbol('no Value');
|
|
|
1813
1720
|
* @returns - The new form data, with all the fields uniquely associated with the old schema set
|
|
1814
1721
|
* to `undefined`. Will return `undefined` if the new schema is not an object containing properties.
|
|
1815
1722
|
*/
|
|
1816
|
-
function sanitizeDataForNewSchema(validator, rootSchema, newSchema, oldSchema, data) {
|
|
1817
|
-
if (data === void 0) {
|
|
1818
|
-
data = {};
|
|
1819
|
-
}
|
|
1723
|
+
function sanitizeDataForNewSchema(validator, rootSchema, newSchema, oldSchema, data = {}) {
|
|
1820
1724
|
// By default, we will clear the form data
|
|
1821
|
-
|
|
1725
|
+
let newFormData;
|
|
1822
1726
|
// If the new schema is of type object and that object contains a list of properties
|
|
1823
1727
|
if (has__default["default"](newSchema, PROPERTIES_KEY)) {
|
|
1824
1728
|
// Create an object containing root-level keys in the old schema, setting each key to undefined to remove the data
|
|
1825
|
-
|
|
1729
|
+
const removeOldSchemaData = {};
|
|
1826
1730
|
if (has__default["default"](oldSchema, PROPERTIES_KEY)) {
|
|
1827
|
-
|
|
1828
|
-
Object.keys(properties).forEach(
|
|
1731
|
+
const properties = get__default["default"](oldSchema, PROPERTIES_KEY, {});
|
|
1732
|
+
Object.keys(properties).forEach(key => {
|
|
1829
1733
|
if (has__default["default"](data, key)) {
|
|
1830
1734
|
removeOldSchemaData[key] = undefined;
|
|
1831
1735
|
}
|
|
1832
1736
|
});
|
|
1833
1737
|
}
|
|
1834
|
-
|
|
1738
|
+
const keys = Object.keys(get__default["default"](newSchema, PROPERTIES_KEY, {}));
|
|
1835
1739
|
// Create a place to store nested data that will be a side-effect of the filter
|
|
1836
|
-
|
|
1837
|
-
keys.forEach(
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1740
|
+
const nestedData = {};
|
|
1741
|
+
keys.forEach(key => {
|
|
1742
|
+
const formValue = get__default["default"](data, key);
|
|
1743
|
+
let oldKeyedSchema = get__default["default"](oldSchema, [PROPERTIES_KEY, key], {});
|
|
1744
|
+
let newKeyedSchema = get__default["default"](newSchema, [PROPERTIES_KEY, key], {});
|
|
1841
1745
|
// Resolve the refs if they exist
|
|
1842
1746
|
if (has__default["default"](oldKeyedSchema, REF_KEY)) {
|
|
1843
1747
|
oldKeyedSchema = retrieveSchema(validator, oldKeyedSchema, rootSchema, formValue);
|
|
@@ -1846,8 +1750,8 @@ function sanitizeDataForNewSchema(validator, rootSchema, newSchema, oldSchema, d
|
|
|
1846
1750
|
newKeyedSchema = retrieveSchema(validator, newKeyedSchema, rootSchema, formValue);
|
|
1847
1751
|
}
|
|
1848
1752
|
// Now get types and see if they are the same
|
|
1849
|
-
|
|
1850
|
-
|
|
1753
|
+
const oldSchemaTypeForKey = get__default["default"](oldKeyedSchema, 'type');
|
|
1754
|
+
const newSchemaTypeForKey = get__default["default"](newKeyedSchema, 'type');
|
|
1851
1755
|
// Check if the old option has the same key with the same type
|
|
1852
1756
|
if (!oldSchemaTypeForKey || oldSchemaTypeForKey === newSchemaTypeForKey) {
|
|
1853
1757
|
if (has__default["default"](removeOldSchemaData, key)) {
|
|
@@ -1857,7 +1761,7 @@ function sanitizeDataForNewSchema(validator, rootSchema, newSchema, oldSchema, d
|
|
|
1857
1761
|
// If it is an object, we'll recurse and store the resulting sanitized data for the key
|
|
1858
1762
|
if (newSchemaTypeForKey === 'object' || newSchemaTypeForKey === 'array' && Array.isArray(formValue)) {
|
|
1859
1763
|
// SIDE-EFFECT: process the new schema type of object recursively to save iterations
|
|
1860
|
-
|
|
1764
|
+
const itemData = sanitizeDataForNewSchema(validator, rootSchema, newKeyedSchema, oldKeyedSchema, formValue);
|
|
1861
1765
|
if (itemData !== undefined || newSchemaTypeForKey === 'array') {
|
|
1862
1766
|
// only put undefined values for the array type and not the object type
|
|
1863
1767
|
nestedData[key] = itemData;
|
|
@@ -1866,8 +1770,8 @@ function sanitizeDataForNewSchema(validator, rootSchema, newSchema, oldSchema, d
|
|
|
1866
1770
|
// Ok, the non-object types match, let's make sure that a default or a const of a different value is replaced
|
|
1867
1771
|
// with the new default or const. This allows the case where two schemas differ that only by the default/const
|
|
1868
1772
|
// value to be properly selected
|
|
1869
|
-
|
|
1870
|
-
|
|
1773
|
+
const newOptionDefault = get__default["default"](newKeyedSchema, 'default', NO_VALUE);
|
|
1774
|
+
const oldOptionDefault = get__default["default"](oldKeyedSchema, 'default', NO_VALUE);
|
|
1871
1775
|
if (newOptionDefault !== NO_VALUE && newOptionDefault !== formValue) {
|
|
1872
1776
|
if (oldOptionDefault === formValue) {
|
|
1873
1777
|
// If the old default matches the formValue, we'll update the new value to match the new default
|
|
@@ -1877,8 +1781,8 @@ function sanitizeDataForNewSchema(validator, rootSchema, newSchema, oldSchema, d
|
|
|
1877
1781
|
removeOldSchemaData[key] = undefined;
|
|
1878
1782
|
}
|
|
1879
1783
|
}
|
|
1880
|
-
|
|
1881
|
-
|
|
1784
|
+
const newOptionConst = get__default["default"](newKeyedSchema, 'const', NO_VALUE);
|
|
1785
|
+
const oldOptionConst = get__default["default"](oldKeyedSchema, 'const', NO_VALUE);
|
|
1882
1786
|
if (newOptionConst !== NO_VALUE && newOptionConst !== formValue) {
|
|
1883
1787
|
// Since this is a const, if the old value matches, replace the value with the new const otherwise clear it
|
|
1884
1788
|
removeOldSchemaData[key] = oldOptionConst === formValue ? newOptionConst : undefined;
|
|
@@ -1886,11 +1790,15 @@ function sanitizeDataForNewSchema(validator, rootSchema, newSchema, oldSchema, d
|
|
|
1886
1790
|
}
|
|
1887
1791
|
}
|
|
1888
1792
|
});
|
|
1889
|
-
newFormData =
|
|
1793
|
+
newFormData = {
|
|
1794
|
+
...data,
|
|
1795
|
+
...removeOldSchemaData,
|
|
1796
|
+
...nestedData
|
|
1797
|
+
};
|
|
1890
1798
|
// First apply removing the old schema data, then apply the nested data, then apply the old data keys to keep
|
|
1891
1799
|
} else if (get__default["default"](oldSchema, 'type') === 'array' && get__default["default"](newSchema, 'type') === 'array' && Array.isArray(data)) {
|
|
1892
|
-
|
|
1893
|
-
|
|
1800
|
+
let oldSchemaItems = get__default["default"](oldSchema, 'items');
|
|
1801
|
+
let newSchemaItems = get__default["default"](newSchema, 'items');
|
|
1894
1802
|
// If any of the array types `items` are arrays (remember arrays are objects) then we'll just drop the data
|
|
1895
1803
|
// Eventually, we may want to deal with when either of the `items` are arrays since those tuple validations
|
|
1896
1804
|
if (typeof oldSchemaItems === 'object' && typeof newSchemaItems === 'object' && !Array.isArray(oldSchemaItems) && !Array.isArray(newSchemaItems)) {
|
|
@@ -1901,14 +1809,14 @@ function sanitizeDataForNewSchema(validator, rootSchema, newSchema, oldSchema, d
|
|
|
1901
1809
|
newSchemaItems = retrieveSchema(validator, newSchemaItems, rootSchema, data);
|
|
1902
1810
|
}
|
|
1903
1811
|
// Now get types and see if they are the same
|
|
1904
|
-
|
|
1905
|
-
|
|
1812
|
+
const oldSchemaType = get__default["default"](oldSchemaItems, 'type');
|
|
1813
|
+
const newSchemaType = get__default["default"](newSchemaItems, 'type');
|
|
1906
1814
|
// Check if the old option has the same key with the same type
|
|
1907
1815
|
if (!oldSchemaType || oldSchemaType === newSchemaType) {
|
|
1908
|
-
|
|
1816
|
+
const maxItems = get__default["default"](newSchema, 'maxItems', -1);
|
|
1909
1817
|
if (newSchemaType === 'object') {
|
|
1910
|
-
newFormData = data.reduce(
|
|
1911
|
-
|
|
1818
|
+
newFormData = data.reduce((newValue, aValue) => {
|
|
1819
|
+
const itemValue = sanitizeDataForNewSchema(validator, rootSchema, newSchemaItems, oldSchemaItems, aValue);
|
|
1912
1820
|
if (itemValue !== undefined && (maxItems < 0 || newValue.length < maxItems)) {
|
|
1913
1821
|
newValue.push(itemValue);
|
|
1914
1822
|
}
|
|
@@ -1941,15 +1849,10 @@ function sanitizeDataForNewSchema(validator, rootSchema, newSchema, oldSchema, d
|
|
|
1941
1849
|
* @param [_recurseList=[]] - The list of retrieved schemas currently being recursed, used to prevent infinite recursion
|
|
1942
1850
|
* @returns - The `IdSchema` object for the `schema`
|
|
1943
1851
|
*/
|
|
1944
|
-
function toIdSchemaInternal(validator, schema, idPrefix, idSeparator, id, rootSchema, formData, _recurseList) {
|
|
1945
|
-
if (_recurseList === void 0) {
|
|
1946
|
-
_recurseList = [];
|
|
1947
|
-
}
|
|
1852
|
+
function toIdSchemaInternal(validator, schema, idPrefix, idSeparator, id, rootSchema, formData, _recurseList = []) {
|
|
1948
1853
|
if (REF_KEY in schema || DEPENDENCIES_KEY in schema || ALL_OF_KEY in schema) {
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
return isEqual__default["default"](item, _schema);
|
|
1952
|
-
});
|
|
1854
|
+
const _schema = retrieveSchema(validator, schema, rootSchema, formData);
|
|
1855
|
+
const sameSchemaIndex = _recurseList.findIndex(item => isEqual__default["default"](item, _schema));
|
|
1953
1856
|
if (sameSchemaIndex === -1) {
|
|
1954
1857
|
return toIdSchemaInternal(validator, _schema, idPrefix, idSeparator, id, rootSchema, formData, _recurseList.concat(_schema));
|
|
1955
1858
|
}
|
|
@@ -1957,14 +1860,14 @@ function toIdSchemaInternal(validator, schema, idPrefix, idSeparator, id, rootSc
|
|
|
1957
1860
|
if (ITEMS_KEY in schema && !get__default["default"](schema, [ITEMS_KEY, REF_KEY])) {
|
|
1958
1861
|
return toIdSchemaInternal(validator, get__default["default"](schema, ITEMS_KEY), idPrefix, idSeparator, id, rootSchema, formData, _recurseList);
|
|
1959
1862
|
}
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
$id
|
|
1863
|
+
const $id = id || idPrefix;
|
|
1864
|
+
const idSchema = {
|
|
1865
|
+
$id
|
|
1963
1866
|
};
|
|
1964
1867
|
if (getSchemaType(schema) === 'object' && PROPERTIES_KEY in schema) {
|
|
1965
|
-
for (
|
|
1966
|
-
|
|
1967
|
-
|
|
1868
|
+
for (const name in schema.properties) {
|
|
1869
|
+
const field = get__default["default"](schema, [PROPERTIES_KEY, name]);
|
|
1870
|
+
const fieldId = idSchema[ID_KEY] + idSeparator + name;
|
|
1968
1871
|
idSchema[name] = toIdSchemaInternal(validator, isObject(field) ? field : {}, idPrefix, idSeparator, fieldId, rootSchema,
|
|
1969
1872
|
// It's possible that formData is not an object -- this can happen if an
|
|
1970
1873
|
// array item has just been added, but not populated with data yet
|
|
@@ -1984,13 +1887,7 @@ function toIdSchemaInternal(validator, schema, idPrefix, idSeparator, id, rootSc
|
|
|
1984
1887
|
* @param [idSeparator='_'] - The separator to use for the path segments in the id
|
|
1985
1888
|
* @returns - The `IdSchema` object for the `schema`
|
|
1986
1889
|
*/
|
|
1987
|
-
function toIdSchema(validator, schema, id, rootSchema, formData, idPrefix, idSeparator) {
|
|
1988
|
-
if (idPrefix === void 0) {
|
|
1989
|
-
idPrefix = 'root';
|
|
1990
|
-
}
|
|
1991
|
-
if (idSeparator === void 0) {
|
|
1992
|
-
idSeparator = '_';
|
|
1993
|
-
}
|
|
1890
|
+
function toIdSchema(validator, schema, id, rootSchema, formData, idPrefix = 'root', idSeparator = '_') {
|
|
1994
1891
|
return toIdSchemaInternal(validator, schema, idPrefix, idSeparator, id, rootSchema, formData);
|
|
1995
1892
|
}
|
|
1996
1893
|
|
|
@@ -2005,39 +1902,38 @@ function toIdSchema(validator, schema, id, rootSchema, formData, idPrefix, idSep
|
|
|
2005
1902
|
* @param [_recurseList=[]] - The list of retrieved schemas currently being recursed, used to prevent infinite recursion
|
|
2006
1903
|
* @returns - The `PathSchema` object for the `schema`
|
|
2007
1904
|
*/
|
|
2008
|
-
function toPathSchemaInternal(validator, schema, name, rootSchema, formData, _recurseList) {
|
|
2009
|
-
var _pathSchema;
|
|
2010
|
-
if (_recurseList === void 0) {
|
|
2011
|
-
_recurseList = [];
|
|
2012
|
-
}
|
|
1905
|
+
function toPathSchemaInternal(validator, schema, name, rootSchema, formData, _recurseList = []) {
|
|
2013
1906
|
if (REF_KEY in schema || DEPENDENCIES_KEY in schema || ALL_OF_KEY in schema) {
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
return isEqual__default["default"](item, _schema);
|
|
2017
|
-
});
|
|
1907
|
+
const _schema = retrieveSchema(validator, schema, rootSchema, formData);
|
|
1908
|
+
const sameSchemaIndex = _recurseList.findIndex(item => isEqual__default["default"](item, _schema));
|
|
2018
1909
|
if (sameSchemaIndex === -1) {
|
|
2019
1910
|
return toPathSchemaInternal(validator, _schema, name, rootSchema, formData, _recurseList.concat(_schema));
|
|
2020
1911
|
}
|
|
2021
1912
|
}
|
|
2022
|
-
|
|
1913
|
+
let pathSchema = {
|
|
1914
|
+
[NAME_KEY]: name.replace(/^\./, '')
|
|
1915
|
+
};
|
|
2023
1916
|
if (ONE_OF_KEY in schema || ANY_OF_KEY in schema) {
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
pathSchema =
|
|
1917
|
+
const xxxOf = ONE_OF_KEY in schema ? schema.oneOf : schema.anyOf;
|
|
1918
|
+
const discriminator = getDiscriminatorFieldFromSchema(schema);
|
|
1919
|
+
const index = getClosestMatchingOption(validator, rootSchema, formData, xxxOf, 0, discriminator);
|
|
1920
|
+
const _schema = xxxOf[index];
|
|
1921
|
+
pathSchema = {
|
|
1922
|
+
...pathSchema,
|
|
1923
|
+
...toPathSchemaInternal(validator, _schema, name, rootSchema, formData, _recurseList)
|
|
1924
|
+
};
|
|
2029
1925
|
}
|
|
2030
1926
|
if (ADDITIONAL_PROPERTIES_KEY in schema && schema[ADDITIONAL_PROPERTIES_KEY] !== false) {
|
|
2031
1927
|
set__default["default"](pathSchema, RJSF_ADDITONAL_PROPERTIES_FLAG, true);
|
|
2032
1928
|
}
|
|
2033
1929
|
if (ITEMS_KEY in schema && Array.isArray(formData)) {
|
|
2034
|
-
formData.forEach(
|
|
2035
|
-
pathSchema[i] = toPathSchemaInternal(validator, schema.items, name
|
|
1930
|
+
formData.forEach((element, i) => {
|
|
1931
|
+
pathSchema[i] = toPathSchemaInternal(validator, schema.items, `${name}.${i}`, rootSchema, element, _recurseList);
|
|
2036
1932
|
});
|
|
2037
1933
|
} else if (PROPERTIES_KEY in schema) {
|
|
2038
|
-
for (
|
|
2039
|
-
|
|
2040
|
-
pathSchema[property] = toPathSchemaInternal(validator, field, name
|
|
1934
|
+
for (const property in schema.properties) {
|
|
1935
|
+
const field = get__default["default"](schema, [PROPERTIES_KEY, property]);
|
|
1936
|
+
pathSchema[property] = toPathSchemaInternal(validator, field, `${name}.${property}`, rootSchema,
|
|
2041
1937
|
// It's possible that formData is not an object -- this can happen if an
|
|
2042
1938
|
// array item has just been added, but not populated with data yet
|
|
2043
1939
|
get__default["default"](formData, [property]), _recurseList);
|
|
@@ -2054,10 +1950,7 @@ function toPathSchemaInternal(validator, schema, name, rootSchema, formData, _re
|
|
|
2054
1950
|
* @param [formData] - The current formData, if any, to assist retrieving a schema
|
|
2055
1951
|
* @returns - The `PathSchema` object for the `schema`
|
|
2056
1952
|
*/
|
|
2057
|
-
function toPathSchema(validator, schema, name, rootSchema, formData) {
|
|
2058
|
-
if (name === void 0) {
|
|
2059
|
-
name = '';
|
|
2060
|
-
}
|
|
1953
|
+
function toPathSchema(validator, schema, name = '', rootSchema, formData) {
|
|
2061
1954
|
return toPathSchemaInternal(validator, schema, name, rootSchema, formData);
|
|
2062
1955
|
}
|
|
2063
1956
|
|
|
@@ -2066,14 +1959,14 @@ function toPathSchema(validator, schema, name, rootSchema, formData) {
|
|
|
2066
1959
|
* Since these generally do not change across a `Form`, this allows for providing a simplified set of APIs to the
|
|
2067
1960
|
* `@rjsf/core` components and the various themes as well. This class implements the `SchemaUtilsType` interface.
|
|
2068
1961
|
*/
|
|
2069
|
-
|
|
1962
|
+
class SchemaUtils {
|
|
2070
1963
|
/** Constructs the `SchemaUtils` instance with the given `validator` and `rootSchema` stored as instance variables
|
|
2071
1964
|
*
|
|
2072
1965
|
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
2073
1966
|
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
2074
1967
|
* @param experimental_defaultFormStateBehavior - Configuration flags to allow users to override default form state behavior
|
|
2075
1968
|
*/
|
|
2076
|
-
|
|
1969
|
+
constructor(validator, rootSchema, experimental_defaultFormStateBehavior) {
|
|
2077
1970
|
this.rootSchema = void 0;
|
|
2078
1971
|
this.validator = void 0;
|
|
2079
1972
|
this.experimental_defaultFormStateBehavior = void 0;
|
|
@@ -2085,8 +1978,7 @@ var SchemaUtils = /*#__PURE__*/function () {
|
|
|
2085
1978
|
*
|
|
2086
1979
|
* @returns - The `ValidatorType`
|
|
2087
1980
|
*/
|
|
2088
|
-
|
|
2089
|
-
_proto.getValidator = function getValidator() {
|
|
1981
|
+
getValidator() {
|
|
2090
1982
|
return this.validator;
|
|
2091
1983
|
}
|
|
2092
1984
|
/** Determines whether either the `validator` and `rootSchema` differ from the ones associated with this instance of
|
|
@@ -2097,11 +1989,8 @@ var SchemaUtils = /*#__PURE__*/function () {
|
|
|
2097
1989
|
* @param rootSchema - The root schema that will be compared against the current one
|
|
2098
1990
|
* @param [experimental_defaultFormStateBehavior] Optional configuration object, if provided, allows users to override default form state behavior
|
|
2099
1991
|
* @returns - True if the `SchemaUtilsType` differs from the given `validator` or `rootSchema`
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
if (experimental_defaultFormStateBehavior === void 0) {
|
|
2103
|
-
experimental_defaultFormStateBehavior = {};
|
|
2104
|
-
}
|
|
1992
|
+
*/
|
|
1993
|
+
doesSchemaUtilsDiffer(validator, rootSchema, experimental_defaultFormStateBehavior = {}) {
|
|
2105
1994
|
if (!validator || !rootSchema) {
|
|
2106
1995
|
return false;
|
|
2107
1996
|
}
|
|
@@ -2116,11 +2005,8 @@ var SchemaUtils = /*#__PURE__*/function () {
|
|
|
2116
2005
|
* If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
|
|
2117
2006
|
* object properties.
|
|
2118
2007
|
* @returns - The resulting `formData` with all the defaults provided
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
if (includeUndefinedValues === void 0) {
|
|
2122
|
-
includeUndefinedValues = false;
|
|
2123
|
-
}
|
|
2008
|
+
*/
|
|
2009
|
+
getDefaultFormState(schema, formData, includeUndefinedValues = false) {
|
|
2124
2010
|
return getDefaultFormState(this.validator, schema, formData, this.rootSchema, includeUndefinedValues, this.experimental_defaultFormStateBehavior);
|
|
2125
2011
|
}
|
|
2126
2012
|
/** Determines whether the combination of `schema` and `uiSchema` properties indicates that the label for the `schema`
|
|
@@ -2130,8 +2016,8 @@ var SchemaUtils = /*#__PURE__*/function () {
|
|
|
2130
2016
|
* @param [uiSchema] - The UI schema from which to derive potentially displayable information
|
|
2131
2017
|
* @param [globalOptions={}] - The optional Global UI Schema from which to get any fallback `xxx` options
|
|
2132
2018
|
* @returns - True if the label should be displayed or false if it should not
|
|
2133
|
-
|
|
2134
|
-
|
|
2019
|
+
*/
|
|
2020
|
+
getDisplayLabel(schema, uiSchema, globalOptions) {
|
|
2135
2021
|
return getDisplayLabel(this.validator, schema, uiSchema, this.rootSchema, globalOptions);
|
|
2136
2022
|
}
|
|
2137
2023
|
/** Determines which of the given `options` provided most closely matches the `formData`.
|
|
@@ -2146,8 +2032,8 @@ var SchemaUtils = /*#__PURE__*/function () {
|
|
|
2146
2032
|
* @param [discriminatorField] - The optional name of the field within the options object whose value is used to
|
|
2147
2033
|
* determine which option is selected
|
|
2148
2034
|
* @returns - The index of the option that is the closest match to the `formData` or the `selectedOption` if no match
|
|
2149
|
-
|
|
2150
|
-
|
|
2035
|
+
*/
|
|
2036
|
+
getClosestMatchingOption(formData, options, selectedOption, discriminatorField) {
|
|
2151
2037
|
return getClosestMatchingOption(this.validator, this.rootSchema, formData, options, selectedOption, discriminatorField);
|
|
2152
2038
|
}
|
|
2153
2039
|
/** Given the `formData` and list of `options`, attempts to find the index of the first option that matches the data.
|
|
@@ -2158,8 +2044,8 @@ var SchemaUtils = /*#__PURE__*/function () {
|
|
|
2158
2044
|
* @param [discriminatorField] - The optional name of the field within the options object whose value is used to
|
|
2159
2045
|
* determine which option is selected
|
|
2160
2046
|
* @returns - The firstindex of the matched option or 0 if none is available
|
|
2161
|
-
|
|
2162
|
-
|
|
2047
|
+
*/
|
|
2048
|
+
getFirstMatchingOption(formData, options, discriminatorField) {
|
|
2163
2049
|
return getFirstMatchingOption(this.validator, formData, options, this.rootSchema, discriminatorField);
|
|
2164
2050
|
}
|
|
2165
2051
|
/** Given the `formData` and list of `options`, attempts to find the index of the option that best matches the data.
|
|
@@ -2171,8 +2057,8 @@ var SchemaUtils = /*#__PURE__*/function () {
|
|
|
2171
2057
|
* determine which option is selected
|
|
2172
2058
|
* @returns - The index of the matched option or 0 if none is available
|
|
2173
2059
|
* @deprecated
|
|
2174
|
-
|
|
2175
|
-
|
|
2060
|
+
*/
|
|
2061
|
+
getMatchingOption(formData, options, discriminatorField) {
|
|
2176
2062
|
return getMatchingOption(this.validator, formData, options, this.rootSchema, discriminatorField);
|
|
2177
2063
|
}
|
|
2178
2064
|
/** Checks to see if the `schema` and `uiSchema` combination represents an array of files
|
|
@@ -2180,24 +2066,24 @@ var SchemaUtils = /*#__PURE__*/function () {
|
|
|
2180
2066
|
* @param schema - The schema for which check for array of files flag is desired
|
|
2181
2067
|
* @param [uiSchema] - The UI schema from which to check the widget
|
|
2182
2068
|
* @returns - True if schema/uiSchema contains an array of files, otherwise false
|
|
2183
|
-
|
|
2184
|
-
|
|
2069
|
+
*/
|
|
2070
|
+
isFilesArray(schema, uiSchema) {
|
|
2185
2071
|
return isFilesArray(this.validator, schema, uiSchema, this.rootSchema);
|
|
2186
2072
|
}
|
|
2187
2073
|
/** Checks to see if the `schema` combination represents a multi-select
|
|
2188
2074
|
*
|
|
2189
2075
|
* @param schema - The schema for which check for a multi-select flag is desired
|
|
2190
2076
|
* @returns - True if schema contains a multi-select, otherwise false
|
|
2191
|
-
|
|
2192
|
-
|
|
2077
|
+
*/
|
|
2078
|
+
isMultiSelect(schema) {
|
|
2193
2079
|
return isMultiSelect(this.validator, schema, this.rootSchema);
|
|
2194
2080
|
}
|
|
2195
2081
|
/** Checks to see if the `schema` combination represents a select
|
|
2196
2082
|
*
|
|
2197
2083
|
* @param schema - The schema for which check for a select flag is desired
|
|
2198
2084
|
* @returns - True if schema contains a select, otherwise false
|
|
2199
|
-
|
|
2200
|
-
|
|
2085
|
+
*/
|
|
2086
|
+
isSelect(schema) {
|
|
2201
2087
|
return isSelect(this.validator, schema, this.rootSchema);
|
|
2202
2088
|
}
|
|
2203
2089
|
/** Merges the errors in `additionalErrorSchema` into the existing `validationData` by combining the hierarchies in
|
|
@@ -2210,8 +2096,8 @@ var SchemaUtils = /*#__PURE__*/function () {
|
|
|
2210
2096
|
* @returns - The `validationData` with the additional errors from `additionalErrorSchema` merged into it, if provided.
|
|
2211
2097
|
* @deprecated - Use the `validationDataMerge()` function exported from `@rjsf/utils` instead. This function will be
|
|
2212
2098
|
* removed in the next major release.
|
|
2213
|
-
|
|
2214
|
-
|
|
2099
|
+
*/
|
|
2100
|
+
mergeValidationData(validationData, additionalErrorSchema) {
|
|
2215
2101
|
return mergeValidationData(this.validator, validationData, additionalErrorSchema);
|
|
2216
2102
|
}
|
|
2217
2103
|
/** Retrieves an expanded schema that has had all of its conditions, additional properties, references and
|
|
@@ -2221,8 +2107,8 @@ var SchemaUtils = /*#__PURE__*/function () {
|
|
|
2221
2107
|
* @param schema - The schema for which retrieving a schema is desired
|
|
2222
2108
|
* @param [rawFormData] - The current formData, if any, to assist retrieving a schema
|
|
2223
2109
|
* @returns - The schema having its conditions, additional properties, references and dependencies resolved
|
|
2224
|
-
|
|
2225
|
-
|
|
2110
|
+
*/
|
|
2111
|
+
retrieveSchema(schema, rawFormData) {
|
|
2226
2112
|
return retrieveSchema(this.validator, schema, this.rootSchema, rawFormData);
|
|
2227
2113
|
}
|
|
2228
2114
|
/** Sanitize the `data` associated with the `oldSchema` so it is considered appropriate for the `newSchema`. If the
|
|
@@ -2235,8 +2121,8 @@ var SchemaUtils = /*#__PURE__*/function () {
|
|
|
2235
2121
|
* @param [data={}] - The form data associated with the schema, defaulting to an empty object when undefined
|
|
2236
2122
|
* @returns - The new form data, with all the fields uniquely associated with the old schema set
|
|
2237
2123
|
* to `undefined`. Will return `undefined` if the new schema is not an object containing properties.
|
|
2238
|
-
|
|
2239
|
-
|
|
2124
|
+
*/
|
|
2125
|
+
sanitizeDataForNewSchema(newSchema, oldSchema, data) {
|
|
2240
2126
|
return sanitizeDataForNewSchema(this.validator, this.rootSchema, newSchema, oldSchema, data);
|
|
2241
2127
|
}
|
|
2242
2128
|
/** Generates an `IdSchema` object for the `schema`, recursively
|
|
@@ -2247,14 +2133,8 @@ var SchemaUtils = /*#__PURE__*/function () {
|
|
|
2247
2133
|
* @param [idPrefix='root'] - The prefix to use for the id
|
|
2248
2134
|
* @param [idSeparator='_'] - The separator to use for the path segments in the id
|
|
2249
2135
|
* @returns - The `IdSchema` object for the `schema`
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
if (idPrefix === void 0) {
|
|
2253
|
-
idPrefix = 'root';
|
|
2254
|
-
}
|
|
2255
|
-
if (idSeparator === void 0) {
|
|
2256
|
-
idSeparator = '_';
|
|
2257
|
-
}
|
|
2136
|
+
*/
|
|
2137
|
+
toIdSchema(schema, id, formData, idPrefix = 'root', idSeparator = '_') {
|
|
2258
2138
|
return toIdSchema(this.validator, schema, id, this.rootSchema, formData, idPrefix, idSeparator);
|
|
2259
2139
|
}
|
|
2260
2140
|
/** Generates an `PathSchema` object for the `schema`, recursively
|
|
@@ -2263,12 +2143,11 @@ var SchemaUtils = /*#__PURE__*/function () {
|
|
|
2263
2143
|
* @param [name] - The base name for the schema
|
|
2264
2144
|
* @param [formData] - The current formData, if any, onto which to provide any missing defaults
|
|
2265
2145
|
* @returns - The `PathSchema` object for the `schema`
|
|
2266
|
-
|
|
2267
|
-
|
|
2146
|
+
*/
|
|
2147
|
+
toPathSchema(schema, name, formData) {
|
|
2268
2148
|
return toPathSchema(this.validator, schema, name, this.rootSchema, formData);
|
|
2269
|
-
}
|
|
2270
|
-
|
|
2271
|
-
}();
|
|
2149
|
+
}
|
|
2150
|
+
}
|
|
2272
2151
|
/** Creates a `SchemaUtilsType` interface that is based around the given `validator` and `rootSchema` parameters. The
|
|
2273
2152
|
* resulting interface implementation will forward the `validator` and `rootSchema` to all the wrapped APIs.
|
|
2274
2153
|
*
|
|
@@ -2277,10 +2156,7 @@ var SchemaUtils = /*#__PURE__*/function () {
|
|
|
2277
2156
|
* @param [experimental_defaultFormStateBehavior] Optional configuration object, if provided, allows users to override default form state behavior
|
|
2278
2157
|
* @returns - An implementation of a `SchemaUtilsType` interface
|
|
2279
2158
|
*/
|
|
2280
|
-
function createSchemaUtils(validator, rootSchema, experimental_defaultFormStateBehavior) {
|
|
2281
|
-
if (experimental_defaultFormStateBehavior === void 0) {
|
|
2282
|
-
experimental_defaultFormStateBehavior = {};
|
|
2283
|
-
}
|
|
2159
|
+
function createSchemaUtils(validator, rootSchema, experimental_defaultFormStateBehavior = {}) {
|
|
2284
2160
|
return new SchemaUtils(validator, rootSchema, experimental_defaultFormStateBehavior);
|
|
2285
2161
|
}
|
|
2286
2162
|
|
|
@@ -2292,17 +2168,17 @@ function createSchemaUtils(validator, rootSchema, experimental_defaultFormStateB
|
|
|
2292
2168
|
*/
|
|
2293
2169
|
function dataURItoBlob(dataURI) {
|
|
2294
2170
|
// Split metadata from data
|
|
2295
|
-
|
|
2171
|
+
const splitted = dataURI.split(',');
|
|
2296
2172
|
// Split params
|
|
2297
|
-
|
|
2173
|
+
const params = splitted[0].split(';');
|
|
2298
2174
|
// Get mime-type from params
|
|
2299
|
-
|
|
2175
|
+
const type = params[0].replace('data:', '');
|
|
2300
2176
|
// Filter the name property from params
|
|
2301
|
-
|
|
2177
|
+
const properties = params.filter(param => {
|
|
2302
2178
|
return param.split('=')[0] === 'name';
|
|
2303
2179
|
});
|
|
2304
2180
|
// Look for the name and use unknown if no name property.
|
|
2305
|
-
|
|
2181
|
+
let name;
|
|
2306
2182
|
if (properties.length !== 1) {
|
|
2307
2183
|
name = 'unknown';
|
|
2308
2184
|
} else {
|
|
@@ -2312,18 +2188,18 @@ function dataURItoBlob(dataURI) {
|
|
|
2312
2188
|
}
|
|
2313
2189
|
// Built the Uint8Array Blob parameter from the base64 string.
|
|
2314
2190
|
try {
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
for (
|
|
2191
|
+
const binary = atob(splitted[1]);
|
|
2192
|
+
const array = [];
|
|
2193
|
+
for (let i = 0; i < binary.length; i++) {
|
|
2318
2194
|
array.push(binary.charCodeAt(i));
|
|
2319
2195
|
}
|
|
2320
2196
|
// Create the blob object
|
|
2321
|
-
|
|
2322
|
-
type
|
|
2197
|
+
const blob = new window.Blob([new Uint8Array(array)], {
|
|
2198
|
+
type
|
|
2323
2199
|
});
|
|
2324
2200
|
return {
|
|
2325
|
-
blob
|
|
2326
|
-
name
|
|
2201
|
+
blob,
|
|
2202
|
+
name
|
|
2327
2203
|
};
|
|
2328
2204
|
} catch (error) {
|
|
2329
2205
|
return {
|
|
@@ -2345,13 +2221,11 @@ function dataURItoBlob(dataURI) {
|
|
|
2345
2221
|
* @returns - The updated string with any replacement specifiers replaced
|
|
2346
2222
|
*/
|
|
2347
2223
|
function replaceStringParameters(inputString, params) {
|
|
2348
|
-
|
|
2224
|
+
let output = inputString;
|
|
2349
2225
|
if (Array.isArray(params)) {
|
|
2350
|
-
|
|
2351
|
-
params.forEach(
|
|
2352
|
-
|
|
2353
|
-
return part === "%" + (index + 1);
|
|
2354
|
-
});
|
|
2226
|
+
const parts = output.split(/(%\d)/);
|
|
2227
|
+
params.forEach((param, index) => {
|
|
2228
|
+
const partIndex = parts.findIndex(part => part === `%${index + 1}`);
|
|
2355
2229
|
if (partIndex >= 0) {
|
|
2356
2230
|
parts[partIndex] = param;
|
|
2357
2231
|
}
|
|
@@ -2384,20 +2258,13 @@ function englishStringTranslator(stringToTranslate, params) {
|
|
|
2384
2258
|
* @returns - The single or list of values specified by the single or list of indexes if they are valid. Otherwise,
|
|
2385
2259
|
* `emptyValue` or an empty list.
|
|
2386
2260
|
*/
|
|
2387
|
-
function enumOptionsValueForIndex(valueIndex, allEnumOptions, emptyValue) {
|
|
2388
|
-
if (allEnumOptions === void 0) {
|
|
2389
|
-
allEnumOptions = [];
|
|
2390
|
-
}
|
|
2261
|
+
function enumOptionsValueForIndex(valueIndex, allEnumOptions = [], emptyValue) {
|
|
2391
2262
|
if (Array.isArray(valueIndex)) {
|
|
2392
|
-
return valueIndex.map(
|
|
2393
|
-
return enumOptionsValueForIndex(index, allEnumOptions);
|
|
2394
|
-
}).filter(function (val) {
|
|
2395
|
-
return val;
|
|
2396
|
-
});
|
|
2263
|
+
return valueIndex.map(index => enumOptionsValueForIndex(index, allEnumOptions)).filter(val => val);
|
|
2397
2264
|
}
|
|
2398
2265
|
// So Number(null) and Number('') both return 0, so use emptyValue for those two values
|
|
2399
|
-
|
|
2400
|
-
|
|
2266
|
+
const index = valueIndex === '' || valueIndex === null ? -1 : Number(valueIndex);
|
|
2267
|
+
const option = allEnumOptions[index];
|
|
2401
2268
|
return option ? option.value : emptyValue;
|
|
2402
2269
|
}
|
|
2403
2270
|
|
|
@@ -2413,15 +2280,10 @@ function enumOptionsValueForIndex(valueIndex, allEnumOptions, emptyValue) {
|
|
|
2413
2280
|
* unless `selected` is a single value. In that case, if the `valueIndex` value matches `selected`, returns
|
|
2414
2281
|
* undefined, otherwise `selected`.
|
|
2415
2282
|
*/
|
|
2416
|
-
function enumOptionsDeselectValue(valueIndex, selected, allEnumOptions) {
|
|
2417
|
-
|
|
2418
|
-
allEnumOptions = [];
|
|
2419
|
-
}
|
|
2420
|
-
var value = enumOptionsValueForIndex(valueIndex, allEnumOptions);
|
|
2283
|
+
function enumOptionsDeselectValue(valueIndex, selected, allEnumOptions = []) {
|
|
2284
|
+
const value = enumOptionsValueForIndex(valueIndex, allEnumOptions);
|
|
2421
2285
|
if (Array.isArray(selected)) {
|
|
2422
|
-
return selected.filter(
|
|
2423
|
-
return !isEqual__default["default"](v, value);
|
|
2424
|
-
});
|
|
2286
|
+
return selected.filter(v => !isEqual__default["default"](v, value));
|
|
2425
2287
|
}
|
|
2426
2288
|
return isEqual__default["default"](value, selected) ? undefined : selected;
|
|
2427
2289
|
}
|
|
@@ -2434,9 +2296,7 @@ function enumOptionsDeselectValue(valueIndex, selected, allEnumOptions) {
|
|
|
2434
2296
|
*/
|
|
2435
2297
|
function enumOptionsIsSelected(value, selected) {
|
|
2436
2298
|
if (Array.isArray(selected)) {
|
|
2437
|
-
return selected.some(
|
|
2438
|
-
return isEqual__default["default"](sel, value);
|
|
2439
|
-
});
|
|
2299
|
+
return selected.some(sel => isEqual__default["default"](sel, value));
|
|
2440
2300
|
}
|
|
2441
2301
|
return isEqual__default["default"](selected, value);
|
|
2442
2302
|
}
|
|
@@ -2452,18 +2312,8 @@ function enumOptionsIsSelected(value, selected) {
|
|
|
2452
2312
|
* @returns - A single string index for the first `value` in `allEnumOptions`, if not `multiple`. Otherwise, the list
|
|
2453
2313
|
* of indexes for (each of) the value(s) in `value`.
|
|
2454
2314
|
*/
|
|
2455
|
-
function enumOptionsIndexForValue(value, allEnumOptions, multiple) {
|
|
2456
|
-
|
|
2457
|
-
allEnumOptions = [];
|
|
2458
|
-
}
|
|
2459
|
-
if (multiple === void 0) {
|
|
2460
|
-
multiple = false;
|
|
2461
|
-
}
|
|
2462
|
-
var selectedIndexes = allEnumOptions.map(function (opt, index) {
|
|
2463
|
-
return enumOptionsIsSelected(opt.value, value) ? String(index) : undefined;
|
|
2464
|
-
}).filter(function (opt) {
|
|
2465
|
-
return typeof opt !== 'undefined';
|
|
2466
|
-
});
|
|
2315
|
+
function enumOptionsIndexForValue(value, allEnumOptions = [], multiple = false) {
|
|
2316
|
+
const selectedIndexes = allEnumOptions.map((opt, index) => enumOptionsIsSelected(opt.value, value) ? String(index) : undefined).filter(opt => typeof opt !== 'undefined');
|
|
2467
2317
|
if (!multiple) {
|
|
2468
2318
|
return selectedIndexes[0];
|
|
2469
2319
|
}
|
|
@@ -2478,25 +2328,17 @@ function enumOptionsIndexForValue(value, allEnumOptions, multiple) {
|
|
|
2478
2328
|
* @param [allEnumOptions=[]] - The list of all the known enumOptions
|
|
2479
2329
|
* @returns - The updated list of selected enum values with enum value at the `valueIndex` added to it
|
|
2480
2330
|
*/
|
|
2481
|
-
function enumOptionsSelectValue(valueIndex, selected, allEnumOptions) {
|
|
2482
|
-
|
|
2483
|
-
allEnumOptions = [];
|
|
2484
|
-
}
|
|
2485
|
-
var value = enumOptionsValueForIndex(valueIndex, allEnumOptions);
|
|
2331
|
+
function enumOptionsSelectValue(valueIndex, selected, allEnumOptions = []) {
|
|
2332
|
+
const value = enumOptionsValueForIndex(valueIndex, allEnumOptions);
|
|
2486
2333
|
if (!lodash.isNil(value)) {
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
return val;
|
|
2493
|
-
});
|
|
2494
|
-
var updated = selected.slice(0, index).concat(value, selected.slice(index));
|
|
2334
|
+
const index = allEnumOptions.findIndex(opt => value === opt.value);
|
|
2335
|
+
const all = allEnumOptions.map(({
|
|
2336
|
+
value: val
|
|
2337
|
+
}) => val);
|
|
2338
|
+
const updated = selected.slice(0, index).concat(value, selected.slice(index));
|
|
2495
2339
|
// As inserting values at predefined index positions doesn't work with empty
|
|
2496
2340
|
// arrays, we need to reorder the updated selection to match the initial order
|
|
2497
|
-
return updated.sort(
|
|
2498
|
-
return Number(all.indexOf(a) > all.indexOf(b));
|
|
2499
|
-
});
|
|
2341
|
+
return updated.sort((a, b) => Number(all.indexOf(a) > all.indexOf(b)));
|
|
2500
2342
|
}
|
|
2501
2343
|
return selected;
|
|
2502
2344
|
}
|
|
@@ -2506,12 +2348,12 @@ function enumOptionsSelectValue(valueIndex, selected, allEnumOptions) {
|
|
|
2506
2348
|
* schema by using either dotted path or an array of path names. Once you are done building the `ErrorSchema`, you can
|
|
2507
2349
|
* get the result and/or reset all the errors back to an initial set and start again.
|
|
2508
2350
|
*/
|
|
2509
|
-
|
|
2351
|
+
class ErrorSchemaBuilder {
|
|
2510
2352
|
/** Construct an `ErrorSchemaBuilder` with an optional initial set of errors in an `ErrorSchema`.
|
|
2511
2353
|
*
|
|
2512
2354
|
* @param [initialSchema] - The optional set of initial errors, that will be cloned into the class
|
|
2513
2355
|
*/
|
|
2514
|
-
|
|
2356
|
+
constructor(initialSchema) {
|
|
2515
2357
|
/** The error schema being built
|
|
2516
2358
|
*
|
|
2517
2359
|
* @private
|
|
@@ -2521,16 +2363,18 @@ var ErrorSchemaBuilder = /*#__PURE__*/function () {
|
|
|
2521
2363
|
}
|
|
2522
2364
|
/** Returns the `ErrorSchema` that has been updated by the methods of the `ErrorSchemaBuilder`
|
|
2523
2365
|
*/
|
|
2524
|
-
|
|
2366
|
+
get ErrorSchema() {
|
|
2367
|
+
return this.errorSchema;
|
|
2368
|
+
}
|
|
2525
2369
|
/** Will get an existing `ErrorSchema` at the specified `pathOfError` or create and return one.
|
|
2526
2370
|
*
|
|
2527
2371
|
* @param [pathOfError] - The optional path into the `ErrorSchema` at which to add the error(s)
|
|
2528
2372
|
* @returns - The error block for the given `pathOfError` or the root if not provided
|
|
2529
2373
|
* @private
|
|
2530
2374
|
*/
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2375
|
+
getOrCreateErrorBlock(pathOfError) {
|
|
2376
|
+
const hasPath = Array.isArray(pathOfError) && pathOfError.length > 0 || typeof pathOfError === 'string';
|
|
2377
|
+
let errorBlock = hasPath ? get__default["default"](this.errorSchema, pathOfError) : this.errorSchema;
|
|
2534
2378
|
if (!errorBlock && pathOfError) {
|
|
2535
2379
|
errorBlock = {};
|
|
2536
2380
|
set__default["default"](this.errorSchema, pathOfError, errorBlock);
|
|
@@ -2541,8 +2385,8 @@ var ErrorSchemaBuilder = /*#__PURE__*/function () {
|
|
|
2541
2385
|
*
|
|
2542
2386
|
* @param [initialSchema] - The optional set of initial errors, that will be cloned into the class
|
|
2543
2387
|
* @returns - The `ErrorSchemaBuilder` object for chaining purposes
|
|
2544
|
-
|
|
2545
|
-
|
|
2388
|
+
*/
|
|
2389
|
+
resetAllErrors(initialSchema) {
|
|
2546
2390
|
this.errorSchema = initialSchema ? cloneDeep__default["default"](initialSchema) : {};
|
|
2547
2391
|
return this;
|
|
2548
2392
|
}
|
|
@@ -2553,17 +2397,16 @@ var ErrorSchemaBuilder = /*#__PURE__*/function () {
|
|
|
2553
2397
|
* @param errorOrList - The error or list of errors to add into the `ErrorSchema`
|
|
2554
2398
|
* @param [pathOfError] - The optional path into the `ErrorSchema` at which to add the error(s)
|
|
2555
2399
|
* @returns - The `ErrorSchemaBuilder` object for chaining purposes
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2400
|
+
*/
|
|
2401
|
+
addErrors(errorOrList, pathOfError) {
|
|
2402
|
+
const errorBlock = this.getOrCreateErrorBlock(pathOfError);
|
|
2403
|
+
let errorsList = get__default["default"](errorBlock, ERRORS_KEY);
|
|
2560
2404
|
if (!Array.isArray(errorsList)) {
|
|
2561
2405
|
errorsList = [];
|
|
2562
2406
|
errorBlock[ERRORS_KEY] = errorsList;
|
|
2563
2407
|
}
|
|
2564
2408
|
if (Array.isArray(errorOrList)) {
|
|
2565
|
-
|
|
2566
|
-
(_errorsList = errorsList).push.apply(_errorsList, errorOrList);
|
|
2409
|
+
errorsList.push(...errorOrList);
|
|
2567
2410
|
} else {
|
|
2568
2411
|
errorsList.push(errorOrList);
|
|
2569
2412
|
}
|
|
@@ -2576,11 +2419,11 @@ var ErrorSchemaBuilder = /*#__PURE__*/function () {
|
|
|
2576
2419
|
* @param errorOrList - The error or list of errors to set into the `ErrorSchema`
|
|
2577
2420
|
* @param [pathOfError] - The optional path into the `ErrorSchema` at which to set the error(s)
|
|
2578
2421
|
* @returns - The `ErrorSchemaBuilder` object for chaining purposes
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2422
|
+
*/
|
|
2423
|
+
setErrors(errorOrList, pathOfError) {
|
|
2424
|
+
const errorBlock = this.getOrCreateErrorBlock(pathOfError);
|
|
2582
2425
|
// Effectively clone the array being given to prevent accidental outside manipulation of the given list
|
|
2583
|
-
|
|
2426
|
+
const listToAdd = Array.isArray(errorOrList) ? [...errorOrList] : [errorOrList];
|
|
2584
2427
|
set__default["default"](errorBlock, ERRORS_KEY, listToAdd);
|
|
2585
2428
|
return this;
|
|
2586
2429
|
}
|
|
@@ -2590,20 +2433,13 @@ var ErrorSchemaBuilder = /*#__PURE__*/function () {
|
|
|
2590
2433
|
*
|
|
2591
2434
|
* @param [pathOfError] - The optional path into the `ErrorSchema` at which to clear the error(s)
|
|
2592
2435
|
* @returns - The `ErrorSchemaBuilder` object for chaining purposes
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2436
|
+
*/
|
|
2437
|
+
clearErrors(pathOfError) {
|
|
2438
|
+
const errorBlock = this.getOrCreateErrorBlock(pathOfError);
|
|
2596
2439
|
set__default["default"](errorBlock, ERRORS_KEY, []);
|
|
2597
2440
|
return this;
|
|
2598
|
-
}
|
|
2599
|
-
|
|
2600
|
-
key: "ErrorSchema",
|
|
2601
|
-
get: function get() {
|
|
2602
|
-
return this.errorSchema;
|
|
2603
|
-
}
|
|
2604
|
-
}]);
|
|
2605
|
-
return ErrorSchemaBuilder;
|
|
2606
|
-
}();
|
|
2441
|
+
}
|
|
2442
|
+
}
|
|
2607
2443
|
|
|
2608
2444
|
/** Extracts the range spec information `{ step?: number, min?: number, max?: number }` that can be spread onto an HTML
|
|
2609
2445
|
* input from the range analog in the schema `{ multipleOf?: number, minimum?: number, maximum?: number }`.
|
|
@@ -2612,7 +2448,7 @@ var ErrorSchemaBuilder = /*#__PURE__*/function () {
|
|
|
2612
2448
|
* @returns - A range specification from the schema
|
|
2613
2449
|
*/
|
|
2614
2450
|
function rangeSpec(schema) {
|
|
2615
|
-
|
|
2451
|
+
const spec = {};
|
|
2616
2452
|
if (schema.multipleOf) {
|
|
2617
2453
|
spec.step = schema.multipleOf;
|
|
2618
2454
|
}
|
|
@@ -2633,16 +2469,11 @@ function rangeSpec(schema) {
|
|
|
2633
2469
|
* @param [autoDefaultStepAny=true] - Determines whether to auto-default step=any when the type is number and no step
|
|
2634
2470
|
* @returns - The extracted `InputPropsType` object
|
|
2635
2471
|
*/
|
|
2636
|
-
function getInputProps(schema, defaultType, options, autoDefaultStepAny) {
|
|
2637
|
-
|
|
2638
|
-
|
|
2639
|
-
|
|
2640
|
-
|
|
2641
|
-
autoDefaultStepAny = true;
|
|
2642
|
-
}
|
|
2643
|
-
var inputProps = _extends({
|
|
2644
|
-
type: defaultType || 'text'
|
|
2645
|
-
}, rangeSpec(schema));
|
|
2472
|
+
function getInputProps(schema, defaultType, options = {}, autoDefaultStepAny = true) {
|
|
2473
|
+
const inputProps = {
|
|
2474
|
+
type: defaultType || 'text',
|
|
2475
|
+
...rangeSpec(schema)
|
|
2476
|
+
};
|
|
2646
2477
|
// If options.inputType is set use that as the input type
|
|
2647
2478
|
if (options.inputType) {
|
|
2648
2479
|
inputProps.type = options.inputType;
|
|
@@ -2673,7 +2504,7 @@ function getInputProps(schema, defaultType, options, autoDefaultStepAny) {
|
|
|
2673
2504
|
|
|
2674
2505
|
/** The default submit button options, exported for testing purposes
|
|
2675
2506
|
*/
|
|
2676
|
-
|
|
2507
|
+
const DEFAULT_OPTIONS = {
|
|
2677
2508
|
props: {
|
|
2678
2509
|
disabled: false
|
|
2679
2510
|
},
|
|
@@ -2685,14 +2516,14 @@ var DEFAULT_OPTIONS = {
|
|
|
2685
2516
|
* @param [uiSchema={}] - the UI Schema from which to extract submit button props
|
|
2686
2517
|
* @returns - The merging of the `DEFAULT_OPTIONS` with any custom ones
|
|
2687
2518
|
*/
|
|
2688
|
-
function getSubmitButtonOptions(uiSchema) {
|
|
2689
|
-
|
|
2690
|
-
uiSchema = {};
|
|
2691
|
-
}
|
|
2692
|
-
var uiOptions = getUiOptions(uiSchema);
|
|
2519
|
+
function getSubmitButtonOptions(uiSchema = {}) {
|
|
2520
|
+
const uiOptions = getUiOptions(uiSchema);
|
|
2693
2521
|
if (uiOptions && uiOptions[SUBMIT_BTN_OPTIONS_KEY]) {
|
|
2694
|
-
|
|
2695
|
-
return
|
|
2522
|
+
const options = uiOptions[SUBMIT_BTN_OPTIONS_KEY];
|
|
2523
|
+
return {
|
|
2524
|
+
...DEFAULT_OPTIONS,
|
|
2525
|
+
...options
|
|
2526
|
+
};
|
|
2696
2527
|
}
|
|
2697
2528
|
return DEFAULT_OPTIONS;
|
|
2698
2529
|
}
|
|
@@ -2705,11 +2536,10 @@ function getSubmitButtonOptions(uiSchema) {
|
|
|
2705
2536
|
* @param [uiOptions={}] - The `UIOptionsType` from which to read an alternate template
|
|
2706
2537
|
* @returns - The template from either the `uiSchema` or `registry` for the `name`
|
|
2707
2538
|
*/
|
|
2708
|
-
function getTemplate(name, registry, uiOptions) {
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
}
|
|
2712
|
-
var templates = registry.templates;
|
|
2539
|
+
function getTemplate(name, registry, uiOptions = {}) {
|
|
2540
|
+
const {
|
|
2541
|
+
templates
|
|
2542
|
+
} = registry;
|
|
2713
2543
|
if (name === 'ButtonTemplates') {
|
|
2714
2544
|
return templates[name];
|
|
2715
2545
|
}
|
|
@@ -2720,11 +2550,10 @@ function getTemplate(name, registry, uiOptions) {
|
|
|
2720
2550
|
);
|
|
2721
2551
|
}
|
|
2722
2552
|
|
|
2723
|
-
var _excluded = ["options"];
|
|
2724
2553
|
/** The map of schema types to widget type to widget name
|
|
2725
2554
|
*/
|
|
2726
|
-
|
|
2727
|
-
|
|
2555
|
+
const widgetMap = {
|
|
2556
|
+
boolean: {
|
|
2728
2557
|
checkbox: 'CheckboxWidget',
|
|
2729
2558
|
radio: 'RadioWidget',
|
|
2730
2559
|
select: 'SelectWidget',
|
|
@@ -2783,16 +2612,21 @@ var widgetMap = {
|
|
|
2783
2612
|
* @returns - The wrapper widget
|
|
2784
2613
|
*/
|
|
2785
2614
|
function mergeWidgetOptions(AWidget) {
|
|
2786
|
-
|
|
2615
|
+
let MergedWidget = get__default["default"](AWidget, 'MergedWidget');
|
|
2787
2616
|
// cache return value as property of widget for proper react reconciliation
|
|
2788
2617
|
if (!MergedWidget) {
|
|
2789
|
-
|
|
2790
|
-
MergedWidget =
|
|
2791
|
-
|
|
2792
|
-
|
|
2793
|
-
|
|
2794
|
-
|
|
2795
|
-
|
|
2618
|
+
const defaultOptions = AWidget.defaultProps && AWidget.defaultProps.options || {};
|
|
2619
|
+
MergedWidget = ({
|
|
2620
|
+
options,
|
|
2621
|
+
...props
|
|
2622
|
+
}) => {
|
|
2623
|
+
return jsxRuntime.jsx(AWidget, {
|
|
2624
|
+
options: {
|
|
2625
|
+
...defaultOptions,
|
|
2626
|
+
...options
|
|
2627
|
+
},
|
|
2628
|
+
...props
|
|
2629
|
+
});
|
|
2796
2630
|
};
|
|
2797
2631
|
set__default["default"](AWidget, 'MergedWidget', MergedWidget);
|
|
2798
2632
|
}
|
|
@@ -2809,31 +2643,28 @@ function mergeWidgetOptions(AWidget) {
|
|
|
2809
2643
|
* @returns - The `Widget` component to use
|
|
2810
2644
|
* @throws - An error if there is no `Widget` component that can be returned
|
|
2811
2645
|
*/
|
|
2812
|
-
function getWidget(schema, widget, registeredWidgets) {
|
|
2813
|
-
|
|
2814
|
-
registeredWidgets = {};
|
|
2815
|
-
}
|
|
2816
|
-
var type = getSchemaType(schema);
|
|
2646
|
+
function getWidget(schema, widget, registeredWidgets = {}) {
|
|
2647
|
+
const type = getSchemaType(schema);
|
|
2817
2648
|
if (typeof widget === 'function' || widget && ReactIs__default["default"].isForwardRef( /*#__PURE__*/react.createElement(widget)) || ReactIs__default["default"].isMemo(widget)) {
|
|
2818
2649
|
return mergeWidgetOptions(widget);
|
|
2819
2650
|
}
|
|
2820
2651
|
if (typeof widget !== 'string') {
|
|
2821
|
-
throw new Error(
|
|
2652
|
+
throw new Error(`Unsupported widget definition: ${typeof widget}`);
|
|
2822
2653
|
}
|
|
2823
2654
|
if (widget in registeredWidgets) {
|
|
2824
|
-
|
|
2655
|
+
const registeredWidget = registeredWidgets[widget];
|
|
2825
2656
|
return getWidget(schema, registeredWidget, registeredWidgets);
|
|
2826
2657
|
}
|
|
2827
2658
|
if (typeof type === 'string') {
|
|
2828
2659
|
if (!(type in widgetMap)) {
|
|
2829
|
-
throw new Error(
|
|
2660
|
+
throw new Error(`No widget for type '${type}'`);
|
|
2830
2661
|
}
|
|
2831
2662
|
if (widget in widgetMap[type]) {
|
|
2832
|
-
|
|
2833
|
-
return getWidget(schema,
|
|
2663
|
+
const registeredWidget = registeredWidgets[widgetMap[type][widget]];
|
|
2664
|
+
return getWidget(schema, registeredWidget, registeredWidgets);
|
|
2834
2665
|
}
|
|
2835
2666
|
}
|
|
2836
|
-
throw new Error(
|
|
2667
|
+
throw new Error(`No widget '${widget}' for type '${type}'`);
|
|
2837
2668
|
}
|
|
2838
2669
|
|
|
2839
2670
|
/** JS has no built-in hashing function, so rolling our own
|
|
@@ -2844,9 +2675,9 @@ function getWidget(schema, widget, registeredWidgets) {
|
|
|
2844
2675
|
* @returns - The resulting hash of the string in hex format
|
|
2845
2676
|
*/
|
|
2846
2677
|
function hashString(string) {
|
|
2847
|
-
|
|
2848
|
-
for (
|
|
2849
|
-
|
|
2678
|
+
let hash = 0;
|
|
2679
|
+
for (let i = 0; i < string.length; i += 1) {
|
|
2680
|
+
const chr = string.charCodeAt(i);
|
|
2850
2681
|
hash = (hash << 5) - hash + chr;
|
|
2851
2682
|
hash = hash & hash; // Convert to 32bit integer
|
|
2852
2683
|
}
|
|
@@ -2860,11 +2691,9 @@ function hashString(string) {
|
|
|
2860
2691
|
* @returns - The string obtained from the hash of the stringified schema
|
|
2861
2692
|
*/
|
|
2862
2693
|
function hashForSchema(schema) {
|
|
2863
|
-
|
|
2694
|
+
const allKeys = new Set();
|
|
2864
2695
|
// solution source: https://stackoverflow.com/questions/16167581/sort-object-properties-and-json-stringify/53593328#53593328
|
|
2865
|
-
JSON.stringify(schema,
|
|
2866
|
-
return allKeys.add(key), value;
|
|
2867
|
-
});
|
|
2696
|
+
JSON.stringify(schema, (key, value) => (allKeys.add(key), value));
|
|
2868
2697
|
return hashString(JSON.stringify(schema, Array.from(allKeys).sort()));
|
|
2869
2698
|
}
|
|
2870
2699
|
|
|
@@ -2876,15 +2705,12 @@ function hashForSchema(schema) {
|
|
|
2876
2705
|
* @param [registeredWidgets={}] - A registry of widget name to `Widget` implementation
|
|
2877
2706
|
* @returns - True if the widget exists, false otherwise
|
|
2878
2707
|
*/
|
|
2879
|
-
function hasWidget(schema, widget, registeredWidgets) {
|
|
2880
|
-
if (registeredWidgets === void 0) {
|
|
2881
|
-
registeredWidgets = {};
|
|
2882
|
-
}
|
|
2708
|
+
function hasWidget(schema, widget, registeredWidgets = {}) {
|
|
2883
2709
|
try {
|
|
2884
2710
|
getWidget(schema, widget, registeredWidgets);
|
|
2885
2711
|
return true;
|
|
2886
2712
|
} catch (e) {
|
|
2887
|
-
|
|
2713
|
+
const err = e;
|
|
2888
2714
|
if (err.message && (err.message.startsWith('No widget') || err.message.startsWith('Unsupported widget'))) {
|
|
2889
2715
|
return false;
|
|
2890
2716
|
}
|
|
@@ -2898,8 +2724,8 @@ function hasWidget(schema, widget, registeredWidgets) {
|
|
|
2898
2724
|
* @param suffix - The suffix to append to the id
|
|
2899
2725
|
*/
|
|
2900
2726
|
function idGenerator(id, suffix) {
|
|
2901
|
-
|
|
2902
|
-
return theId
|
|
2727
|
+
const theId = isString__default["default"](id) ? id : id[ID_KEY];
|
|
2728
|
+
return `${theId}__${suffix}`;
|
|
2903
2729
|
}
|
|
2904
2730
|
/** Return a consistent `id` for the field description element
|
|
2905
2731
|
*
|
|
@@ -2949,12 +2775,9 @@ function titleId(id) {
|
|
|
2949
2775
|
* @param [includeExamples=false] - Optional flag, if true, will add the `examplesId` into the list
|
|
2950
2776
|
* @returns - The string containing the list of ids for use in an `aria-describedBy` attribute
|
|
2951
2777
|
*/
|
|
2952
|
-
function ariaDescribedByIds(id, includeExamples) {
|
|
2953
|
-
|
|
2954
|
-
|
|
2955
|
-
}
|
|
2956
|
-
var examples = includeExamples ? " " + examplesId(id) : '';
|
|
2957
|
-
return errorId(id) + " " + descriptionId(id) + " " + helpId(id) + examples;
|
|
2778
|
+
function ariaDescribedByIds(id, includeExamples = false) {
|
|
2779
|
+
const examples = includeExamples ? ` ${examplesId(id)}` : '';
|
|
2780
|
+
return `${errorId(id)} ${descriptionId(id)} ${helpId(id)}${examples}`;
|
|
2958
2781
|
}
|
|
2959
2782
|
/** Return a consistent `id` for the `optionIndex`s of a `Radio` or `Checkboxes` widget
|
|
2960
2783
|
*
|
|
@@ -2963,7 +2786,7 @@ function ariaDescribedByIds(id, includeExamples) {
|
|
|
2963
2786
|
* @returns - An id for the option index based on the parent `id`
|
|
2964
2787
|
*/
|
|
2965
2788
|
function optionId(id, optionIndex) {
|
|
2966
|
-
return id
|
|
2789
|
+
return `${id}-${optionIndex}`;
|
|
2967
2790
|
}
|
|
2968
2791
|
|
|
2969
2792
|
function labelValue(label, hideLabel, fallback) {
|
|
@@ -2987,11 +2810,11 @@ function localToUTC(dateString) {
|
|
|
2987
2810
|
* @throws - Error when the schema does not have a constant value
|
|
2988
2811
|
*/
|
|
2989
2812
|
function toConstant(schema) {
|
|
2990
|
-
if (ENUM_KEY in schema && Array.isArray(schema
|
|
2991
|
-
return schema
|
|
2813
|
+
if (ENUM_KEY in schema && Array.isArray(schema.enum) && schema.enum.length === 1) {
|
|
2814
|
+
return schema.enum[0];
|
|
2992
2815
|
}
|
|
2993
2816
|
if (CONST_KEY in schema) {
|
|
2994
|
-
return schema
|
|
2817
|
+
return schema.const;
|
|
2995
2818
|
}
|
|
2996
2819
|
throw new Error('schema cannot be inferred as a constant');
|
|
2997
2820
|
}
|
|
@@ -3007,28 +2830,28 @@ function toConstant(schema) {
|
|
|
3007
2830
|
function optionsList(schema) {
|
|
3008
2831
|
// enumNames was deprecated in v5 and is intentionally omitted from the RJSFSchema type.
|
|
3009
2832
|
// Cast the type to include enumNames so the feature still works.
|
|
3010
|
-
|
|
2833
|
+
const schemaWithEnumNames = schema;
|
|
3011
2834
|
if (schemaWithEnumNames.enumNames && "development" !== 'production') {
|
|
3012
2835
|
console.warn('The enumNames property is deprecated and may be removed in a future major release.');
|
|
3013
2836
|
}
|
|
3014
|
-
if (schema
|
|
3015
|
-
return schema
|
|
3016
|
-
|
|
2837
|
+
if (schema.enum) {
|
|
2838
|
+
return schema.enum.map((value, i) => {
|
|
2839
|
+
const label = schemaWithEnumNames.enumNames && schemaWithEnumNames.enumNames[i] || String(value);
|
|
3017
2840
|
return {
|
|
3018
|
-
label
|
|
3019
|
-
value
|
|
2841
|
+
label,
|
|
2842
|
+
value
|
|
3020
2843
|
};
|
|
3021
2844
|
});
|
|
3022
2845
|
}
|
|
3023
|
-
|
|
3024
|
-
return altSchemas && altSchemas.map(
|
|
3025
|
-
|
|
3026
|
-
|
|
3027
|
-
|
|
2846
|
+
const altSchemas = schema.oneOf || schema.anyOf;
|
|
2847
|
+
return altSchemas && altSchemas.map(aSchemaDef => {
|
|
2848
|
+
const aSchema = aSchemaDef;
|
|
2849
|
+
const value = toConstant(aSchema);
|
|
2850
|
+
const label = aSchema.title || String(value);
|
|
3028
2851
|
return {
|
|
3029
2852
|
schema: aSchema,
|
|
3030
|
-
label
|
|
3031
|
-
value
|
|
2853
|
+
label,
|
|
2854
|
+
value
|
|
3032
2855
|
};
|
|
3033
2856
|
});
|
|
3034
2857
|
}
|
|
@@ -3047,35 +2870,27 @@ function orderProperties(properties, order) {
|
|
|
3047
2870
|
if (!Array.isArray(order)) {
|
|
3048
2871
|
return properties;
|
|
3049
2872
|
}
|
|
3050
|
-
|
|
3051
|
-
|
|
3052
|
-
|
|
3053
|
-
|
|
3054
|
-
|
|
3055
|
-
|
|
3056
|
-
|
|
3057
|
-
|
|
3058
|
-
|
|
3059
|
-
|
|
3060
|
-
var orderFiltered = order.filter(function (prop) {
|
|
3061
|
-
return prop === '*' || propertyHash[prop];
|
|
3062
|
-
});
|
|
3063
|
-
var orderHash = arrayToHash(orderFiltered);
|
|
3064
|
-
var rest = properties.filter(function (prop) {
|
|
3065
|
-
return !orderHash[prop];
|
|
3066
|
-
});
|
|
3067
|
-
var restIndex = orderFiltered.indexOf('*');
|
|
2873
|
+
const arrayToHash = arr => arr.reduce((prev, curr) => {
|
|
2874
|
+
prev[curr] = true;
|
|
2875
|
+
return prev;
|
|
2876
|
+
}, {});
|
|
2877
|
+
const errorPropList = arr => arr.length > 1 ? `properties '${arr.join("', '")}'` : `property '${arr[0]}'`;
|
|
2878
|
+
const propertyHash = arrayToHash(properties);
|
|
2879
|
+
const orderFiltered = order.filter(prop => prop === '*' || propertyHash[prop]);
|
|
2880
|
+
const orderHash = arrayToHash(orderFiltered);
|
|
2881
|
+
const rest = properties.filter(prop => !orderHash[prop]);
|
|
2882
|
+
const restIndex = orderFiltered.indexOf('*');
|
|
3068
2883
|
if (restIndex === -1) {
|
|
3069
2884
|
if (rest.length) {
|
|
3070
|
-
throw new Error(
|
|
2885
|
+
throw new Error(`uiSchema order list does not contain ${errorPropList(rest)}`);
|
|
3071
2886
|
}
|
|
3072
2887
|
return orderFiltered;
|
|
3073
2888
|
}
|
|
3074
2889
|
if (restIndex !== orderFiltered.lastIndexOf('*')) {
|
|
3075
2890
|
throw new Error('uiSchema order list contains more than one wildcard item');
|
|
3076
2891
|
}
|
|
3077
|
-
|
|
3078
|
-
complete.splice
|
|
2892
|
+
const complete = [...orderFiltered];
|
|
2893
|
+
complete.splice(restIndex, 1, ...rest);
|
|
3079
2894
|
return complete;
|
|
3080
2895
|
}
|
|
3081
2896
|
|
|
@@ -3086,7 +2901,7 @@ function orderProperties(properties, order) {
|
|
|
3086
2901
|
* @returns - The number converted to a string with leading zero padding if the number of digits is less than `width`
|
|
3087
2902
|
*/
|
|
3088
2903
|
function pad(num, width) {
|
|
3089
|
-
|
|
2904
|
+
let s = String(num);
|
|
3090
2905
|
while (s.length < width) {
|
|
3091
2906
|
s = '0' + s;
|
|
3092
2907
|
}
|
|
@@ -3100,10 +2915,7 @@ function pad(num, width) {
|
|
|
3100
2915
|
* @returns - The date string converted to a `DateObject`
|
|
3101
2916
|
* @throws - Error when the date cannot be parsed from the string
|
|
3102
2917
|
*/
|
|
3103
|
-
function parseDateString(dateString, includeTime) {
|
|
3104
|
-
if (includeTime === void 0) {
|
|
3105
|
-
includeTime = true;
|
|
3106
|
-
}
|
|
2918
|
+
function parseDateString(dateString, includeTime = true) {
|
|
3107
2919
|
if (!dateString) {
|
|
3108
2920
|
return {
|
|
3109
2921
|
year: -1,
|
|
@@ -3114,7 +2926,7 @@ function parseDateString(dateString, includeTime) {
|
|
|
3114
2926
|
second: includeTime ? -1 : 0
|
|
3115
2927
|
};
|
|
3116
2928
|
}
|
|
3117
|
-
|
|
2929
|
+
const date = new Date(dateString);
|
|
3118
2930
|
if (Number.isNaN(date.getTime())) {
|
|
3119
2931
|
throw new Error('Unable to parse date ' + dateString);
|
|
3120
2932
|
}
|
|
@@ -3139,11 +2951,11 @@ function parseDateString(dateString, includeTime) {
|
|
|
3139
2951
|
*/
|
|
3140
2952
|
function schemaRequiresTrueValue(schema) {
|
|
3141
2953
|
// Check if const is a truthy value
|
|
3142
|
-
if (schema
|
|
2954
|
+
if (schema.const) {
|
|
3143
2955
|
return true;
|
|
3144
2956
|
}
|
|
3145
2957
|
// Check if an enum has a single value of true
|
|
3146
|
-
if (schema
|
|
2958
|
+
if (schema.enum && schema.enum.length === 1 && schema.enum[0] === true) {
|
|
3147
2959
|
return true;
|
|
3148
2960
|
}
|
|
3149
2961
|
// If anyOf has a single value, evaluate the subschema
|
|
@@ -3156,9 +2968,7 @@ function schemaRequiresTrueValue(schema) {
|
|
|
3156
2968
|
}
|
|
3157
2969
|
// Evaluate each subschema in allOf, to see if one of them requires a true value
|
|
3158
2970
|
if (schema.allOf) {
|
|
3159
|
-
|
|
3160
|
-
return schemaRequiresTrueValue(subSchema);
|
|
3161
|
-
};
|
|
2971
|
+
const schemaSome = subSchema => schemaRequiresTrueValue(subSchema);
|
|
3162
2972
|
return schema.allOf.some(schemaSome);
|
|
3163
2973
|
}
|
|
3164
2974
|
return false;
|
|
@@ -3173,8 +2983,10 @@ function schemaRequiresTrueValue(schema) {
|
|
|
3173
2983
|
* @returns - True if the component should be re-rendered, false otherwise
|
|
3174
2984
|
*/
|
|
3175
2985
|
function shouldRender(component, nextProps, nextState) {
|
|
3176
|
-
|
|
3177
|
-
|
|
2986
|
+
const {
|
|
2987
|
+
props,
|
|
2988
|
+
state
|
|
2989
|
+
} = component;
|
|
3178
2990
|
return !deepEquals(props, nextProps) || !deepEquals(state, nextState);
|
|
3179
2991
|
}
|
|
3180
2992
|
|
|
@@ -3185,21 +2997,17 @@ function shouldRender(component, nextProps, nextState) {
|
|
|
3185
2997
|
* @param [time=true] - Optional flag used to remove the time portion of the date string if false
|
|
3186
2998
|
* @returns - The UTC date string
|
|
3187
2999
|
*/
|
|
3188
|
-
function toDateString(dateObject, time) {
|
|
3189
|
-
|
|
3190
|
-
|
|
3191
|
-
|
|
3192
|
-
|
|
3193
|
-
|
|
3194
|
-
|
|
3195
|
-
|
|
3196
|
-
|
|
3197
|
-
|
|
3198
|
-
|
|
3199
|
-
_dateObject$second = dateObject.second,
|
|
3200
|
-
second = _dateObject$second === void 0 ? 0 : _dateObject$second;
|
|
3201
|
-
var utcTime = Date.UTC(year, month - 1, day, hour, minute, second);
|
|
3202
|
-
var datetime = new Date(utcTime).toJSON();
|
|
3000
|
+
function toDateString(dateObject, time = true) {
|
|
3001
|
+
const {
|
|
3002
|
+
year,
|
|
3003
|
+
month,
|
|
3004
|
+
day,
|
|
3005
|
+
hour = 0,
|
|
3006
|
+
minute = 0,
|
|
3007
|
+
second = 0
|
|
3008
|
+
} = dateObject;
|
|
3009
|
+
const utcTime = Date.UTC(year, month - 1, day, hour, minute, second);
|
|
3010
|
+
const datetime = new Date(utcTime).toJSON();
|
|
3203
3011
|
return time ? datetime : datetime.slice(0, 10);
|
|
3204
3012
|
}
|
|
3205
3013
|
|
|
@@ -3209,29 +3017,26 @@ function toDateString(dateObject, time) {
|
|
|
3209
3017
|
* @param [fieldPath=[]] - The current field path, defaults to [] if not specified
|
|
3210
3018
|
* @returns - The list of `RJSFValidationErrors` extracted from the `errorSchema`
|
|
3211
3019
|
*/
|
|
3212
|
-
function toErrorList(errorSchema, fieldPath) {
|
|
3213
|
-
if (fieldPath === void 0) {
|
|
3214
|
-
fieldPath = [];
|
|
3215
|
-
}
|
|
3020
|
+
function toErrorList(errorSchema, fieldPath = []) {
|
|
3216
3021
|
if (!errorSchema) {
|
|
3217
3022
|
return [];
|
|
3218
3023
|
}
|
|
3219
|
-
|
|
3024
|
+
let errorList = [];
|
|
3220
3025
|
if (ERRORS_KEY in errorSchema) {
|
|
3221
|
-
errorList = errorList.concat(errorSchema[ERRORS_KEY].map(
|
|
3222
|
-
|
|
3026
|
+
errorList = errorList.concat(errorSchema[ERRORS_KEY].map(message => {
|
|
3027
|
+
const property = `.${fieldPath.join('.')}`;
|
|
3223
3028
|
return {
|
|
3224
|
-
property
|
|
3225
|
-
message
|
|
3226
|
-
stack: property
|
|
3029
|
+
property,
|
|
3030
|
+
message,
|
|
3031
|
+
stack: `${property} ${message}`
|
|
3227
3032
|
};
|
|
3228
3033
|
}));
|
|
3229
3034
|
}
|
|
3230
|
-
return Object.keys(errorSchema).reduce(
|
|
3035
|
+
return Object.keys(errorSchema).reduce((acc, key) => {
|
|
3231
3036
|
if (key !== ERRORS_KEY) {
|
|
3232
|
-
|
|
3037
|
+
const childSchema = errorSchema[key];
|
|
3233
3038
|
if (isPlainObject__default["default"](childSchema)) {
|
|
3234
|
-
acc = acc.concat(toErrorList(childSchema, [
|
|
3039
|
+
acc = acc.concat(toErrorList(childSchema, [...fieldPath, key]));
|
|
3235
3040
|
}
|
|
3236
3041
|
}
|
|
3237
3042
|
return acc;
|
|
@@ -3258,13 +3063,15 @@ function toErrorList(errorSchema, fieldPath) {
|
|
|
3258
3063
|
* @returns - The `ErrorSchema` built from the list of `RJSFValidationErrors`
|
|
3259
3064
|
*/
|
|
3260
3065
|
function toErrorSchema(errors) {
|
|
3261
|
-
|
|
3066
|
+
const builder = new ErrorSchemaBuilder();
|
|
3262
3067
|
if (errors.length) {
|
|
3263
|
-
errors.forEach(
|
|
3264
|
-
|
|
3265
|
-
|
|
3068
|
+
errors.forEach(error => {
|
|
3069
|
+
const {
|
|
3070
|
+
property,
|
|
3071
|
+
message
|
|
3072
|
+
} = error;
|
|
3266
3073
|
// When the property is the root element, just use an empty array for the path
|
|
3267
|
-
|
|
3074
|
+
const path = property === '.' ? [] : toPath__default["default"](property);
|
|
3268
3075
|
// If the property is at the root (.level1) then toPath creates
|
|
3269
3076
|
// an empty array element at the first index. Remove it.
|
|
3270
3077
|
if (path.length > 0 && path[0] === '') {
|
|
@@ -3284,17 +3091,21 @@ function toErrorSchema(errors) {
|
|
|
3284
3091
|
* @returns - The `ErrorSchema` resulting from the stripping of the `addError()` function
|
|
3285
3092
|
*/
|
|
3286
3093
|
function unwrapErrorHandler(errorHandler) {
|
|
3287
|
-
return Object.keys(errorHandler).reduce(
|
|
3094
|
+
return Object.keys(errorHandler).reduce((acc, key) => {
|
|
3288
3095
|
if (key === 'addError') {
|
|
3289
3096
|
return acc;
|
|
3290
3097
|
} else {
|
|
3291
|
-
|
|
3292
|
-
var childSchema = errorHandler[key];
|
|
3098
|
+
const childSchema = errorHandler[key];
|
|
3293
3099
|
if (isPlainObject__default["default"](childSchema)) {
|
|
3294
|
-
|
|
3295
|
-
|
|
3100
|
+
return {
|
|
3101
|
+
...acc,
|
|
3102
|
+
[key]: unwrapErrorHandler(childSchema)
|
|
3103
|
+
};
|
|
3296
3104
|
}
|
|
3297
|
-
return
|
|
3105
|
+
return {
|
|
3106
|
+
...acc,
|
|
3107
|
+
[key]: childSchema
|
|
3108
|
+
};
|
|
3298
3109
|
}
|
|
3299
3110
|
}, {});
|
|
3300
3111
|
}
|
|
@@ -3313,15 +3124,15 @@ function utcToLocal(jsonDate) {
|
|
|
3313
3124
|
// > should be a _valid local date and time string_ (not GMT)
|
|
3314
3125
|
// Note - date constructor passed local ISO-8601 does not correctly
|
|
3315
3126
|
// change time to UTC in node pre-8
|
|
3316
|
-
|
|
3317
|
-
|
|
3318
|
-
|
|
3319
|
-
|
|
3320
|
-
|
|
3321
|
-
|
|
3322
|
-
|
|
3323
|
-
|
|
3324
|
-
return yyyy
|
|
3127
|
+
const date = new Date(jsonDate);
|
|
3128
|
+
const yyyy = pad(date.getFullYear(), 4);
|
|
3129
|
+
const MM = pad(date.getMonth() + 1, 2);
|
|
3130
|
+
const dd = pad(date.getDate(), 2);
|
|
3131
|
+
const hh = pad(date.getHours(), 2);
|
|
3132
|
+
const mm = pad(date.getMinutes(), 2);
|
|
3133
|
+
const ss = pad(date.getSeconds(), 2);
|
|
3134
|
+
const SSS = pad(date.getMilliseconds(), 3);
|
|
3135
|
+
return `${yyyy}-${MM}-${dd}T${hh}:${mm}:${ss}.${SSS}`;
|
|
3325
3136
|
}
|
|
3326
3137
|
|
|
3327
3138
|
/** Merges the errors in `additionalErrorSchema` into the existing `validationData` by combining the hierarchies in the
|
|
@@ -3337,17 +3148,19 @@ function validationDataMerge(validationData, additionalErrorSchema) {
|
|
|
3337
3148
|
if (!additionalErrorSchema) {
|
|
3338
3149
|
return validationData;
|
|
3339
3150
|
}
|
|
3340
|
-
|
|
3341
|
-
|
|
3342
|
-
|
|
3343
|
-
|
|
3151
|
+
const {
|
|
3152
|
+
errors: oldErrors,
|
|
3153
|
+
errorSchema: oldErrorSchema
|
|
3154
|
+
} = validationData;
|
|
3155
|
+
let errors = toErrorList(additionalErrorSchema);
|
|
3156
|
+
let errorSchema = additionalErrorSchema;
|
|
3344
3157
|
if (!isEmpty__default["default"](oldErrorSchema)) {
|
|
3345
3158
|
errorSchema = mergeObjects(oldErrorSchema, additionalErrorSchema, true);
|
|
3346
|
-
errors = [].concat(
|
|
3159
|
+
errors = [...oldErrors].concat(errors);
|
|
3347
3160
|
}
|
|
3348
3161
|
return {
|
|
3349
|
-
errorSchema
|
|
3350
|
-
errors
|
|
3162
|
+
errorSchema,
|
|
3163
|
+
errors
|
|
3351
3164
|
};
|
|
3352
3165
|
}
|
|
3353
3166
|
|
|
@@ -3357,9 +3170,9 @@ function validationDataMerge(validationData, additionalErrorSchema) {
|
|
|
3357
3170
|
* @param node - The object node to which a ROOT_SCHEMA_PREFIX is added when a REF_KEY is part of it
|
|
3358
3171
|
*/
|
|
3359
3172
|
function withIdRefPrefixObject(node) {
|
|
3360
|
-
for (
|
|
3361
|
-
|
|
3362
|
-
|
|
3173
|
+
for (const key in node) {
|
|
3174
|
+
const realObj = node;
|
|
3175
|
+
const value = realObj[key];
|
|
3363
3176
|
if (key === REF_KEY && typeof value === 'string' && value.startsWith('#')) {
|
|
3364
3177
|
realObj[key] = ROOT_SCHEMA_PREFIX + value;
|
|
3365
3178
|
} else {
|
|
@@ -3374,7 +3187,7 @@ function withIdRefPrefixObject(node) {
|
|
|
3374
3187
|
* @param node - The list of object nodes to which a ROOT_SCHEMA_PREFIX is added when a REF_KEY is part of it
|
|
3375
3188
|
*/
|
|
3376
3189
|
function withIdRefPrefixArray(node) {
|
|
3377
|
-
for (
|
|
3190
|
+
for (let i = 0; i < node.length; i++) {
|
|
3378
3191
|
node[i] = withIdRefPrefix(node[i]);
|
|
3379
3192
|
}
|
|
3380
3193
|
return node;
|
|
@@ -3386,11 +3199,13 @@ function withIdRefPrefixArray(node) {
|
|
|
3386
3199
|
* @returns - A copy of the `schemaNode` with updated `$ref`s
|
|
3387
3200
|
*/
|
|
3388
3201
|
function withIdRefPrefix(schemaNode) {
|
|
3389
|
-
if (schemaNode.constructor === Object) {
|
|
3390
|
-
return withIdRefPrefixObject(_extends({}, schemaNode));
|
|
3391
|
-
}
|
|
3392
3202
|
if (Array.isArray(schemaNode)) {
|
|
3393
|
-
return withIdRefPrefixArray([]
|
|
3203
|
+
return withIdRefPrefixArray([...schemaNode]);
|
|
3204
|
+
}
|
|
3205
|
+
if (isObject__default["default"](schemaNode)) {
|
|
3206
|
+
return withIdRefPrefixObject({
|
|
3207
|
+
...schemaNode
|
|
3208
|
+
});
|
|
3394
3209
|
}
|
|
3395
3210
|
return schemaNode;
|
|
3396
3211
|
}
|
|
@@ -3478,13 +3293,13 @@ exports.TranslatableString = void 0;
|
|
|
3478
3293
|
* the hashed value of the schema. NOTE: After hashing the schema, an $id with the hash value is added to the
|
|
3479
3294
|
* schema IF that schema doesn't already have an $id, prior to putting the schema into the map.
|
|
3480
3295
|
*/
|
|
3481
|
-
|
|
3296
|
+
class ParserValidator {
|
|
3482
3297
|
/** Construct the ParserValidator for the given `rootSchema`. This `rootSchema` will be stashed in the `schemaMap`
|
|
3483
3298
|
* first.
|
|
3484
3299
|
*
|
|
3485
3300
|
* @param rootSchema - The root schema against which this validator will be executed
|
|
3486
3301
|
*/
|
|
3487
|
-
|
|
3302
|
+
constructor(rootSchema) {
|
|
3488
3303
|
/** The rootSchema provided during construction of the class */
|
|
3489
3304
|
this.rootSchema = void 0;
|
|
3490
3305
|
/** The map of schemas encountered by the ParserValidator */
|
|
@@ -3499,23 +3314,24 @@ var ParserValidator = /*#__PURE__*/function () {
|
|
|
3499
3314
|
* @param schema - The schema which is to be added to the map
|
|
3500
3315
|
* @param hash - The hash value at which to map the schema
|
|
3501
3316
|
*/
|
|
3502
|
-
|
|
3503
|
-
|
|
3504
|
-
|
|
3505
|
-
|
|
3506
|
-
|
|
3507
|
-
|
|
3317
|
+
addSchema(schema, hash) {
|
|
3318
|
+
const key = get__default["default"](schema, ID_KEY, hash);
|
|
3319
|
+
const identifiedSchema = {
|
|
3320
|
+
...schema,
|
|
3321
|
+
[ID_KEY]: key
|
|
3322
|
+
};
|
|
3323
|
+
const existing = this.schemaMap[key];
|
|
3508
3324
|
if (!existing) {
|
|
3509
3325
|
this.schemaMap[key] = identifiedSchema;
|
|
3510
3326
|
} else if (!isEqual__default["default"](existing, identifiedSchema)) {
|
|
3511
3327
|
console.error('existing schema:', JSON.stringify(existing, null, 2));
|
|
3512
3328
|
console.error('new schema:', JSON.stringify(identifiedSchema, null, 2));
|
|
3513
|
-
throw new Error(
|
|
3329
|
+
throw new Error(`Two different schemas exist with the same key ${key}! What a bad coincidence. If possible, try adding an $id to one of the schemas`);
|
|
3514
3330
|
}
|
|
3515
3331
|
}
|
|
3516
3332
|
/** Returns the current `schemaMap` to the caller
|
|
3517
|
-
|
|
3518
|
-
|
|
3333
|
+
*/
|
|
3334
|
+
getSchemaMap() {
|
|
3519
3335
|
return this.schemaMap;
|
|
3520
3336
|
}
|
|
3521
3337
|
/** Implements the `ValidatorType` `isValid()` method to capture the `schema` in the `schemaMap`. Throws an error when
|
|
@@ -3525,8 +3341,8 @@ var ParserValidator = /*#__PURE__*/function () {
|
|
|
3525
3341
|
* @param _formData - The formData parameter that is ignored
|
|
3526
3342
|
* @param rootSchema - The root schema associated with the schema
|
|
3527
3343
|
* @throws - Error when the given `rootSchema` differs from the root schema provided during construction
|
|
3528
|
-
|
|
3529
|
-
|
|
3344
|
+
*/
|
|
3345
|
+
isValid(schema, _formData, rootSchema) {
|
|
3530
3346
|
if (!isEqual__default["default"](rootSchema, this.rootSchema)) {
|
|
3531
3347
|
throw new Error('Unexpectedly calling isValid() with a rootSchema that differs from the construction rootSchema');
|
|
3532
3348
|
}
|
|
@@ -3537,16 +3353,16 @@ var ParserValidator = /*#__PURE__*/function () {
|
|
|
3537
3353
|
*
|
|
3538
3354
|
* @param _schema - The schema parameter that is ignored
|
|
3539
3355
|
* @param _formData - The formData parameter that is ignored
|
|
3540
|
-
|
|
3541
|
-
|
|
3356
|
+
*/
|
|
3357
|
+
rawValidation(_schema, _formData) {
|
|
3542
3358
|
throw new Error('Unexpectedly calling the `rawValidation()` method during schema parsing');
|
|
3543
3359
|
}
|
|
3544
3360
|
/** Implements the `ValidatorType` `toErrorList()` method to throw an error since it is never supposed to be called
|
|
3545
3361
|
*
|
|
3546
3362
|
* @param _errorSchema - The error schema parameter that is ignored
|
|
3547
3363
|
* @param _fieldPath - The field path parameter that is ignored
|
|
3548
|
-
|
|
3549
|
-
|
|
3364
|
+
*/
|
|
3365
|
+
toErrorList(_errorSchema, _fieldPath) {
|
|
3550
3366
|
throw new Error('Unexpectedly calling the `toErrorList()` method during schema parsing');
|
|
3551
3367
|
}
|
|
3552
3368
|
/** Implements the `ValidatorType` `validateFormData()` method to throw an error since it is never supposed to be
|
|
@@ -3557,12 +3373,11 @@ var ParserValidator = /*#__PURE__*/function () {
|
|
|
3557
3373
|
* @param _customValidate - The customValidate parameter that is ignored
|
|
3558
3374
|
* @param _transformErrors - The transformErrors parameter that is ignored
|
|
3559
3375
|
* @param _uiSchema - The uiSchema parameter that is ignored
|
|
3560
|
-
|
|
3561
|
-
|
|
3376
|
+
*/
|
|
3377
|
+
validateFormData(_formData, _schema, _customValidate, _transformErrors, _uiSchema) {
|
|
3562
3378
|
throw new Error('Unexpectedly calling the `validateFormData()` method during schema parsing');
|
|
3563
|
-
}
|
|
3564
|
-
|
|
3565
|
-
}();
|
|
3379
|
+
}
|
|
3380
|
+
}
|
|
3566
3381
|
|
|
3567
3382
|
/** Recursive function used to parse the given `schema` belonging to the `rootSchema`. The `validator` is used to
|
|
3568
3383
|
* capture the sub-schemas that the `isValid()` function is called with. For each schema returned by the
|
|
@@ -3575,17 +3390,15 @@ var ParserValidator = /*#__PURE__*/function () {
|
|
|
3575
3390
|
* @param schema - The current schema element being parsed
|
|
3576
3391
|
*/
|
|
3577
3392
|
function parseSchema(validator, recurseList, rootSchema, schema) {
|
|
3578
|
-
|
|
3579
|
-
schemas.forEach(
|
|
3580
|
-
|
|
3581
|
-
return isEqual__default["default"](item, schema);
|
|
3582
|
-
});
|
|
3393
|
+
const schemas = retrieveSchemaInternal(validator, schema, rootSchema, undefined, true);
|
|
3394
|
+
schemas.forEach(schema => {
|
|
3395
|
+
const sameSchemaIndex = recurseList.findIndex(item => isEqual__default["default"](item, schema));
|
|
3583
3396
|
if (sameSchemaIndex === -1) {
|
|
3584
3397
|
recurseList.push(schema);
|
|
3585
|
-
|
|
3586
|
-
allOptions.forEach(
|
|
3398
|
+
const allOptions = resolveAnyOrOneOfSchemas(validator, schema, rootSchema, true);
|
|
3399
|
+
allOptions.forEach(s => {
|
|
3587
3400
|
if (PROPERTIES_KEY in s && s[PROPERTIES_KEY]) {
|
|
3588
|
-
forEach__default["default"](schema[PROPERTIES_KEY],
|
|
3401
|
+
forEach__default["default"](schema[PROPERTIES_KEY], value => {
|
|
3589
3402
|
parseSchema(validator, recurseList, rootSchema, value);
|
|
3590
3403
|
});
|
|
3591
3404
|
}
|
|
@@ -3603,8 +3416,8 @@ function parseSchema(validator, recurseList, rootSchema, schema) {
|
|
|
3603
3416
|
* @returns - The `SchemaMap` of all schemas that were parsed
|
|
3604
3417
|
*/
|
|
3605
3418
|
function schemaParser(rootSchema) {
|
|
3606
|
-
|
|
3607
|
-
|
|
3419
|
+
const validator = new ParserValidator(rootSchema);
|
|
3420
|
+
const recurseList = [];
|
|
3608
3421
|
parseSchema(validator, recurseList, rootSchema, rootSchema);
|
|
3609
3422
|
return validator.getSchemaMap();
|
|
3610
3423
|
}
|