@rjsf/utils 5.5.0 → 5.5.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.
@@ -615,7 +615,7 @@
615
615
  deep: false
616
616
  });
617
617
  } catch (e) {
618
- console.warn('could not merge subschemas in allOf:\n' + e);
618
+ console.warn('could not merge subschemas in allOf:\n', e);
619
619
  var _resolvedSchema = resolvedSchema,
620
620
  resolvedSchemaWithoutAllOf = _objectWithoutPropertiesLoose(_resolvedSchema, _excluded3);
621
621
  return resolvedSchemaWithoutAllOf;
@@ -745,9 +745,7 @@
745
745
  type: 'object',
746
746
  properties: (_properties = {}, _properties[dependencyKey] = conditionPropertySchema, _properties)
747
747
  };
748
- var _validator$validateFo = validator.validateFormData(formData, conditionSchema),
749
- errors = _validator$validateFo.errors;
750
- return errors.length === 0;
748
+ return validator.isValid(conditionSchema, formData, rootSchema);
751
749
  }
752
750
  return false;
753
751
  });
@@ -1125,15 +1123,19 @@
1125
1123
  * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
1126
1124
  * If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
1127
1125
  * false when computing defaults for any nested object properties.
1126
+ * @param [_recurseList=[]] - The list of ref names currently being recursed, used to prevent infinite recursion
1128
1127
  * @returns - The resulting `formData` with all the defaults provided
1129
1128
  */
