@rjsf/utils 6.0.0-beta.1 → 6.0.0-beta.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/index.js CHANGED
@@ -46,6 +46,7 @@ __export(index_exports, {
46
46
  ID_KEY: () => ID_KEY,
47
47
  IF_KEY: () => IF_KEY,
48
48
  ITEMS_KEY: () => ITEMS_KEY,
49
+ JSON_SCHEMA_DRAFT_2020_12: () => JSON_SCHEMA_DRAFT_2020_12,
49
50
  JUNK_OPTION_ID: () => JUNK_OPTION_ID,
50
51
  LOOKUP_MAP_NAME: () => LOOKUP_MAP_NAME,
51
52
  NAME_KEY: () => NAME_KEY,
@@ -57,6 +58,7 @@ __export(index_exports, {
57
58
  REQUIRED_KEY: () => REQUIRED_KEY,
58
59
  RJSF_ADDITIONAL_PROPERTIES_FLAG: () => RJSF_ADDITIONAL_PROPERTIES_FLAG,
59
60
  ROOT_SCHEMA_PREFIX: () => ROOT_SCHEMA_PREFIX,
61
+ SCHEMA_KEY: () => SCHEMA_KEY,
60
62
  SUBMIT_BTN_OPTIONS_KEY: () => SUBMIT_BTN_OPTIONS_KEY,
61
63
  TranslatableString: () => TranslatableString,
62
64
  UI_FIELD_KEY: () => UI_FIELD_KEY,
@@ -214,6 +216,7 @@ var READONLY_KEY = "readonly";
214
216
  var REQUIRED_KEY = "required";
215
217
  var SUBMIT_BTN_OPTIONS_KEY = "submitButtonOptions";
216
218
  var REF_KEY = "$ref";
219
+ var SCHEMA_KEY = "$schema";
217
220
  var DISCRIMINATOR_PATH = ["discriminator", "propertyName"];
218
221
  var FORM_CONTEXT_NAME = "formContext";
219
222
  var LOOKUP_MAP_NAME = "layoutGridLookupMap";
@@ -223,6 +226,7 @@ var UI_FIELD_KEY = "ui:field";
223
226
  var UI_WIDGET_KEY = "ui:widget";
224
227
  var UI_OPTIONS_KEY = "ui:options";
225
228
  var UI_GLOBAL_OPTIONS_KEY = "ui:globalOptions";
229
+ var JSON_SCHEMA_DRAFT_2020_12 = "https://json-schema.org/draft/2020-12/schema";
226
230
 
227
231
  // src/getUiOptions.ts
228
232
  function getUiOptions(uiSchema = {}, globalOptions = {}) {
@@ -315,20 +319,52 @@ var import_json_schema_merge_allof = __toESM(require("json-schema-merge-allof"),
315
319
  // src/findSchemaDefinition.ts
316
320
  var import_jsonpointer = __toESM(require("jsonpointer"), 1);
317
321
  var import_omit = __toESM(require("lodash/omit"), 1);
322
+ var import_isObject3 = __toESM(require("lodash/isObject"), 1);
323
+ var import_isEmpty = __toESM(require("lodash/isEmpty"), 1);
324
+ var import_fast_uri = __toESM(require("fast-uri"), 1);
325
+ function findEmbeddedSchemaRecursive(schema, ref) {
326
+ if (ID_KEY in schema && import_fast_uri.default.equal(schema[ID_KEY], ref)) {
327
+ return schema;
328
+ }
329
+ for (const subSchema of Object.values(schema)) {
330
+ if ((0, import_isObject3.default)(subSchema)) {
331
+ const result = findEmbeddedSchemaRecursive(subSchema, ref);
332
+ if (result !== void 0) {
333
+ return result;
334
+ }
335
+ }
336
+ }
337
+ return void 0;
338
+ }
318
339
  function splitKeyElementFromObject(key, object) {
319
340
  const value = object[key];
320
341
  const remaining = (0, import_omit.default)(object, [key]);
321
342
  return [remaining, value];
322
343
  }
323
- function findSchemaDefinitionRecursive($ref, rootSchema = {}, recurseList = []) {
344
+ function findSchemaDefinitionRecursive($ref, rootSchema = {}, recurseList = [], baseURI = ID_KEY in rootSchema ? rootSchema[ID_KEY] : void 0) {
324
345
  const ref = $ref || "";
325
- let decodedRef;
346
+ let current = void 0;
326
347
  if (ref.startsWith("#")) {
327
- decodedRef = decodeURIComponent(ref.substring(1));
328
- } else {
329
- throw new Error(`Could not find a definition for ${$ref}.`);
348
+ const decodedRef = decodeURIComponent(ref.substring(1));
349
+ if (baseURI === void 0 || ID_KEY in rootSchema && rootSchema[ID_KEY] === baseURI) {
350
+ current = import_jsonpointer.default.get(rootSchema, decodedRef);
351
+ } else if (rootSchema[SCHEMA_KEY] === JSON_SCHEMA_DRAFT_2020_12) {
352
+ current = findEmbeddedSchemaRecursive(rootSchema, baseURI.replace(/\/$/, ""));
353
+ if (current !== void 0) {
354
+ current = import_jsonpointer.default.get(current, decodedRef);
355
+ }
356
+ }
357
+ } else if (rootSchema[SCHEMA_KEY] === JSON_SCHEMA_DRAFT_2020_12) {
358
+ const resolvedRef = baseURI ? import_fast_uri.default.resolve(baseURI, ref) : ref;
359
+ const [refId, ...refAnchor] = resolvedRef.replace(/#\/?$/, "").split("#");
360
+ current = findEmbeddedSchemaRecursive(rootSchema, refId.replace(/\/$/, ""));
361
+ if (current !== void 0) {
362
+ baseURI = current[ID_KEY];
363
+ if (!(0, import_isEmpty.default)(refAnchor)) {
364
+ current = import_jsonpointer.default.get(current, decodeURIComponent(refAnchor.join("#")));
365
+ }
366
+ }
330
367
  }
331
- const current = import_jsonpointer.default.get(rootSchema, decodedRef);
332
368
  if (current === void 0) {
333
369
  throw new Error(`Could not find a definition for ${$ref}.`);
334
370
  }
@@ -343,7 +379,7 @@ function findSchemaDefinitionRecursive($ref, rootSchema = {}, recurseList = [])
343
379
  throw new Error(`Definition for ${firstRef} contains a circular reference through ${circularPath}`);
344
380
  }
345
381
  const [remaining, theRef] = splitKeyElementFromObject(REF_KEY, current);
346
- const subSchema = findSchemaDefinitionRecursive(theRef, rootSchema, [...recurseList, ref]);
382
+ const subSchema = findSchemaDefinitionRecursive(theRef, rootSchema, [...recurseList, ref], baseURI);
347
383
  if (Object.keys(remaining).length > 0) {
348
384
  return { ...remaining, ...subSchema };
349
385
  }
@@ -351,9 +387,9 @@ function findSchemaDefinitionRecursive($ref, rootSchema = {}, recurseList = [])
351
387
  }
352
388
  return current;
353
389
  }
354
- function findSchemaDefinition($ref, rootSchema = {}) {
390
+ function findSchemaDefinition($ref, rootSchema = {}, baseURI = ID_KEY in rootSchema ? rootSchema[ID_KEY] : void 0) {
355
391
  const recurseList = [];
356
- return findSchemaDefinitionRecursive($ref, rootSchema, recurseList);
392
+ return findSchemaDefinitionRecursive($ref, rootSchema, recurseList, baseURI);
357
393
  }
358
394
 
359
395
  // src/getDiscriminatorFieldFromSchema.ts
@@ -512,7 +548,7 @@ function getFirstMatchingOption(validator, formData, options, rootSchema, discri
512
548
  }
513
549
 
514
550
  // src/schema/retrieveSchema.ts
515
- var import_isEmpty = __toESM(require("lodash/isEmpty"), 1);
551
+ var import_isEmpty2 = __toESM(require("lodash/isEmpty"), 1);
516
552
  function retrieveSchema(validator, schema, rootSchema = {}, rawFormData, experimental_customMergeAllOf) {
517
553
  return retrieveSchemaInternal(
518
554
  validator,
@@ -678,7 +714,7 @@ function resolveReference(validator, schema, rootSchema, expandAllBranches, recu
678
714
  }
679
715
  return [schema];
680
716
  }
681
- function resolveAllReferences(schema, rootSchema, recurseList) {
717
+ function resolveAllReferences(schema, rootSchema, recurseList, baseURI) {
682
718
  if (!isObject(schema)) {
683
719
  return schema;
684
720
  }
@@ -689,8 +725,11 @@ function resolveAllReferences(schema, rootSchema, recurseList) {
689
725
  return resolvedSchema;
690
726
  }
691
727
  recurseList.push($ref);
692
- const refSchema = findSchemaDefinition($ref, rootSchema);
728
+ const refSchema = findSchemaDefinition($ref, rootSchema, baseURI);
693
729
  resolvedSchema = { ...refSchema, ...localSchema };
730
+ if (ID_KEY in resolvedSchema) {
731
+ baseURI = resolvedSchema[ID_KEY];
732
+ }
694
733
  }
695
734
  if (PROPERTIES_KEY in resolvedSchema) {
696
735
  const childrenLists = [];
@@ -698,7 +737,7 @@ function resolveAllReferences(schema, rootSchema, recurseList) {
698
737
  resolvedSchema[PROPERTIES_KEY],
699
738
  (result, value, key) => {
700
739
  const childList = [...recurseList];
701
- result[key] = resolveAllReferences(value, rootSchema, childList);
740
+ result[key] = resolveAllReferences(value, rootSchema, childList, baseURI);
702
741
  childrenLists.push(childList);
703
742
  },
704
743
  {}
@@ -709,7 +748,7 @@ function resolveAllReferences(schema, rootSchema, recurseList) {
709
748
  if (ITEMS_KEY in resolvedSchema && !Array.isArray(resolvedSchema.items) && typeof resolvedSchema.items !== "boolean") {
710
749
  resolvedSchema = {
711
750
  ...resolvedSchema,
712
- items: resolveAllReferences(resolvedSchema.items, rootSchema, recurseList)
751
+ items: resolveAllReferences(resolvedSchema.items, rootSchema, recurseList, baseURI)
713
752
  };
714
753
  }
715
754
  return deepEquals(schema, resolvedSchema) ? schema : resolvedSchema;
@@ -726,7 +765,7 @@ function stubExistingAdditionalProperties(validator, theSchema, rootSchema, aFor
726
765
  }
727
766
  if (PATTERN_PROPERTIES_KEY in schema) {
728
767
  const matchingProperties = getMatchingPatternProperties(schema, key);
729
- if (!(0, import_isEmpty.default)(matchingProperties)) {
768
+ if (!(0, import_isEmpty2.default)(matchingProperties)) {
730
769
  schema.properties[key] = retrieveSchema(
731
770
  validator,
732
771
  { allOf: Object.values(matchingProperties) },
@@ -816,7 +855,10 @@ function retrieveSchemaInternal(validator, schema, rootSchema, rawFormData, expa
816
855
  resolvedSchema = { ...resolvedSchema, allOf: withoutContainsSchemas };
817
856
  }
818
857
  resolvedSchema = experimental_customMergeAllOf ? experimental_customMergeAllOf(resolvedSchema) : (0, import_json_schema_merge_allof.default)(resolvedSchema, {
819
- deep: false
858
+ deep: false,
859
+ resolvers: {
860
+ $defs: import_json_schema_merge_allof.default.options.resolvers.definitions
861
+ }
820
862
  });
821
863
  if (withContainsSchemas.length) {
822
864
  resolvedSchema.allOf = withContainsSchemas;
@@ -831,7 +873,7 @@ function retrieveSchemaInternal(validator, schema, rootSchema, rawFormData, expa
831
873
  resolvedSchema = Object.keys(resolvedSchema.properties).reduce(
832
874
  (schema2, key) => {
833
875
  const matchingProperties = getMatchingPatternProperties(schema2, key);
834
- if (!(0, import_isEmpty.default)(matchingProperties)) {
876
+ if (!(0, import_isEmpty2.default)(matchingProperties)) {
835
877
  schema2.properties[key] = retrieveSchema(
836
878
  validator,
837
879
  { allOf: [schema2.properties[key], ...Object.values(matchingProperties)] },
@@ -1055,13 +1097,13 @@ function findSelectedOptionInXxxOf(validator, rootSchema, schema, fallbackField,
1055
1097
  // src/schema/getFromSchema.ts
1056
1098
  var import_get6 = __toESM(require("lodash/get"), 1);
1057
1099
  var import_has2 = __toESM(require("lodash/has"), 1);
1058
- var import_isEmpty2 = __toESM(require("lodash/isEmpty"), 1);
1100
+ var import_isEmpty3 = __toESM(require("lodash/isEmpty"), 1);
1059
1101
  function getFromSchemaInternal(validator, rootSchema, schema, path, experimental_customMergeAllOf) {
1060
1102
  let fieldSchema = schema;
1061
1103
  if ((0, import_has2.default)(schema, REF_KEY)) {
1062
1104
  fieldSchema = retrieveSchema(validator, schema, rootSchema, void 0, experimental_customMergeAllOf);
1063
1105
  }
1064
- if ((0, import_isEmpty2.default)(path)) {
1106
+ if ((0, import_isEmpty3.default)(path)) {
1065
1107
  return fieldSchema;
1066
1108
  }
1067
1109
  const pathList = Array.isArray(path) ? path : path.split(".");
@@ -1174,13 +1216,13 @@ function findFieldInSchema(validator, rootSchema, schema, path, formData = {}, e
1174
1216
 
1175
1217
  // src/schema/getDefaultFormState.ts
1176
1218
  var import_get11 = __toESM(require("lodash/get"), 1);
1177
- var import_isEmpty3 = __toESM(require("lodash/isEmpty"), 1);
1219
+ var import_isEmpty4 = __toESM(require("lodash/isEmpty"), 1);
1178
1220
 
1179
1221
  // src/schema/getClosestMatchingOption.ts
1180
1222
  var import_get8 = __toESM(require("lodash/get"), 1);
1181
1223
  var import_has4 = __toESM(require("lodash/has"), 1);
1182
1224
  var import_isNumber2 = __toESM(require("lodash/isNumber"), 1);
1183
- var import_isObject5 = __toESM(require("lodash/isObject"), 1);
1225
+ var import_isObject6 = __toESM(require("lodash/isObject"), 1);
1184
1226
  var import_isString2 = __toESM(require("lodash/isString"), 1);
1185
1227
  var import_reduce = __toESM(require("lodash/reduce"), 1);
1186
1228
  var import_times2 = __toESM(require("lodash/times"), 1);
@@ -1196,7 +1238,7 @@ var JUNK_OPTION = {
1196
1238
  function calculateIndexScore(validator, rootSchema, schema, formData, experimental_customMergeAllOf) {
1197
1239
  let totalScore = 0;
1198
1240
  if (schema) {
1199
- if ((0, import_isObject5.default)(schema.properties)) {
1241
+ if ((0, import_isObject6.default)(schema.properties)) {
1200
1242
  totalScore += (0, import_reduce.default)(
1201
1243
  schema.properties,
1202
1244
  (score, value, key) => {
@@ -1234,7 +1276,7 @@ function calculateIndexScore(validator, rootSchema, schema, formData, experiment
1234
1276
  );
1235
1277
  }
1236
1278
  if (value.type === "object") {
1237
- if ((0, import_isObject5.default)(formValue)) {
1279
+ if ((0, import_isObject6.default)(formValue)) {
1238
1280
  score += 1;
1239
1281
  }
1240
1282
  return score + calculateIndexScore(validator, rootSchema, value, formValue, experimental_customMergeAllOf);
@@ -1503,10 +1545,10 @@ function maybeAddDefaultToObject(obj, key, computedDefault, includeUndefinedValu
1503
1545
  const isSelfOrParentRequired = isParentRequired === void 0 ? requiredFields.includes(key) : isParentRequired;
1504
1546
  if (isObject(computedDefault)) {
1505
1547
  if (emptyObjectFields === "skipEmptyDefaults") {
1506
- if (!(0, import_isEmpty3.default)(computedDefault)) {
1548
+ if (!(0, import_isEmpty4.default)(computedDefault)) {
1507
1549
  obj[key] = computedDefault;
1508
1550
  }
1509
- } else if ((!(0, import_isEmpty3.default)(computedDefault) || requiredFields.includes(key)) && (isSelfOrParentRequired || emptyObjectFields !== "populateRequiredDefaults")) {
1551
+ } else if ((!(0, import_isEmpty4.default)(computedDefault) || requiredFields.includes(key)) && (isSelfOrParentRequired || emptyObjectFields !== "populateRequiredDefaults")) {
1510
1552
  obj[key] = computedDefault;
1511
1553
  }
1512
1554
  } else if (
@@ -1542,7 +1584,7 @@ function computeDefaults(validator, rawSchema, computeDefaultsProps = {}) {
1542
1584
  defaults = schema[CONST_KEY];
1543
1585
  } else if (isObject(defaults) && isObject(schema.default)) {
1544
1586
  defaults = mergeObjects(defaults, schema.default);
1545
- } else if (DEFAULT_KEY in schema && !schema[ANY_OF_KEY] && !schema[ONE_OF_KEY]) {
1587
+ } else if (DEFAULT_KEY in schema && !schema[ANY_OF_KEY] && !schema[ONE_OF_KEY] && !schema[REF_KEY]) {
1546
1588
  defaults = schema.default;
1547
1589
  } else if (REF_KEY in schema) {
1548
1590
  const refName = schema[REF_KEY];
@@ -1550,6 +1592,9 @@ function computeDefaults(validator, rawSchema, computeDefaultsProps = {}) {
1550
1592
  updatedRecurseList = _recurseList.concat(refName);
1551
1593
  schemaToCompute = findSchemaDefinition(refName, rootSchema);
1552
1594
  }
1595
+ if (schemaToCompute && !defaults) {
1596
+ defaults = schema.default;
1597
+ }
1553
1598
  } else if (DEPENDENCIES_KEY in schema) {
1554
1599
  const defaultFormData = {
1555
1600
  ...getDefaultBasedOnSchemaType(validator, schema, computeDefaultsProps, defaults),
@@ -3231,7 +3276,7 @@ function utcToLocal(jsonDate) {
3231
3276
  }
3232
3277
 
3233
3278
  // src/validationDataMerge.ts
3234
- var import_isEmpty4 = __toESM(require("lodash/isEmpty"), 1);
3279
+ var import_isEmpty5 = __toESM(require("lodash/isEmpty"), 1);
3235
3280
  function validationDataMerge(validationData, additionalErrorSchema) {
3236
3281
  if (!additionalErrorSchema) {
3237
3282
  return validationData;
@@ -3239,7 +3284,7 @@ function validationDataMerge(validationData, additionalErrorSchema) {
3239
3284
  const { errors: oldErrors, errorSchema: oldErrorSchema } = validationData;
3240
3285
  let errors = toErrorList(additionalErrorSchema);
3241
3286
  let errorSchema = additionalErrorSchema;
3242
- if (!(0, import_isEmpty4.default)(oldErrorSchema)) {
3287
+ if (!(0, import_isEmpty5.default)(oldErrorSchema)) {
3243
3288
  errorSchema = mergeObjects(oldErrorSchema, additionalErrorSchema, true);
3244
3289
  errors = [...oldErrors].concat(errors);
3245
3290
  }
@@ -3247,7 +3292,7 @@ function validationDataMerge(validationData, additionalErrorSchema) {
3247
3292
  }
3248
3293
 
3249
3294
  // src/withIdRefPrefix.ts
3250
- var import_isObject10 = __toESM(require("lodash/isObject"), 1);
3295
+ var import_isObject11 = __toESM(require("lodash/isObject"), 1);
3251
3296
  function withIdRefPrefixObject(node) {
3252
3297
  for (const key in node) {
3253
3298
  const realObj = node;
@@ -3270,7 +3315,7 @@ function withIdRefPrefix(schemaNode) {
3270
3315
  if (Array.isArray(schemaNode)) {
3271
3316
  return withIdRefPrefixArray([...schemaNode]);
3272
3317
  }
3273
- if ((0, import_isObject10.default)(schemaNode)) {
3318
+ if ((0, import_isObject11.default)(schemaNode)) {
3274
3319
  return withIdRefPrefixObject({ ...schemaNode });
3275
3320
  }
3276
3321
  return schemaNode;