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