1130
- function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFormData, includeUndefinedValues) {
1129
+ function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFormData, includeUndefinedValues, _recurseList) {
1131
1130
  if (rootSchema === void 0) {
1132
1131
  rootSchema = {};
1133
1132
  }
1134
1133
  if (includeUndefinedValues === void 0) {
1135
1134
  includeUndefinedValues = false;
1136
1135
  }
1136
+ if (_recurseList === void 0) {
1137
+ _recurseList = [];
1138
+ }
1137
1139
  var formData = isObject(rawFormData) ? rawFormData : {};
1138
1140
  var schema = isObject(rawSchema) ? rawSchema : {};
1139
1141
  // Compute the defaults recursively: give highest priority to deepest nodes.
@@ -1145,15 +1147,18 @@
1145
1147
  } else if (DEFAULT_KEY in schema) {
1146
1148
  defaults = schema["default"];
1147
1149
  } else if (REF_KEY in schema) {
1150
+ var refName = schema[REF_KEY];
1148
1151
  // Use referenced schema defaults for this node.
1149
- var refSchema = findSchemaDefinition(schema[REF_KEY], rootSchema);
1150
- return computeDefaults(validator, refSchema, defaults, rootSchema, formData, includeUndefinedValues);
1152
+ if (!_recurseList.includes(refName)) {
1153
+ var refSchema = findSchemaDefinition(refName, rootSchema);
1154
+ return computeDefaults(validator, refSchema, defaults, rootSchema, formData, includeUndefinedValues, _recurseList.concat(refName));
1155
+ }
1151
1156
  } else if (DEPENDENCIES_KEY in schema) {
1152
1157
  var resolvedSchema = resolveDependencies(validator, schema, rootSchema, formData);
1153
- return computeDefaults(validator, resolvedSchema, defaults, rootSchema, formData, includeUndefinedValues);
1158
+ return computeDefaults(validator, resolvedSchema, defaults, rootSchema, formData, includeUndefinedValues, _recurseList);
1154
1159
  } else if (isFixedItems(schema)) {
1155
1160
  defaults = schema.items.map(function (itemSchema, idx) {
1156
- return computeDefaults(validator, itemSchema, Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined, rootSchema, formData, includeUndefinedValues);
1161
+ return computeDefaults(validator, itemSchema, Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined, rootSchema, formData, includeUndefinedValues, _recurseList);
1157
1162
  });
1158
1163
  } else if (ONE_OF_KEY in schema) {
1159
1164
  if (schema.oneOf.length === 0) {
@@ -1177,16 +1182,30 @@
1177
1182
  var objectDefaults = Object.keys(schema.properties || {}).reduce(function (acc, key) {
1178
1183
  // Compute the defaults for this node, with the parent defaults we might
1179
1184
  // have from a previous run: defaults[key].
1180
- var computedDefault = computeDefaults(validator, get__default["default"](schema, [PROPERTIES_KEY, key]), get__default["default"](defaults, [key]), rootSchema, get__default["default"](formData, [key]), includeUndefinedValues === true);
1185
+ 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);
1181
1186
  maybeAddDefaultToObject(acc, key, computedDefault, includeUndefinedValues, schema.required);
1182
1187
  return acc;
1183
1188
  }, {});
1184
- if (schema.additionalProperties && isObject(defaults)) {
1185
- var additionalPropertiesSchema = isObject(schema.additionalProperties) ? schema.additionalProperties : {}; // as per spec additionalProperties may be either schema or boolean
1186
- Object.keys(defaults).filter(function (key) {
1187
- return !schema.properties || !schema.properties[key];
1188
- }).forEach(function (key) {
1189
- var computedDefault = computeDefaults(validator, additionalPropertiesSchema, get__default["default"](defaults, [key]), rootSchema, get__default["default"](formData, [key]), includeUndefinedValues === true);
1189
+ if (schema.additionalProperties) {
1190
+ // as per spec additionalProperties may be either schema or boolean
1191
+ var additionalPropertiesSchema = isObject(schema.additionalProperties) ? schema.additionalProperties : {};
1192
+ var keys = new Set();
1193
+ if (isObject(defaults)) {
1194
+ Object.keys(defaults).filter(function (key) {
1195
+ return !schema.properties || !schema.properties[key];
1196
+ }).forEach(function (key) {
1197
+ return keys.add(key);
1198
+ });
1199
+ }
1200
+ if (isObject(formData)) {
1201
+ Object.keys(formData).filter(function (key) {
1202
+ return !schema.properties || !schema.properties[key];
1203
+ }).forEach(function (key) {
1204
+ return keys.add(key);
1205
+ });
1206
+ }
1207
+ keys.forEach(function (key) {
1208
+ var computedDefault = computeDefaults(validator, additionalPropertiesSchema, get__default["default"](defaults, [key]), rootSchema, get__default["default"](formData, [key]), includeUndefinedValues === true, _recurseList);
1190
1209
  maybeAddDefaultToObject(objectDefaults, key, computedDefault, includeUndefinedValues);
1191
1210
  });
1192
1211
  }
@@ -1197,14 +1216,14 @@
1197
1216
  if (Array.isArray(defaults)) {
1198
1217
  defaults = defaults.map(function (item, idx) {
1199
1218
  var schemaItem = getInnerSchemaForArrayItem(schema, AdditionalItemsHandling.Fallback, idx);
1200
- return computeDefaults(validator, schemaItem, item, rootSchema);
1219
+ return computeDefaults(validator, schemaItem, item, rootSchema, undefined, undefined, _recurseList);
1201
1220
  });
1202
1221
  }
1203
1222
  // Deeply inject defaults into already existing form data
1204
1223
  if (Array.isArray(rawFormData)) {
1205
1224
  var schemaItem = getInnerSchemaForArrayItem(schema);
1206
1225
  defaults = rawFormData.map(function (item, idx) {
1207
- return computeDefaults(validator, schemaItem, get__default["default"](defaults, [idx]), rootSchema, item);
1226
+ return computeDefaults(validator, schemaItem, get__default["default"](defaults, [idx]), rootSchema, item, undefined, _recurseList);
1208
1227
  });
1209
1228
  }
1210
1229
  if (schema.minItems) {
@@ -1215,7 +1234,7 @@
1215
1234
  // populate the array with the defaults
1216
1235
  var fillerSchema = getInnerSchemaForArrayItem(schema, AdditionalItemsHandling.Invert);
1217
1236
  var fillerDefault = fillerSchema["default"];
1218
- var fillerEntries = new Array(schema.minItems - defaultsLength).fill(computeDefaults(validator, fillerSchema, fillerDefault, rootSchema));
1237
+ var fillerEntries = new Array(schema.minItems - defaultsLength).fill(computeDefaults(validator, fillerSchema, fillerDefault, rootSchema, undefined, undefined, _recurseList));
1219
1238
  // then fill up the rest with either the item default or empty, up to minItems
1220
1239
  return defaultEntries.concat(fillerEntries);
1221
1240
  }
@@ -1522,30 +1541,34 @@
1522
1541
  return newFormData;
1523
1542
  }
1524
1543
 
1525
- /** Generates an `IdSchema` object for the `schema`, recursively
1544
+ /** An internal helper that generates an `IdSchema` object for the `schema`, recursively with protection against
1545
+ * infinite recursion
1526
1546
  *
1527
1547
  * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
1528
1548
  * @param schema - The schema for which the `IdSchema` is desired
1549
+ * @param idPrefix - The prefix to use for the id
1550
+ * @param idSeparator - The separator to use for the path segments in the id
1529
1551
  * @param [id] - The base id for the schema
1530
1552
  * @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
1531
1553
  * @param [formData] - The current formData, if any, to assist retrieving a schema
1532
- * @param [idPrefix='root'] - The prefix to use for the id
1533
- * @param [idSeparator='_'] - The separator to use for the path segments in the id
1554
+ * @param [_recurseList=[]] - The list of retrieved schemas currently being recursed, used to prevent infinite recursion
1534
1555
  * @returns - The `IdSchema` object for the `schema`
1535
1556
  */
1536
- function toIdSchema(validator, schema, id, rootSchema, formData, idPrefix, idSeparator) {
1537
- if (idPrefix === void 0) {
1538
- idPrefix = 'root';
1539
- }
1540
- if (idSeparator === void 0) {
1541
- idSeparator = '_';
1557
+ function toIdSchemaInternal(validator, schema, idPrefix, idSeparator, id, rootSchema, formData, _recurseList) {
1558
+ if (_recurseList === void 0) {
1559
+ _recurseList = [];
1542
1560
  }
1543
1561
  if (REF_KEY in schema || DEPENDENCIES_KEY in schema || ALL_OF_KEY in schema) {
1544
1562
  var _schema = retrieveSchema(validator, schema, rootSchema, formData);
1545
- return toIdSchema(validator, _schema, id, rootSchema, formData, idPrefix, idSeparator);
1563
+ var sameSchemaIndex = _recurseList.findIndex(function (item) {
1564
+ return isEqual__default["default"](item, _schema);
1565
+ });
1566
+ if (sameSchemaIndex === -1) {
1567
+ return toIdSchemaInternal(validator, _schema, idPrefix, idSeparator, id, rootSchema, formData, _recurseList.concat(_schema));
1568
+ }
1546
1569
  }
1547
1570
  if (ITEMS_KEY in schema && !get__default["default"](schema, [ITEMS_KEY, REF_KEY])) {
1548
- return toIdSchema(validator, get__default["default"](schema, ITEMS_KEY), id, rootSchema, formData, idPrefix, idSeparator);
1571
+ return toIdSchemaInternal(validator, get__default["default"](schema, ITEMS_KEY), idPrefix, idSeparator, id, rootSchema, formData, _recurseList);
1549
1572
  }
1550
1573
  var $id = id || idPrefix;
1551
1574
  var idSchema = {
@@ -1555,62 +1578,104 @@
1555
1578
  for (var name in schema.properties) {
1556
1579
  var field = get__default["default"](schema, [PROPERTIES_KEY, name]);
1557
1580
  var fieldId = idSchema[ID_KEY] + idSeparator + name;
1558
- idSchema[name] = toIdSchema(validator, isObject(field) ? field : {}, fieldId, rootSchema,
1581
+ idSchema[name] = toIdSchemaInternal(validator, isObject(field) ? field : {}, idPrefix, idSeparator, fieldId, rootSchema,
1559
1582
  // It's possible that formData is not an object -- this can happen if an
1560
1583
  // array item has just been added, but not populated with data yet
1561
- get__default["default"](formData, [name]), idPrefix, idSeparator);
1584
+ get__default["default"](formData, [name]), _recurseList);
1562
1585
  }
1563
1586
  }
1564
1587
  return idSchema;
1565
1588
  }
1589
+ /** Generates an `IdSchema` object for the `schema`, recursively
1590
+ *
1591
+ * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
1592
+ * @param schema - The schema for which the `IdSchema` is desired
1593
+ * @param [id] - The base id for the schema
1594
+ * @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
1595
+ * @param [formData] - The current formData, if any, to assist retrieving a schema
1596
+ * @param [idPrefix='root'] - The prefix to use for the id
1597
+ * @param [idSeparator='_'] - The separator to use for the path segments in the id
1598
+ * @returns - The `IdSchema` object for the `schema`
1599
+ */
1600
+ function toIdSchema(validator, schema, id, rootSchema, formData, idPrefix, idSeparator) {
1601
+ if (idPrefix === void 0) {
1602
+ idPrefix = 'root';
1603
+ }
1604
+ if (idSeparator === void 0) {
1605
+ idSeparator = '_';
1606
+ }
1607
+ return toIdSchemaInternal(validator, schema, idPrefix, idSeparator, id, rootSchema, formData);
1608
+ }
1566
1609
 
1567
- /** Generates an `PathSchema` object for the `schema`, recursively
1610
+ /** An internal helper that generates an `PathSchema` object for the `schema`, recursively with protection against
1611
+ * infinite recursion
1568
1612
  *
1569
1613
  * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
1570
1614
  * @param schema - The schema for which the `PathSchema` is desired
1571
1615
  * @param [name=''] - The base name for the schema
1572
1616
  * @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
1573
1617
  * @param [formData] - The current formData, if any, to assist retrieving a schema
1618
+ * @param [_recurseList=[]] - The list of retrieved schemas currently being recursed, used to prevent infinite recursion
1574
1619
  * @returns - The `PathSchema` object for the `schema`
1575
1620
  */
1576
- function toPathSchema(validator, schema, name, rootSchema, formData) {
1621
+ function toPathSchemaInternal(validator, schema, name, rootSchema, formData, _recurseList) {
1577
1622
  var _pathSchema;
1578
- if (name === void 0) {
1579
- name = '';
1623
+ if (_recurseList === void 0) {
1624
+ _recurseList = [];
1580
1625
  }
1581
1626
  if (REF_KEY in schema || DEPENDENCIES_KEY in schema || ALL_OF_KEY in schema) {
1582
1627
  var _schema = retrieveSchema(validator, schema, rootSchema, formData);
1583
- return toPathSchema(validator, _schema, name, rootSchema, formData);
1628
+ var sameSchemaIndex = _recurseList.findIndex(function (item) {
1629
+ return isEqual__default["default"](item, _schema);
1630
+ });
1631
+ if (sameSchemaIndex === -1) {
1632
+ return toPathSchemaInternal(validator, _schema, name, rootSchema, formData, _recurseList.concat(_schema));
1633
+ }
1584
1634
  }
1585
1635
  var pathSchema = (_pathSchema = {}, _pathSchema[NAME_KEY] = name.replace(/^\./, ''), _pathSchema);
1586
1636
  if (ONE_OF_KEY in schema) {
1587
1637
  var index = getClosestMatchingOption(validator, rootSchema, formData, schema.oneOf, 0);
1588
1638
  var _schema2 = schema.oneOf[index];
1589
- return toPathSchema(validator, _schema2, name, rootSchema, formData);
1639
+ return toPathSchemaInternal(validator, _schema2, name, rootSchema, formData, _recurseList);
1590
1640
  }
1591
1641
  if (ANY_OF_KEY in schema) {
1592
1642
  var _index = getClosestMatchingOption(validator, rootSchema, formData, schema.anyOf, 0);
1593
1643
  var _schema3 = schema.anyOf[_index];
1594
- return toPathSchema(validator, _schema3, name, rootSchema, formData);
1644
+ return toPathSchemaInternal(validator, _schema3, name, rootSchema, formData, _recurseList);
1595
1645
  }
1596
1646
  if (ADDITIONAL_PROPERTIES_KEY in schema && schema[ADDITIONAL_PROPERTIES_KEY] !== false) {
1597
1647
  set__default["default"](pathSchema, RJSF_ADDITONAL_PROPERTIES_FLAG, true);
1598
1648
  }
1599
1649
  if (ITEMS_KEY in schema && Array.isArray(formData)) {
1600
1650
  formData.forEach(function (element, i) {
1601
- pathSchema[i] = toPathSchema(validator, schema.items, name + "." + i, rootSchema, element);
1651
+ pathSchema[i] = toPathSchemaInternal(validator, schema.items, name + "." + i, rootSchema, element, _recurseList);
1602
1652
  });
1603
1653
  } else if (PROPERTIES_KEY in schema) {
1604
1654
  for (var property in schema.properties) {
1605
1655
  var field = get__default["default"](schema, [PROPERTIES_KEY, property]);
1606
- pathSchema[property] = toPathSchema(validator, field, name + "." + property, rootSchema,
1656
+ pathSchema[property] = toPathSchemaInternal(validator, field, name + "." + property, rootSchema,
1607
1657
  // It's possible that formData is not an object -- this can happen if an
1608
1658
  // array item has just been added, but not populated with data yet
1609
- get__default["default"](formData, [property]));
1659
+ get__default["default"](formData, [property]), _recurseList);
1610
1660
  }
1611
1661
  }
1612
1662
  return pathSchema;
1613
1663
  }
1664
+ /** Generates an `PathSchema` object for the `schema`, recursively
1665
+ *
1666
+ * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
1667
+ * @param schema - The schema for which the `PathSchema` is desired
1668
+ * @param [name=''] - The base name for the schema
1669
+ * @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
1670
+ * @param [formData] - The current formData, if any, to assist retrieving a schema
1671
+ * @returns - The `PathSchema` object for the `schema`
1672
+ */
1673
+ function toPathSchema(validator, schema, name, rootSchema, formData) {
1674
+ if (name === void 0) {
1675
+ name = '';
1676
+ }
1677
+ return toPathSchemaInternal(validator, schema, name, rootSchema, formData);
1678
+ }
1614
1679
 
1615
1680
  /** The `SchemaUtils` class provides a wrapper around the publicly exported APIs in the `utils/schema` directory such
1616
1681
  * that one does not have to explicitly pass the `validator` or `rootSchema` to each method. Since both the `validator`
@@ -2039,16 +2104,15 @@
2039
2104
  * get the result and/or reset all the errors back to an initial set and start again.
2040
2105
  */
2041
2106
  var ErrorSchemaBuilder = /*#__PURE__*/function () {
2042
- /** The error schema being built
2043
- *
2044
- * @private
2045
- */
2046
-
2047
2107
  /** Construct an `ErrorSchemaBuilder` with an optional initial set of errors in an `ErrorSchema`.
2048
2108
  *
2049
2109
  * @param [initialSchema] - The optional set of initial errors, that will be cloned into the class
2050
2110
  */
2051
2111
  function ErrorSchemaBuilder(initialSchema) {
2112
+ /** The error schema being built
2113
+ *
2114
+ * @private
2115
+ */
2052
2116
  this.errorSchema = {};
2053
2117
  this.resetAllErrors(initialSchema);
2054
2118
  }