@rjsf/utils 5.5.0 → 5.5.1

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/index.d.ts CHANGED
@@ -1373,7 +1373,7 @@ declare function isObject(thing: any): boolean;
1373
1373
  /** Helper function that will return the value to use for a widget `label` based on `hideLabel`. The `fallback` is used
1374
1374
  * as the return value from the function when `hideLabel` is true. Due to the implementation of theme components, it
1375
1375
  * may be necessary to return something other than `undefined` to cause the theme component to not render a label. Some
1376
- * themes require may `false` and others may require and empty string.
1376
+ * themes require may `false` and others may require an empty string.
1377
1377
  *
1378
1378
  * @param [label] - The label string or component to render when not hidden
1379
1379
  * @param [hideLabel] - Flag, if true, will cause the label to be hidden
@@ -632,7 +632,7 @@ function retrieveSchema(validator, schema, rootSchema, rawFormData) {
632
632
  deep: false
633
633
  });
634
634
  } catch (e) {
635
- console.warn('could not merge subschemas in allOf:\n' + e);
635
+ console.warn('could not merge subschemas in allOf:\n', e);
636
636
  var _resolvedSchema = resolvedSchema,
637
637
  resolvedSchemaWithoutAllOf = _objectWithoutPropertiesLoose(_resolvedSchema, _excluded3);
638
638
  return resolvedSchemaWithoutAllOf;
@@ -762,9 +762,7 @@ function withExactlyOneSubschema(validator, schema, rootSchema, dependencyKey, o
762
762
  type: 'object',
763
763
  properties: (_properties = {}, _properties[dependencyKey] = conditionPropertySchema, _properties)
764
764
  };
765
- var _validator$validateFo = validator.validateFormData(formData, conditionSchema),
766
- errors = _validator$validateFo.errors;
767
- return errors.length === 0;
765
+ return validator.isValid(conditionSchema, formData, rootSchema);
768
766
  }
769
767
  return false;
770
768
  });
@@ -1142,15 +1140,19 @@ function maybeAddDefaultToObject(obj, key, computedDefault, includeUndefinedValu
1142
1140
  * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
1143
1141
  * If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
1144
1142
  * false when computing defaults for any nested object properties.
1143
+ * @param [_recurseList=[]] - The list of ref names currently being recursed, used to prevent infinite recursion
1145
1144
  * @returns - The resulting `formData` with all the defaults provided
1146
1145
  */
