@rjsf/utils 6.0.0-beta.12 → 6.0.0-beta.14

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.
Files changed (47) hide show
  1. package/dist/index.js +100 -23
  2. package/dist/index.js.map +4 -4
  3. package/dist/utils.esm.js +100 -23
  4. package/dist/utils.esm.js.map +4 -4
  5. package/dist/utils.umd.js +98 -24
  6. package/lib/constants.d.ts +1 -0
  7. package/lib/constants.js +1 -0
  8. package/lib/constants.js.map +1 -1
  9. package/lib/createSchemaUtils.js +11 -1
  10. package/lib/createSchemaUtils.js.map +1 -1
  11. package/lib/findSchemaDefinition.d.ts +6 -0
  12. package/lib/findSchemaDefinition.js +44 -3
  13. package/lib/findSchemaDefinition.js.map +1 -1
  14. package/lib/getUiOptions.js +4 -0
  15. package/lib/getUiOptions.js.map +1 -1
  16. package/lib/index.d.ts +3 -1
  17. package/lib/index.js +2 -1
  18. package/lib/index.js.map +1 -1
  19. package/lib/mergeDefaultsWithFormData.js +2 -2
  20. package/lib/mergeDefaultsWithFormData.js.map +1 -1
  21. package/lib/schema/getDefaultFormState.js +10 -2
  22. package/lib/schema/getDefaultFormState.js.map +1 -1
  23. package/lib/schema/getDisplayLabel.js +2 -2
  24. package/lib/schema/getDisplayLabel.js.map +1 -1
  25. package/lib/schema/retrieveSchema.js +2 -2
  26. package/lib/schema/retrieveSchema.js.map +1 -1
  27. package/lib/shallowEquals.d.ts +8 -0
  28. package/lib/shallowEquals.js +36 -0
  29. package/lib/shallowEquals.js.map +1 -0
  30. package/lib/shouldRender.d.ts +8 -2
  31. package/lib/shouldRender.js +17 -2
  32. package/lib/shouldRender.js.map +1 -1
  33. package/lib/tsconfig.tsbuildinfo +1 -1
  34. package/lib/types.d.ts +20 -3
  35. package/package.json +5 -5
  36. package/src/constants.ts +1 -0
  37. package/src/createSchemaUtils.ts +11 -1
  38. package/src/findSchemaDefinition.ts +51 -3
  39. package/src/getUiOptions.ts +4 -0
  40. package/src/index.ts +4 -0
  41. package/src/mergeDefaultsWithFormData.ts +1 -1
  42. package/src/schema/getDefaultFormState.ts +12 -2
  43. package/src/schema/getDisplayLabel.ts +2 -2
  44. package/src/schema/retrieveSchema.ts +2 -2
  45. package/src/shallowEquals.ts +41 -0
  46. package/src/shouldRender.ts +27 -2
  47. package/src/types.ts +23 -3
package/dist/utils.esm.js CHANGED
@@ -75,10 +75,14 @@ var UI_FIELD_KEY = "ui:field";
75
75
  var UI_WIDGET_KEY = "ui:widget";
76
76
  var UI_OPTIONS_KEY = "ui:options";
77
77
  var UI_GLOBAL_OPTIONS_KEY = "ui:globalOptions";
78
+ var JSON_SCHEMA_DRAFT_2019_09 = "https://json-schema.org/draft/2019-09/schema";
78
79
  var JSON_SCHEMA_DRAFT_2020_12 = "https://json-schema.org/draft/2020-12/schema";
79
80
 
80
81
  // src/getUiOptions.ts
