@remoteoss/json-schema-form 0.10.1-beta.0 → 0.11.0-beta.0

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.
@@ -1,8 +1,8 @@
1
1
 
2
2
  /*!
3
3
  Copyright (c) 2024 Remote Technology, Inc.
4
- NPM Package: @remoteoss/json-schema-form@0.10.1-beta.0
5
- Generated: Tue, 23 Jul 2024 21:13:09 GMT
4
+ NPM Package: @remoteoss/json-schema-form@0.11.0-beta.0
5
+ Generated: Thu, 25 Jul 2024 16:43:12 GMT
6
6
 
7
7
  MIT License
8
8
 
@@ -2524,9 +2524,9 @@ var require_mapToArray = __commonJS({
2524
2524
  // node_modules/lodash/_setToArray.js
2525
2525
  var require_setToArray = __commonJS({
2526
2526
  "node_modules/lodash/_setToArray.js"(exports2, module2) {
2527
- function setToArray2(set2) {
2528
- var index = -1, result = Array(set2.size);
2529
- set2.forEach(function(value) {
2527
+ function setToArray2(set3) {
2528
+ var index = -1, result = Array(set3.size);
2529
+ set3.forEach(function(value) {
2530
2530
  result[++index] = value;
2531
2531
  });
2532
2532
  return result;
@@ -3489,10 +3489,10 @@ var require_merge = __commonJS({
3489
3489
  var require_set = __commonJS({
3490
3490
  "node_modules/lodash/set.js"(exports2, module2) {
3491
3491
  var baseSet = require_baseSet();
3492
- function set2(object2, path, value) {
3492
+ function set3(object2, path, value) {
3493
3493
  return object2 == null ? object2 : baseSet(object2, path, value);
3494
3494
  }
3495
- module2.exports = set2;
3495
+ module2.exports = set3;
3496
3496
  }
3497
3497
  });
3498
3498
 
@@ -6862,6 +6862,168 @@ var require_mapValues = __commonJS({
6862
6862
  }
6863
6863
  });
6864
6864
 
6865
+ // node_modules/lodash/_arrayIncludesWith.js
6866
+ var require_arrayIncludesWith = __commonJS({
6867
+ "node_modules/lodash/_arrayIncludesWith.js"(exports2, module2) {
6868
+ function arrayIncludesWith(array2, value, comparator) {
6869
+ var index = -1, length2 = array2 == null ? 0 : array2.length;
6870
+ while (++index < length2) {
6871
+ if (comparator(value, array2[index])) {
6872
+ return true;
6873
+ }
6874
+ }
6875
+ return false;
6876
+ }
6877
+ module2.exports = arrayIncludesWith;
6878
+ }
6879
+ });
6880
+
6881
+ // node_modules/lodash/_baseDifference.js
6882
+ var require_baseDifference = __commonJS({
6883
+ "node_modules/lodash/_baseDifference.js"(exports2, module2) {
6884
+ var SetCache2 = require_SetCache();
6885
+ var arrayIncludes = require_arrayIncludes();
6886
+ var arrayIncludesWith = require_arrayIncludesWith();
6887
+ var arrayMap2 = require_arrayMap();
6888
+ var baseUnary2 = require_baseUnary();
6889
+ var cacheHas2 = require_cacheHas();
6890
+ var LARGE_ARRAY_SIZE2 = 200;
6891
+ function baseDifference(array2, values2, iteratee, comparator) {
6892
+ var index = -1, includes = arrayIncludes, isCommon = true, length2 = array2.length, result = [], valuesLength = values2.length;
6893
+ if (!length2) {
6894
+ return result;
6895
+ }
6896
+ if (iteratee) {
6897
+ values2 = arrayMap2(values2, baseUnary2(iteratee));
6898
+ }
6899
+ if (comparator) {
6900
+ includes = arrayIncludesWith;
6901
+ isCommon = false;
6902
+ } else if (values2.length >= LARGE_ARRAY_SIZE2) {
6903
+ includes = cacheHas2;
6904
+ isCommon = false;
6905
+ values2 = new SetCache2(values2);
6906
+ }
6907
+ outer:
6908
+ while (++index < length2) {
6909
+ var value = array2[index], computed = iteratee == null ? value : iteratee(value);
6910
+ value = comparator || value !== 0 ? value : 0;
6911
+ if (isCommon && computed === computed) {
6912
+ var valuesIndex = valuesLength;
6913
+ while (valuesIndex--) {
6914
+ if (values2[valuesIndex] === computed) {
6915
+ continue outer;
6916
+ }
6917
+ }
6918
+ result.push(value);
6919
+ } else if (!includes(values2, computed, comparator)) {
6920
+ result.push(value);
6921
+ }
6922
+ }
6923
+ return result;
6924
+ }
6925
+ module2.exports = baseDifference;
6926
+ }
6927
+ });
6928
+
6929
+ // node_modules/lodash/difference.js
6930
+ var require_difference = __commonJS({
6931
+ "node_modules/lodash/difference.js"(exports2, module2) {
6932
+ var baseDifference = require_baseDifference();
6933
+ var baseFlatten = require_baseFlatten();
6934
+ var baseRest = require_baseRest();
6935
+ var isArrayLikeObject = require_isArrayLikeObject();
6936
+ var difference2 = baseRest(function(array2, values2) {
6937
+ return isArrayLikeObject(array2) ? baseDifference(array2, baseFlatten(values2, 1, isArrayLikeObject, true)) : [];
6938
+ });
6939
+ module2.exports = difference2;
6940
+ }
6941
+ });
6942
+
6943
+ // node_modules/lodash/_baseIntersection.js
6944
+ var require_baseIntersection = __commonJS({
6945
+ "node_modules/lodash/_baseIntersection.js"(exports2, module2) {
6946
+ var SetCache2 = require_SetCache();
6947
+ var arrayIncludes = require_arrayIncludes();
6948
+ var arrayIncludesWith = require_arrayIncludesWith();
6949
+ var arrayMap2 = require_arrayMap();
6950
+ var baseUnary2 = require_baseUnary();
6951
+ var cacheHas2 = require_cacheHas();
6952
+ var nativeMin = Math.min;
6953
+ function baseIntersection(arrays, iteratee, comparator) {
6954
+ var includes = comparator ? arrayIncludesWith : arrayIncludes, length2 = arrays[0].length, othLength = arrays.length, othIndex = othLength, caches = Array(othLength), maxLength = Infinity, result = [];
6955
+ while (othIndex--) {
6956
+ var array2 = arrays[othIndex];
6957
+ if (othIndex && iteratee) {
6958
+ array2 = arrayMap2(array2, baseUnary2(iteratee));
6959
+ }
6960
+ maxLength = nativeMin(array2.length, maxLength);
6961
+ caches[othIndex] = !comparator && (iteratee || length2 >= 120 && array2.length >= 120) ? new SetCache2(othIndex && array2) : void 0;
6962
+ }
6963
+ array2 = arrays[0];
6964
+ var index = -1, seen = caches[0];
6965
+ outer:
6966
+ while (++index < length2 && result.length < maxLength) {
6967
+ var value = array2[index], computed = iteratee ? iteratee(value) : value;
6968
+ value = comparator || value !== 0 ? value : 0;
6969
+ if (!(seen ? cacheHas2(seen, computed) : includes(result, computed, comparator))) {
6970
+ othIndex = othLength;
6971
+ while (--othIndex) {
6972
+ var cache = caches[othIndex];
6973
+ if (!(cache ? cacheHas2(cache, computed) : includes(arrays[othIndex], computed, comparator))) {
6974
+ continue outer;
6975
+ }
6976
+ }
6977
+ if (seen) {
6978
+ seen.push(computed);
6979
+ }
6980
+ result.push(value);
6981
+ }
6982
+ }
6983
+ return result;
6984
+ }
6985
+ module2.exports = baseIntersection;
6986
+ }
6987
+ });
6988
+
6989
+ // node_modules/lodash/_castArrayLikeObject.js
6990
+ var require_castArrayLikeObject = __commonJS({
6991
+ "node_modules/lodash/_castArrayLikeObject.js"(exports2, module2) {
6992
+ var isArrayLikeObject = require_isArrayLikeObject();
6993
+ function castArrayLikeObject(value) {
6994
+ return isArrayLikeObject(value) ? value : [];
6995
+ }
6996
+ module2.exports = castArrayLikeObject;
6997
+ }
6998
+ });
6999
+
7000
+ // node_modules/lodash/intersection.js
7001
+ var require_intersection = __commonJS({
7002
+ "node_modules/lodash/intersection.js"(exports2, module2) {
7003
+ var arrayMap2 = require_arrayMap();
7004
+ var baseIntersection = require_baseIntersection();
7005
+ var baseRest = require_baseRest();
7006
+ var castArrayLikeObject = require_castArrayLikeObject();
7007
+ var intersection2 = baseRest(function(arrays) {
7008
+ var mapped = arrayMap2(arrays, castArrayLikeObject);
7009
+ return mapped.length && mapped[0] === arrays[0] ? baseIntersection(mapped) : [];
7010
+ });
7011
+ module2.exports = intersection2;
7012
+ }
7013
+ });
7014
+
7015
+ // node_modules/lodash/mergeWith.js
7016
+ var require_mergeWith = __commonJS({
7017
+ "node_modules/lodash/mergeWith.js"(exports2, module2) {
7018
+ var baseMerge = require_baseMerge();
7019
+ var createAssigner = require_createAssigner();
7020
+ var mergeWith2 = createAssigner(function(object2, source, srcIndex, customizer) {
7021
+ baseMerge(object2, source, srcIndex, customizer);
7022
+ });
7023
+ module2.exports = mergeWith2;
7024
+ }
7025
+ });
7026
+
6865
7027
  // src/createHeadlessForm.js
6866
7028
  var import_get4 = __toESM(require_get());
6867
7029
  var import_isNil3 = __toESM(require_isNil());
@@ -8316,9 +8478,9 @@ function mapToArray(map) {
8316
8478
  var mapToArray_default = mapToArray;
8317
8479
 
8318
8480
  // node_modules/lodash-es/_setToArray.js
8319
- function setToArray(set2) {
8320
- var index = -1, result = Array(set2.size);
8321
- set2.forEach(function(value) {
8481
+ function setToArray(set3) {
8482
+ var index = -1, result = Array(set3.size);
8483
+ set3.forEach(function(value) {
8322
8484
  result[++index] = value;
8323
8485
  });
8324
8486
  return result;
@@ -13151,37 +13313,78 @@ function createHeadlessForm(jsonSchema, customConfig = {}) {
13151
13313
  }
13152
13314
 
13153
13315
  // src/modify.js
13316
+ var import_difference = __toESM(require_difference());
13154
13317
  var import_get5 = __toESM(require_get());
13318
+ var import_intersection = __toESM(require_intersection());
13155
13319
  var import_merge3 = __toESM(require_merge());
13320
+ var import_mergeWith = __toESM(require_mergeWith());
13321
+ var import_set2 = __toESM(require_set());
13322
+ var WARNING_TYPES = {
13323
+ FIELD_TO_CHANGE_NOT_FOUND: "FIELD_TO_CHANGE_NOT_FOUND",
13324
+ ORDER_MISSING_FIELDS: "ORDER_MISSING_FIELDS",
13325
+ FIELD_TO_CREATE_EXISTS: "FIELD_TO_CREATE_EXISTS",
13326
+ PICK_MISSED_FIELD: "PICK_MISSED_FIELD"
13327
+ };
13156
13328
  function shortToFullPath(path) {
13157
13329
  return path.replace(".", ".properties.");
13158
13330
  }
13331
+ function mergeReplaceArray(_, newVal) {
13332
+ return Array.isArray(newVal) ? newVal : void 0;
13333
+ }
13159
13334
  function standardizeAttrs(attrs) {
13160
- const { errorMessage, properties, ...rest } = attrs;
13335
+ const { errorMessage, presentation, properties, ...rest } = attrs;
13161
13336
  return {
13162
13337
  ...rest,
13338
+ ...presentation ? { "x-jsf-presentation": presentation } : {},
13163
13339
  ...errorMessage ? { "x-jsf-errorMessage": errorMessage } : {}
13164
13340
  };
13165
13341
  }
13342
+ function isConditionalReferencingAnyPickedField(condition, fieldsToPick) {
13343
+ const { if: ifCondition, then: thenCondition, else: elseCondition } = condition;
13344
+ const inIf = (0, import_intersection.default)(ifCondition.required, fieldsToPick);
13345
+ if (inIf.length > 0) {
13346
+ return true;
13347
+ }
13348
+ const inThen = (0, import_intersection.default)(thenCondition.required, fieldsToPick) || (0, import_intersection.default)(Object.keys(thenCondition.properties), fieldsToPick);
13349
+ if (inThen.length > 0) {
13350
+ return true;
13351
+ }
13352
+ const inElse = (0, import_intersection.default)(elseCondition.required, fieldsToPick) || (0, import_intersection.default)(Object.keys(elseCondition.properties), fieldsToPick);
13353
+ if (inElse.length > 0) {
13354
+ return true;
13355
+ }
13356
+ return false;
13357
+ }
13166
13358
  function rewriteFields(schema, fieldsConfig) {
13167
13359
  if (!fieldsConfig)
13168
- return null;
13360
+ return { warnings: null };
13361
+ const warnings = [];
13169
13362
  const fieldsToModify = Object.entries(fieldsConfig);
13170
13363
  fieldsToModify.forEach(([shortPath, mutation]) => {
13171
13364
  const fieldPath = shortToFullPath(shortPath);
13172
13365
  if (!(0, import_get5.default)(schema.properties, fieldPath)) {
13366
+ warnings.push({
13367
+ type: WARNING_TYPES.FIELD_TO_CHANGE_NOT_FOUND,
13368
+ message: `Changing field "${shortPath}" was ignored because it does not exist.`
13369
+ });
13173
13370
  return;
13174
13371
  }
13175
13372
  const fieldAttrs = (0, import_get5.default)(schema.properties, fieldPath);
13176
13373
  const fieldChanges = typeof mutation === "function" ? mutation(fieldAttrs) : mutation;
13177
- (0, import_merge3.default)((0, import_get5.default)(schema.properties, fieldPath), {
13178
- ...fieldAttrs,
13179
- ...standardizeAttrs(fieldChanges)
13180
- });
13374
+ (0, import_mergeWith.default)(
13375
+ (0, import_get5.default)(schema.properties, fieldPath),
13376
+ {
13377
+ ...fieldAttrs,
13378
+ ...standardizeAttrs(fieldChanges)
13379
+ },
13380
+ mergeReplaceArray
13381
+ );
13181
13382
  if (fieldChanges.properties) {
13182
- rewriteFields((0, import_get5.default)(schema.properties, fieldPath), fieldChanges.properties);
13383
+ const result = rewriteFields((0, import_get5.default)(schema.properties, fieldPath), fieldChanges.properties);
13384
+ warnings.push(result.warnings);
13183
13385
  }
13184
13386
  });
13387
+ return { warnings: warnings.flat() };
13185
13388
  }
13186
13389
  function rewriteAllFields(schema, configCallback, context) {
13187
13390
  if (!configCallback)
@@ -13189,10 +13392,14 @@ function rewriteAllFields(schema, configCallback, context) {
13189
13392
  const parentName = context?.parent;
13190
13393
  Object.entries(schema.properties).forEach(([fieldName, fieldAttrs]) => {
13191
13394
  const fullName = parentName ? `${parentName}.${fieldName}` : fieldName;
13192
- (0, import_merge3.default)((0, import_get5.default)(schema.properties, fieldName), {
13193
- ...fieldAttrs,
13194
- ...configCallback(fullName, fieldAttrs)
13195
- });
13395
+ (0, import_mergeWith.default)(
13396
+ (0, import_get5.default)(schema.properties, fieldName),
13397
+ {
13398
+ ...fieldAttrs,
13399
+ ...configCallback(fullName, fieldAttrs)
13400
+ },
13401
+ mergeReplaceArray
13402
+ );
13196
13403
  if (fieldAttrs.properties) {
13197
13404
  rewriteAllFields(fieldAttrs, configCallback, {
13198
13405
  parent: fieldName
@@ -13200,11 +13407,154 @@ function rewriteAllFields(schema, configCallback, context) {
13200
13407
  }
13201
13408
  });
13202
13409
  }
13410
+ function reorderFields(schema, configOrder) {
13411
+ if (!configOrder)
13412
+ return { warnings: null };
13413
+ const warnings = [];
13414
+ const originalOrder = schema["x-jsf-order"] || [];
13415
+ const orderConfig = typeof configOrder === "function" ? configOrder(originalOrder) : configOrder;
13416
+ const remaining = (0, import_difference.default)(originalOrder, orderConfig);
13417
+ if (remaining.length > 0) {
13418
+ warnings.push({
13419
+ type: WARNING_TYPES.ORDER_MISSING_FIELDS,
13420
+ message: `Some fields got forgotten in the new order. They were automatically appended: ${remaining.join(
13421
+ ", "
13422
+ )}`
13423
+ });
13424
+ }
13425
+ schema["x-jsf-order"] = [...orderConfig, ...remaining];
13426
+ return { warnings };
13427
+ }
13428
+ function createFields(schema, fieldsConfig) {
13429
+ if (!fieldsConfig)
13430
+ return { warnings: null };
13431
+ const warnings = [];
13432
+ const fieldsToCreate = Object.entries(fieldsConfig);
13433
+ fieldsToCreate.forEach(([shortPath, fieldAttrs]) => {
13434
+ const fieldPath = shortToFullPath(shortPath);
13435
+ if (fieldAttrs.properties) {
13436
+ const result = createFields((0, import_get5.default)(schema.properties, fieldPath), fieldAttrs.properties);
13437
+ warnings.push(result.warnings);
13438
+ }
13439
+ const fieldInSchema = (0, import_get5.default)(schema.properties, fieldPath);
13440
+ if (fieldInSchema) {
13441
+ warnings.push({
13442
+ type: WARNING_TYPES.FIELD_TO_CREATE_EXISTS,
13443
+ message: `Creating field "${shortPath}" was ignored because it already exists.`
13444
+ });
13445
+ return;
13446
+ }
13447
+ const fieldInObjectPath = (0, import_set2.default)({}, fieldPath, standardizeAttrs(fieldAttrs));
13448
+ (0, import_merge3.default)(schema.properties, fieldInObjectPath);
13449
+ });
13450
+ return { warnings: warnings.flat() };
13451
+ }
13452
+ function pickFields(originalSchema, fieldsToPick) {
13453
+ if (!fieldsToPick) {
13454
+ return { schema: originalSchema, warnings: null };
13455
+ }
13456
+ const newSchema = {
13457
+ properties: {}
13458
+ };
13459
+ Object.entries(originalSchema).forEach(([attrKey, attrValue]) => {
13460
+ switch (attrKey) {
13461
+ case "properties":
13462
+ fieldsToPick.forEach((fieldPath) => {
13463
+ (0, import_set2.default)(newSchema.properties, fieldPath, attrValue[fieldPath]);
13464
+ });
13465
+ break;
13466
+ case "x-jsf-order":
13467
+ case "required":
13468
+ newSchema[attrKey] = attrValue.filter((fieldName) => fieldsToPick.includes(fieldName));
13469
+ break;
13470
+ case "allOf": {
13471
+ const newAllOf = originalSchema.allOf.filter(
13472
+ (condition) => isConditionalReferencingAnyPickedField(condition, fieldsToPick)
13473
+ );
13474
+ newSchema[attrKey] = newAllOf;
13475
+ break;
13476
+ }
13477
+ default:
13478
+ newSchema[attrKey] = attrValue;
13479
+ }
13480
+ });
13481
+ let missingFields = {};
13482
+ newSchema.allOf.forEach((condition) => {
13483
+ const { if: ifCondition, then: thenCondition, else: elseCondition } = condition;
13484
+ const index = originalSchema.allOf.indexOf(condition);
13485
+ missingFields = {
13486
+ ...missingFields,
13487
+ ...findMissingFields(ifCondition, {
13488
+ fields: fieldsToPick,
13489
+ path: `allOf[${index}].if`
13490
+ }),
13491
+ ...findMissingFields(thenCondition, {
13492
+ fields: fieldsToPick,
13493
+ path: `allOf[${index}].then`
13494
+ }),
13495
+ ...findMissingFields(elseCondition, {
13496
+ fields: fieldsToPick,
13497
+ path: `allOf[${index}].else`
13498
+ })
13499
+ };
13500
+ });
13501
+ const warnings = [];
13502
+ if (Object.keys(missingFields).length > 0) {
13503
+ Object.entries(missingFields).forEach(([fieldName]) => {
13504
+ (0, import_set2.default)(newSchema.properties, fieldName, originalSchema.properties[fieldName]);
13505
+ });
13506
+ warnings.push({
13507
+ type: WARNING_TYPES.PICK_MISSED_FIELD,
13508
+ message: `The picked fields are in conditionals that refeer other fields. They added automatically: ${Object.keys(
13509
+ missingFields
13510
+ ).map((name) => `"${name}"`).join(", ")}. Check "meta" for more details.`,
13511
+ meta: missingFields
13512
+ });
13513
+ }
13514
+ return { schema: newSchema, warnings };
13515
+ }
13516
+ function findMissingFields(conditional, { fields, path }) {
13517
+ if (!conditional) {
13518
+ return null;
13519
+ }
13520
+ let missingFields = {};
13521
+ conditional.required?.forEach((fieldName) => {
13522
+ if (!fields.includes(fieldName)) {
13523
+ missingFields[fieldName] = {
13524
+ path
13525
+ };
13526
+ }
13527
+ });
13528
+ Object.entries(conditional.properties || []).forEach(([fieldName]) => {
13529
+ if (!fields.includes(fieldName)) {
13530
+ missingFields[fieldName] = { path };
13531
+ }
13532
+ });
13533
+ return missingFields;
13534
+ }
13203
13535
  function modify(originalSchema, config) {
13204
13536
  const schema = JSON.parse(JSON.stringify(originalSchema));
13205
- rewriteFields(schema, config.fields);
13537
+ const resultRewrite = rewriteFields(schema, config.fields);
13206
13538
  rewriteAllFields(schema, config.allFields);
13207
- return schema;
13539
+ const resultCreate = createFields(schema, config.create);
13540
+ const resultPick = pickFields(schema, config.pick);
13541
+ const finalSchema = resultPick.schema;
13542
+ const resultReorder = reorderFields(finalSchema, config.orderRoot);
13543
+ if (!config.muteLogging) {
13544
+ console.warn(
13545
+ "json-schema-form modify(): We highly recommend you to handle/report the returned `warnings` as they highlight possible bugs in your modifications. To mute this log, pass `muteLogging: true` to the config."
13546
+ );
13547
+ }
13548
+ const warnings = [
13549
+ resultRewrite.warnings,
13550
+ resultCreate.warnings,
13551
+ resultPick.warnings,
13552
+ resultReorder.warnings
13553
+ ].flat().filter(Boolean);
13554
+ return {
13555
+ schema: finalSchema,
13556
+ warnings
13557
+ };
13208
13558
  }
13209
13559
  export {
13210
13560
  buildCompleteYupSchema,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remoteoss/json-schema-form",
3
- "version": "0.10.1-beta.0",
3
+ "version": "0.11.0-beta.0",
4
4
  "description": "Headless UI form powered by JSON Schemas",
5
5
  "author": "Remote.com <engineering@remote.com> (https://remote.com/)",
6
6
  "license": "MIT",