1147
- function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFormData, includeUndefinedValues) {
1146
+ function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFormData, includeUndefinedValues, _recurseList) {
1148
1147
  if (rootSchema === void 0) {
1149
1148
  rootSchema = {};
1150
1149
  }
1151
1150
  if (includeUndefinedValues === void 0) {
1152
1151
  includeUndefinedValues = false;
1153
1152
  }
1153
+ if (_recurseList === void 0) {
1154
+ _recurseList = [];
1155
+ }
1154
1156
  var formData = isObject(rawFormData) ? rawFormData : {};
1155
1157
  var schema = isObject(rawSchema) ? rawSchema : {};
1156
1158
  // Compute the defaults recursively: give highest priority to deepest nodes.
@@ -1162,15 +1164,18 @@ function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFo
1162
1164
  } else if (DEFAULT_KEY in schema) {
1163
1165
  defaults = schema["default"];
1164
1166
  } else if (REF_KEY in schema) {
1167
+ var refName = schema[REF_KEY];
1165
1168
  // Use referenced schema defaults for this node.
1166
- var refSchema = findSchemaDefinition(schema[REF_KEY], rootSchema);
1167
- return computeDefaults(validator, refSchema, defaults, rootSchema, formData, includeUndefinedValues);
1169
+ if (!_recurseList.includes(refName)) {
1170
+ var refSchema = findSchemaDefinition(refName, rootSchema);
1171
+ return computeDefaults(validator, refSchema, defaults, rootSchema, formData, includeUndefinedValues, _recurseList.concat(refName));
1172
+ }
1168
1173
  } else if (DEPENDENCIES_KEY in schema) {
1169
1174
  var resolvedSchema = resolveDependencies(validator, schema, rootSchema, formData);
1170
- return computeDefaults(validator, resolvedSchema, defaults, rootSchema, formData, includeUndefinedValues);
1175
+ return computeDefaults(validator, resolvedSchema, defaults, rootSchema, formData, includeUndefinedValues, _recurseList);
1171
1176
  } else if (isFixedItems(schema)) {
1172
1177
  defaults = schema.items.map(function (itemSchema, idx) {
1173
- return computeDefaults(validator, itemSchema, Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined, rootSchema, formData, includeUndefinedValues);
1178
+ return computeDefaults(validator, itemSchema, Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined, rootSchema, formData, includeUndefinedValues, _recurseList);
1174
1179
  });
1175
1180
  } else if (ONE_OF_KEY in schema) {
1176
1181
  if (schema.oneOf.length === 0) {
@@ -1194,16 +1199,30 @@ function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFo
1194
1199
  var objectDefaults = Object.keys(schema.properties || {}).reduce(function (acc, key) {
1195
1200
  // Compute the defaults for this node, with the parent defaults we might
1196
1201
  // have from a previous run: defaults[key].
1197
- var computedDefault = computeDefaults(validator, get__default["default"](schema, [PROPERTIES_KEY, key]), get__default["default"](defaults, [key]), rootSchema, get__default["default"](formData, [key]), includeUndefinedValues === true);
1202
+ var computedDefault = computeDefaults(validator, get__default["default"](schema, [PROPERTIES_KEY, key]), get__default["default"](defaults, [key]), rootSchema, get__default["default"](formData, [key]), includeUndefinedValues === true, _recurseList);
1198
1203
  maybeAddDefaultToObject(acc, key, computedDefault, includeUndefinedValues, schema.required);
1199
1204
  return acc;
1200
1205
  }, {});
1201
- if (schema.additionalProperties && isObject(defaults)) {
1202
- var additionalPropertiesSchema = isObject(schema.additionalProperties) ? schema.additionalProperties : {}; // as per spec additionalProperties may be either schema or boolean
1203
- Object.keys(defaults).filter(function (key) {
1204
- return !schema.properties || !schema.properties[key];
1205
- }).forEach(function (key) {
1206
- var computedDefault = computeDefaults(validator, additionalPropertiesSchema, get__default["default"](defaults, [key]), rootSchema, get__default["default"](formData, [key]), includeUndefinedValues === true);
1206
+ if (schema.additionalProperties) {
1207
+ // as per spec additionalProperties may be either schema or boolean
1208
+ var additionalPropertiesSchema = isObject(schema.additionalProperties) ? schema.additionalProperties : {};
1209
+ var keys = new Set();
1210
+ if (isObject(defaults)) {
1211
+ Object.keys(defaults).filter(function (key) {
1212
+ return !schema.properties || !schema.properties[key];
1213
+ }).forEach(function (key) {
1214
+ return keys.add(key);
1215
+ });
1216
+ }
1217
+ if (isObject(formData)) {
1218
+ Object.keys(formData).filter(function (key) {
1219
+ return !schema.properties || !schema.properties[key];
1220
+ }).forEach(function (key) {
1221
+ return keys.add(key);
1222
+ });
1223
+ }
1224
+ keys.forEach(function (key) {
1225
+ var computedDefault = computeDefaults(validator, additionalPropertiesSchema, get__default["default"](defaults, [key]), rootSchema, get__default["default"](formData, [key]), includeUndefinedValues === true, _recurseList);
1207
1226
  maybeAddDefaultToObject(objectDefaults, key, computedDefault, includeUndefinedValues);
1208
1227
  });
1209
1228
  }
@@ -1214,14 +1233,14 @@ function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFo
1214
1233
  if (Array.isArray(defaults)) {
1215
1234
  defaults = defaults.map(function (item, idx) {
1216
1235
  var schemaItem = getInnerSchemaForArrayItem(schema, AdditionalItemsHandling.Fallback, idx);
1217
- return computeDefaults(validator, schemaItem, item, rootSchema);
1236
+ return computeDefaults(validator, schemaItem, item, rootSchema, undefined, undefined, _recurseList);
1218
1237
  });
1219
1238
  }
1220
1239
  // Deeply inject defaults into already existing form data
1221
1240
  if (Array.isArray(rawFormData)) {
1222
1241
  var schemaItem = getInnerSchemaForArrayItem(schema);
1223
1242
  defaults = rawFormData.map(function (item, idx) {
1224
- return computeDefaults(validator, schemaItem, get__default["default"](defaults, [idx]), rootSchema, item);
1243
+ return computeDefaults(validator, schemaItem, get__default["default"](defaults, [idx]), rootSchema, item, undefined, _recurseList);
1225
1244
  });
1226
1245
  }
1227
1246
  if (schema.minItems) {
@@ -1232,7 +1251,7 @@ function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFo
1232
1251
  // populate the array with the defaults
1233
1252
  var fillerSchema = getInnerSchemaForArrayItem(schema, AdditionalItemsHandling.Invert);
1234
1253
  var fillerDefault = fillerSchema["default"];
1235
- var fillerEntries = new Array(schema.minItems - defaultsLength).fill(computeDefaults(validator, fillerSchema, fillerDefault, rootSchema));
1254
+ var fillerEntries = new Array(schema.minItems - defaultsLength).fill(computeDefaults(validator, fillerSchema, fillerDefault, rootSchema, undefined, undefined, _recurseList));
1236
1255
  // then fill up the rest with either the item default or empty, up to minItems
1237
1256
  return defaultEntries.concat(fillerEntries);
1238
1257
  }
@@ -1539,30 +1558,34 @@ function sanitizeDataForNewSchema(validator, rootSchema, newSchema, oldSchema, d
1539
1558
  return newFormData;
1540
1559
  }
1541
1560
 
1542
- /** Generates an `IdSchema` object for the `schema`, recursively
1561
+ /** An internal helper that generates an `IdSchema` object for the `schema`, recursively with protection against
1562
+ * infinite recursion
1543
1563
  *
1544
1564
  * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
1545
1565
  * @param schema - The schema for which the `IdSchema` is desired
1566
+ * @param idPrefix - The prefix to use for the id
1567
+ * @param idSeparator - The separator to use for the path segments in the id
1546
1568
  * @param [id] - The base id for the schema
1547
1569
  * @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
1548
1570
  * @param [formData] - The current formData, if any, to assist retrieving a schema
1549
- * @param [idPrefix='root'] - The prefix to use for the id
1550
- * @param [idSeparator='_'] - The separator to use for the path segments in the id
1571
+ * @param [_recurseList=[]] - The list of retrieved schemas currently being recursed, used to prevent infinite recursion
1551
1572
  * @returns - The `IdSchema` object for the `schema`
1552
1573
  */
1553
- function toIdSchema(validator, schema, id, rootSchema, formData, idPrefix, idSeparator) {
1554
- if (idPrefix === void 0) {
1555
- idPrefix = 'root';
1556
- }
1557
- if (idSeparator === void 0) {
1558
- idSeparator = '_';
1574
+ function toIdSchemaInternal(validator, schema, idPrefix, idSeparator, id, rootSchema, formData, _recurseList) {
1575
+ if (_recurseList === void 0) {
1576
+ _recurseList = [];
1559
1577
  }
1560
1578
  if (REF_KEY in schema || DEPENDENCIES_KEY in schema || ALL_OF_KEY in schema) {
1561
1579
  var _schema = retrieveSchema(validator, schema, rootSchema, formData);
1562
- return toIdSchema(validator, _schema, id, rootSchema, formData, idPrefix, idSeparator);
1580
+ var sameSchemaIndex = _recurseList.findIndex(function (item) {
1581
+ return isEqual__default["default"](item, _schema);
1582
+ });
1583
+ if (sameSchemaIndex === -1) {
1584
+ return toIdSchemaInternal(validator, _schema, idPrefix, idSeparator, id, rootSchema, formData, _recurseList.concat(_schema));
1585
+ }
1563
1586
  }
1564
1587
  if (ITEMS_KEY in schema && !get__default["default"](schema, [ITEMS_KEY, REF_KEY])) {
1565
- return toIdSchema(validator, get__default["default"](schema, ITEMS_KEY), id, rootSchema, formData, idPrefix, idSeparator);
1588
+ return toIdSchemaInternal(validator, get__default["default"](schema, ITEMS_KEY), idPrefix, idSeparator, id, rootSchema, formData, _recurseList);
1566
1589
  }
1567
1590
  var $id = id || idPrefix;
1568
1591
  var idSchema = {
@@ -1572,62 +1595,104 @@ function toIdSchema(validator, schema, id, rootSchema, formData, idPrefix, idSep
1572
1595
  for (var name in schema.properties) {
1573
1596
  var field = get__default["default"](schema, [PROPERTIES_KEY, name]);
1574
1597
  var fieldId = idSchema[ID_KEY] + idSeparator + name;
1575
- idSchema[name] = toIdSchema(validator, isObject(field) ? field : {}, fieldId, rootSchema,
1598
+ idSchema[name] = toIdSchemaInternal(validator, isObject(field) ? field : {}, idPrefix, idSeparator, fieldId, rootSchema,
1576
1599
  // It's possible that formData is not an object -- this can happen if an
1577
1600
  // array item has just been added, but not populated with data yet
1578
- get__default["default"](formData, [name]), idPrefix, idSeparator);
1601
+ get__default["default"](formData, [name]), _recurseList);
1579
1602
  }
1580
1603
  }
1581
1604
  return idSchema;
1582
1605
  }
1606
+ /** Generates an `IdSchema` object for the `schema`, recursively
1607
+ *
1608
+ * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
1609
+ * @param schema - The schema for which the `IdSchema` is desired
1610
+ * @param [id] - The base id for the schema
1611
+ * @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
1612
+ * @param [formData] - The current formData, if any, to assist retrieving a schema
1613
+ * @param [idPrefix='root'] - The prefix to use for the id
1614
+ * @param [idSeparator='_'] - The separator to use for the path segments in the id
1615
+ * @returns - The `IdSchema` object for the `schema`
1616
+ */
1617
+ function toIdSchema(validator, schema, id, rootSchema, formData, idPrefix, idSeparator) {
1618
+ if (idPrefix === void 0) {
1619
+ idPrefix = 'root';
1620
+ }
1621
+ if (idSeparator === void 0) {
1622
+ idSeparator = '_';
1623
+ }
1624
+ return toIdSchemaInternal(validator, schema, idPrefix, idSeparator, id, rootSchema, formData);
1625
+ }
1583
1626
 
1584
- /** Generates an `PathSchema` object for the `schema`, recursively
1627
+ /** An internal helper that generates an `PathSchema` object for the `schema`, recursively with protection against
1628
+ * infinite recursion
1585
1629
  *
1586
1630
  * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
1587
1631
  * @param schema - The schema for which the `PathSchema` is desired
1588
1632
  * @param [name=''] - The base name for the schema
1589
1633
  * @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
1590
1634
  * @param [formData] - The current formData, if any, to assist retrieving a schema
1635
+ * @param [_recurseList=[]] - The list of retrieved schemas currently being recursed, used to prevent infinite recursion
1591
1636
  * @returns - The `PathSchema` object for the `schema`
1592
1637
  */
1593
- function toPathSchema(validator, schema, name, rootSchema, formData) {
1638
+ function toPathSchemaInternal(validator, schema, name, rootSchema, formData, _recurseList) {
1594
1639
  var _pathSchema;
1595
- if (name === void 0) {
1596
- name = '';
1640
+ if (_recurseList === void 0) {
1641
+ _recurseList = [];
1597
1642
  }
1598
1643
  if (REF_KEY in schema || DEPENDENCIES_KEY in schema || ALL_OF_KEY in schema) {
1599
1644
  var _schema = retrieveSchema(validator, schema, rootSchema, formData);
1600
- return toPathSchema(validator, _schema, name, rootSchema, formData);
1645
+ var sameSchemaIndex = _recurseList.findIndex(function (item) {
1646
+ return isEqual__default["default"](item, _schema);
1647
+ });
1648
+ if (sameSchemaIndex === -1) {
1649
+ return toPathSchemaInternal(validator, _schema, name, rootSchema, formData, _recurseList.concat(_schema));
1650
+ }
1601
1651
  }
1602
1652
  var pathSchema = (_pathSchema = {}, _pathSchema[NAME_KEY] = name.replace(/^\./, ''), _pathSchema);
1603
1653
  if (ONE_OF_KEY in schema) {
1604
1654
  var index = getClosestMatchingOption(validator, rootSchema, formData, schema.oneOf, 0);
1605
1655
  var _schema2 = schema.oneOf[index];
1606
- return toPathSchema(validator, _schema2, name, rootSchema, formData);
1656
+ return toPathSchemaInternal(validator, _schema2, name, rootSchema, formData, _recurseList);
1607
1657
  }
1608
1658
  if (ANY_OF_KEY in schema) {
1609
1659
  var _index = getClosestMatchingOption(validator, rootSchema, formData, schema.anyOf, 0);
1610
1660
  var _schema3 = schema.anyOf[_index];
1611
- return toPathSchema(validator, _schema3, name, rootSchema, formData);
1661
+ return toPathSchemaInternal(validator, _schema3, name, rootSchema, formData, _recurseList);
1612
1662
  }
1613
1663
  if (ADDITIONAL_PROPERTIES_KEY in schema && schema[ADDITIONAL_PROPERTIES_KEY] !== false) {
1614
1664
  set__default["default"](pathSchema, RJSF_ADDITONAL_PROPERTIES_FLAG, true);
1615
1665
  }
1616
1666
  if (ITEMS_KEY in schema && Array.isArray(formData)) {
1617
1667
  formData.forEach(function (element, i) {
1618
- pathSchema[i] = toPathSchema(validator, schema.items, name + "." + i, rootSchema, element);
1668
+ pathSchema[i] = toPathSchemaInternal(validator, schema.items, name + "." + i, rootSchema, element, _recurseList);
1619
1669
  });
1620
1670
  } else if (PROPERTIES_KEY in schema) {
1621
1671
  for (var property in schema.properties) {
1622
1672
  var field = get__default["default"](schema, [PROPERTIES_KEY, property]);
1623
- pathSchema[property] = toPathSchema(validator, field, name + "." + property, rootSchema,
1673
+ pathSchema[property] = toPathSchemaInternal(validator, field, name + "." + property, rootSchema,
1624
1674
  // It's possible that formData is not an object -- this can happen if an
1625
1675
  // array item has just been added, but not populated with data yet
1626
- get__default["default"](formData, [property]));
1676
+ get__default["default"](formData, [property]), _recurseList);
1627
1677
  }
1628
1678
  }
1629
1679
  return pathSchema;
1630
1680
  }
1681
+ /** Generates an `PathSchema` object for the `schema`, recursively
1682
+ *
1683
+ * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
1684
+ * @param schema - The schema for which the `PathSchema` is desired
1685
+ * @param [name=''] - The base name for the schema
1686
+ * @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
1687
+ * @param [formData] - The current formData, if any, to assist retrieving a schema
1688
+ * @returns - The `PathSchema` object for the `schema`
1689
+ */
1690
+ function toPathSchema(validator, schema, name, rootSchema, formData) {
1691
+ if (name === void 0) {
1692
+ name = '';
1693
+ }
1694
+ return toPathSchemaInternal(validator, schema, name, rootSchema, formData);
1695
+ }
1631
1696
 
1632
1697
  /** The `SchemaUtils` class provides a wrapper around the publicly exported APIs in the `utils/schema` directory such
1633
1698
  * that one does not have to explicitly pass the `validator` or `rootSchema` to each method. Since both the `validator`
@@ -2056,16 +2121,15 @@ function enumOptionsSelectValue(valueIndex, selected, allEnumOptions) {
2056
2121
  * get the result and/or reset all the errors back to an initial set and start again.
2057
2122
  */
2058
2123
  var ErrorSchemaBuilder = /*#__PURE__*/function () {
2059
- /** The error schema being built
2060
- *
2061
- * @private
2062
- */
2063
-
2064
2124
  /** Construct an `ErrorSchemaBuilder` with an optional initial set of errors in an `ErrorSchema`.
2065
2125
  *
2066
2126
  * @param [initialSchema] - The optional set of initial errors, that will be cloned into the class
2067
2127
  */
2068
2128
  function ErrorSchemaBuilder(initialSchema) {
2129
+ /** The error schema being built
2130
+ *
2131
+ * @private
2132
+ */
2069
2133
  this.errorSchema = {};
2070
2134
  this.resetAllErrors(initialSchema);
2071
2135
  }