81
82
  function getUiOptions(uiSchema = {}, globalOptions = {}) {
83
+ if (!uiSchema) {
84
+ return { ...globalOptions };
85
+ }
82
86
  return Object.keys(uiSchema).filter((key) => key.indexOf("ui:") === 0).reduce(
83
87
  (options, key) => {
84
88
  const value = uiSchema[key];
@@ -177,7 +181,16 @@ function findEmbeddedSchemaRecursive(schema, ref) {
177
181
  return schema;
178
182
  }
179
183
  for (const subSchema of Object.values(schema)) {
180
- if (isObject2(subSchema)) {
184
+ if (Array.isArray(subSchema)) {
185
+ for (const item of subSchema) {
186
+ if (isObject2(item)) {
187
+ const result = findEmbeddedSchemaRecursive(item, ref);
188
+ if (result !== void 0) {
189
+ return result;
190
+ }
191
+ }
192
+ }
193
+ } else if (isObject2(subSchema)) {
181
194
  const result = findEmbeddedSchemaRecursive(subSchema, ref);
182
195
  if (result !== void 0) {
183
196
  return result;
@@ -186,6 +199,23 @@ function findEmbeddedSchemaRecursive(schema, ref) {
186
199
  }
187
200
  return void 0;
188
201
  }
202
+ function makeAllReferencesAbsolute(schema, baseURI) {
203
+ const currentURI = get(schema, ID_KEY, baseURI);
204
+ if (REF_KEY in schema) {
205
+ schema = { ...schema, [REF_KEY]: UriResolver.resolve(currentURI, schema[REF_KEY]) };
206
+ }
207
+ for (const [key, subSchema] of Object.entries(schema)) {
208
+ if (Array.isArray(subSchema)) {
209
+ schema = {
210
+ ...schema,
211
+ [key]: subSchema.map((item) => isObject2(item) ? makeAllReferencesAbsolute(item, currentURI) : item)
212
+ };
213
+ } else if (isObject2(subSchema)) {
214
+ schema = { ...schema, [key]: makeAllReferencesAbsolute(subSchema, currentURI) };
215
+ }
216
+ }
217
+ return schema;
218
+ }
189
219
  function splitKeyElementFromObject(key, object) {
190
220
  const value = object[key];
191
221
  const remaining = omit(object, [key]);
@@ -231,7 +261,11 @@ function findSchemaDefinitionRecursive($ref, rootSchema = {}, recurseList = [],
231
261
  const [remaining, theRef] = splitKeyElementFromObject(REF_KEY, current);
232
262
  const subSchema = findSchemaDefinitionRecursive(theRef, rootSchema, [...recurseList, ref], baseURI);
233
263
  if (Object.keys(remaining).length > 0) {
234
- return { ...remaining, ...subSchema };
264
+ if (rootSchema[SCHEMA_KEY] === JSON_SCHEMA_DRAFT_2019_09 || rootSchema[SCHEMA_KEY] === JSON_SCHEMA_DRAFT_2020_12) {
265
+ return { [ALL_OF_KEY]: [remaining, subSchema] };
266
+ } else {
267
+ return { ...remaining, ...subSchema };
268
+ }
235
269
  }
236
270
  return subSchema;
237
271
  }
@@ -618,7 +652,7 @@ function stubExistingAdditionalProperties(validator, theSchema, rootSchema, aFor
618
652
  if (!isEmpty2(matchingProperties)) {
619
653
  schema.properties[key] = retrieveSchema(
620
654
  validator,
621
- { allOf: Object.values(matchingProperties) },
655
+ { [ALL_OF_KEY]: Object.values(matchingProperties) },
622
656
  rootSchema,
623
657
  get5(formData, [key]),
624
658
  experimental_customMergeAllOf
@@ -633,7 +667,7 @@ function stubExistingAdditionalProperties(validator, theSchema, rootSchema, aFor
633
667
  if (REF_KEY in schema.additionalProperties) {
634
668
  additionalProperties = retrieveSchema(
635
669
  validator,
636
- { $ref: get5(schema.additionalProperties, [REF_KEY]) },
670
+ { [REF_KEY]: get5(schema.additionalProperties, [REF_KEY]) },
637
671
  rootSchema,
638
672
  formData,
639
673
  experimental_customMergeAllOf
@@ -1240,7 +1274,7 @@ function mergeDefaultsWithFormData(defaults, formData, mergeExtraArrayDefaults =
1240
1274
  return acc2;
1241
1275
  }
1242
1276
  acc2[key] = mergeDefaultsWithFormData(
1243
- get10(defaults, key) ?? {},
1277
+ get10(defaults, key),
1244
1278
  keyValue,
1245
1279
  mergeExtraArrayDefaults,
1246
1280
  defaultSupercedesUndefined,
@@ -1400,8 +1434,12 @@ function getInnerSchemaForArrayItem(schema, additionalItems = 0 /* Ignore */, id
1400
1434
  }
1401
1435
  function maybeAddDefaultToObject(obj, key, computedDefault, includeUndefinedValues, isParentRequired, requiredFields = [], experimental_defaultFormStateBehavior = {}, isConst = false) {
1402
1436
  const { emptyObjectFields = "populateAllDefaults" } = experimental_defaultFormStateBehavior;
1403
- if (includeUndefinedValues || isConst) {
1437
+ if (includeUndefinedValues === true || isConst) {
1404
1438
  obj[key] = computedDefault;
1439
+ } else if (includeUndefinedValues === "excludeObjectChildren") {
1440
+ if (!isObject(computedDefault) || !isEmpty4(computedDefault)) {
1441
+ obj[key] = computedDefault;
1442
+ }
1405
1443
  } else if (emptyObjectFields !== "skipDefaults") {
1406
1444
  const isSelfOrParentRequired = isParentRequired === void 0 ? requiredFields.includes(key) : isParentRequired;
1407
1445
  if (isObject(computedDefault)) {
@@ -1831,10 +1869,10 @@ function getDisplayLabel(validator, schema, uiSchema = {}, rootSchema, globalOpt
1831
1869
  if (schemaType === "object") {
1832
1870
  displayLabel = false;
1833
1871
  }
1834
- if (schemaType === "boolean" && !uiSchema[UI_WIDGET_KEY]) {
1872
+ if (schemaType === "boolean" && uiSchema && !uiSchema[UI_WIDGET_KEY]) {
1835
1873
  displayLabel = false;
1836
1874
  }
1837
- if (uiSchema[UI_FIELD_KEY]) {
1875
+ if (uiSchema && uiSchema[UI_FIELD_KEY]) {
1838
1876
  displayLabel = false;
1839
1877
  }
1840
1878
  return displayLabel;
@@ -2160,6 +2198,7 @@ function toPathSchema(validator, schema, name = "", rootSchema, formData, experi
2160
2198
  }
2161
2199
 
2162
2200
  // src/createSchemaUtils.ts
2201
+ import get16 from "lodash/get";
2163
2202
  var SchemaUtils = class {
2164
2203
  /** Constructs the `SchemaUtils` instance with the given `validator` and `rootSchema` stored as instance variables
2165
2204
  *
@@ -2169,7 +2208,11 @@ var SchemaUtils = class {
2169
2208
  * @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
2170
2209
  */
2171
2210
  constructor(validator, rootSchema, experimental_defaultFormStateBehavior, experimental_customMergeAllOf) {
2172
- this.rootSchema = rootSchema;
2211
+ if (rootSchema && rootSchema[SCHEMA_KEY] === JSON_SCHEMA_DRAFT_2020_12) {
2212
+ this.rootSchema = makeAllReferencesAbsolute(rootSchema, get16(rootSchema, ID_KEY, "#"));
2213
+ } else {
2214
+ this.rootSchema = rootSchema;
2215
+ }
2173
2216
  this.validator = validator;
2174
2217
  this.experimental_defaultFormStateBehavior = experimental_defaultFormStateBehavior;
2175
2218
  this.experimental_customMergeAllOf = experimental_customMergeAllOf;
@@ -2500,6 +2543,31 @@ function dateRangeOptions(start, stop) {
2500
2543
  return options;
2501
2544
  }
2502
2545
 
2546
+ // src/shallowEquals.ts
2547
+ function shallowEquals(a, b) {
2548
+ if (Object.is(a, b)) {
2549
+ return true;
2550
+ }
2551
+ if (a == null || b == null) {
2552
+ return false;
2553
+ }
2554
+ if (typeof a !== "object" || typeof b !== "object") {
2555
+ return false;
2556
+ }
2557
+ const keysA = Object.keys(a);
2558
+ const keysB = Object.keys(b);
2559
+ if (keysA.length !== keysB.length) {
2560
+ return false;
2561
+ }
2562
+ for (let i = 0; i < keysA.length; i++) {
2563
+ const key = keysA[i];
2564
+ if (!Object.prototype.hasOwnProperty.call(b, key) || !Object.is(a[key], b[key])) {
2565
+ return false;
2566
+ }
2567
+ }
2568
+ return true;
2569
+ }
2570
+
2503
2571
  // src/replaceStringParameters.ts
2504
2572
  function replaceStringParameters(inputString, params) {
2505
2573
  let output = inputString;
@@ -2572,7 +2640,7 @@ function enumOptionsSelectValue(valueIndex, selected, allEnumOptions = []) {
2572
2640
 
2573
2641
  // src/ErrorSchemaBuilder.ts
2574
2642
  import cloneDeep from "lodash/cloneDeep";
2575
- import get16 from "lodash/get";
2643
+ import get17 from "lodash/get";
2576
2644
  import set3 from "lodash/set";
2577
2645
  import setWith from "lodash/setWith";
2578
2646
  var ErrorSchemaBuilder = class {
@@ -2601,7 +2669,7 @@ var ErrorSchemaBuilder = class {
2601
2669
  */
2602
2670
  getOrCreateErrorBlock(pathOfError) {
2603
2671
  const hasPath = Array.isArray(pathOfError) && pathOfError.length > 0 || typeof pathOfError === "string";
2604
- let errorBlock = hasPath ? get16(this.errorSchema, pathOfError) : this.errorSchema;
2672
+ let errorBlock = hasPath ? get17(this.errorSchema, pathOfError) : this.errorSchema;
2605
2673
  if (!errorBlock && pathOfError) {
2606
2674
  errorBlock = {};
2607
2675
  setWith(this.errorSchema, pathOfError, errorBlock, Object);
@@ -2627,7 +2695,7 @@ var ErrorSchemaBuilder = class {
2627
2695
  */
2628
2696
  addErrors(errorOrList, pathOfError) {
2629
2697
  const errorBlock = this.getOrCreateErrorBlock(pathOfError);
2630
- let errorsList = get16(errorBlock, ERRORS_KEY);
2698
+ let errorsList = get17(errorBlock, ERRORS_KEY);
2631
2699
  if (!Array.isArray(errorsList)) {
2632
2700
  errorsList = [];
2633
2701
  errorBlock[ERRORS_KEY] = errorsList;
@@ -2776,9 +2844,9 @@ function getTemplate(name, registry, uiOptions = {}) {
2776
2844
 
2777
2845
  // src/getTestIds.ts
2778
2846
  import { nanoid } from "nanoid";
2779
- import get17 from "lodash/get";
2847
+ import get18 from "lodash/get";
2780
2848
  function getTestIds() {
2781
- if (typeof process === "undefined" || get17(process, "env.NODE_ENV") !== "test") {
2849
+ if (typeof process === "undefined" || get18(process, "env.NODE_ENV") !== "test") {
2782
2850
  return {};
2783
2851
  }
2784
2852
  const ids = /* @__PURE__ */ new Map();
@@ -2798,7 +2866,7 @@ function getTestIds() {
2798
2866
  // src/getWidget.tsx
2799
2867
  import { createElement } from "react";
2800
2868
  import ReactIs from "react-is";
2801
- import get18 from "lodash/get";
2869
+ import get19 from "lodash/get";
2802
2870
  import set4 from "lodash/set";
2803
2871
  import { jsx } from "react/jsx-runtime";
2804
2872
  var widgetMap = {
@@ -2854,7 +2922,7 @@ var widgetMap = {
2854
2922
  }
2855
2923
  };
2856
2924
  function mergeWidgetOptions(AWidget) {
2857
- let MergedWidget = get18(AWidget, "MergedWidget");
2925
+ let MergedWidget = get19(AWidget, "MergedWidget");
2858
2926
  if (!MergedWidget) {
2859
2927
  const defaultOptions = AWidget.defaultProps && AWidget.defaultProps.options || {};
2860
2928
  MergedWidget = ({ options, ...props }) => {
@@ -2967,14 +3035,14 @@ function localToUTC(dateString) {
2967
3035
  }
2968
3036
 
2969
3037
  // src/lookupFromFormContext.ts
2970
- import get19 from "lodash/get";
3038
+ import get20 from "lodash/get";
2971
3039
  import has6 from "lodash/has";
2972
3040
  function lookupFromFormContext(regOrFc, toLookup, fallback) {
2973
3041
  const lookupPath = [LOOKUP_MAP_NAME];
2974
3042
  if (has6(regOrFc, FORM_CONTEXT_NAME)) {
2975
3043
  lookupPath.unshift(FORM_CONTEXT_NAME);
2976
3044
  }
2977
- return get19(regOrFc, [...lookupPath, toLookup], fallback);
3045
+ return get20(regOrFc, [...lookupPath, toLookup], fallback);
2978
3046
  }
2979
3047
 
2980
3048
  // src/orderProperties.ts
@@ -3055,7 +3123,14 @@ function schemaRequiresTrueValue(schema) {
3055
3123
  }
3056
3124
 
3057
3125
  // src/shouldRender.ts
3058
- function shouldRender(component, nextProps, nextState) {
3126
+ function shouldRender(component, nextProps, nextState, updateStrategy = "customDeep") {
3127
+ if (updateStrategy === "always") {
3128
+ return true;
3129
+ }
3130
+ if (updateStrategy === "shallow") {
3131
+ const { props: props2, state: state2 } = component;
3132
+ return !shallowEquals(props2, nextProps) || !shallowEquals(state2, nextState);
3133
+ }
3059
3134
  const { props, state } = component;
3060
3135
  return !deepEquals(props, nextProps) || !deepEquals(state, nextState);
3061
3136
  }
@@ -3202,7 +3277,7 @@ function withIdRefPrefix(schemaNode) {
3202
3277
  import keys from "lodash/keys";
3203
3278
  import pickBy from "lodash/pickBy";
3204
3279
  import isPlainObject4 from "lodash/isPlainObject";
3205
- import get20 from "lodash/get";
3280
+ import get21 from "lodash/get";
3206
3281
  import difference from "lodash/difference";
3207
3282
  function getChangedFields(a, b) {
3208
3283
  const aIsPlainObject = isPlainObject4(a);
@@ -3215,7 +3290,7 @@ function getChangedFields(a, b) {
3215
3290
  } else if (!aIsPlainObject && bIsPlainObject) {
3216
3291
  return keys(b);
3217
3292
  } else {
3218
- const unequalFields = keys(pickBy(a, (value, key) => !deepEquals(value, get20(b, key))));
3293
+ const unequalFields = keys(pickBy(a, (value, key) => !deepEquals(value, get21(b, key))));
3219
3294
  const diffFields = difference(keys(b), keys(a));
3220
3295
  return [...unequalFields, ...diffFields];
3221
3296
  }
@@ -3260,7 +3335,7 @@ var TranslatableString = /* @__PURE__ */ ((TranslatableString2) => {
3260
3335
  import forEach from "lodash/forEach";
3261
3336
 
3262
3337
  // src/parser/ParserValidator.ts
3263
- import get21 from "lodash/get";
3338
+ import get22 from "lodash/get";
3264
3339
  var ParserValidator = class {
3265
3340
  /** Construct the ParserValidator for the given `rootSchema`. This `rootSchema` will be stashed in the `schemaMap`
3266
3341
  * first.
@@ -3286,7 +3361,7 @@ var ParserValidator = class {
3286
3361
  * @param hash - The hash value at which to map the schema
3287
3362
  */
3288
3363
  addSchema(schema, hash) {
3289
- const key = get21(schema, ID_KEY, hash);
3364
+ const key = get22(schema, ID_KEY, hash);
3290
3365
  const identifiedSchema = { ...schema, [ID_KEY]: key };
3291
3366
  const existing = this.schemaMap[key];
3292
3367
  if (!existing) {
@@ -3393,6 +3468,7 @@ export {
3393
3468
  ID_KEY,
3394
3469
  IF_KEY,
3395
3470
  ITEMS_KEY,
3471
+ JSON_SCHEMA_DRAFT_2019_09,
3396
3472
  JSON_SCHEMA_DRAFT_2020_12,
3397
3473
  JUNK_OPTION_ID,
3398
3474
  LOOKUP_MAP_NAME,
@@ -3480,6 +3556,7 @@ export {
3480
3556
  sanitizeDataForNewSchema,
3481
3557
  schemaParser,
3482
3558
  schemaRequiresTrueValue,
3559
+ shallowEquals,
3483
3560
  shouldRender,
3484
3561
  sortedJSONStringify,
3485
3562
  titleId,