@fogpipe/forma-core 0.16.0 → 0.17.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.cjs CHANGED
@@ -45,6 +45,7 @@ __export(index_exports, {
45
45
  isDisplayField: () => isDisplayField,
46
46
  isEnabled: () => isEnabled,
47
47
  isFieldVisible: () => isFieldVisible,
48
+ isMatrixField: () => isMatrixField,
48
49
  isReadonly: () => isReadonly,
49
50
  isRequired: () => isRequired,
50
51
  isSelectionField: () => isSelectionField,
@@ -80,6 +81,9 @@ function isSelectionField(field) {
80
81
  function isArrayField(field) {
81
82
  return field.type === "array";
82
83
  }
84
+ function isMatrixField(field) {
85
+ return field.type === "matrix";
86
+ }
83
87
  function isDataField(field) {
84
88
  return field.type !== "display";
85
89
  }
@@ -486,6 +490,9 @@ function evaluateFieldVisibility(path, fieldDef, data, context, result) {
486
490
  evaluateArrayItemVisibility(path, fieldDef, arrayData, context, result);
487
491
  }
488
492
  }
493
+ if (isMatrixField(fieldDef) && fieldDef.rows) {
494
+ evaluateMatrixRowVisibility(path, fieldDef, context, result);
495
+ }
489
496
  }
490
497
  function evaluateArrayItemVisibility(arrayPath, fieldDef, arrayData, baseContext, result) {
491
498
  if (!fieldDef.itemFields) return;
@@ -511,6 +518,16 @@ function evaluateArrayItemVisibility(arrayPath, fieldDef, arrayData, baseContext
511
518
  }
512
519
  }
513
520
  }
521
+ function evaluateMatrixRowVisibility(matrixPath, fieldDef, context, result) {
522
+ for (const row of fieldDef.rows) {
523
+ const rowPath = `${matrixPath}.${row.id}`;
524
+ if (row.visibleWhen) {
525
+ result[rowPath] = evaluateBoolean(row.visibleWhen, context);
526
+ } else {
527
+ result[rowPath] = true;
528
+ }
529
+ }
530
+ }
514
531
  function isFieldVisible(fieldPath, data, spec, options = {}) {
515
532
  const fieldDef = spec.fields[fieldPath];
516
533
  if (!fieldDef) {
@@ -884,6 +901,16 @@ function validateField(path, value, fieldDef, schemaProperty, spec, data, comput
884
901
  );
885
902
  errors.push(...arrayErrors);
886
903
  }
904
+ if (fieldDef.type === "matrix" && value !== null && value !== void 0 && typeof value === "object" && !Array.isArray(value)) {
905
+ const matrixErrors = validateMatrix(
906
+ path,
907
+ value,
908
+ fieldDef,
909
+ visibility,
910
+ required
911
+ );
912
+ errors.push(...matrixErrors);
913
+ }
887
914
  return errors;
888
915
  }
889
916
  function isEmpty(value) {
@@ -1225,6 +1252,58 @@ function validateArrayItem(arrayPath, index, item, itemFields, itemSchema, spec,
1225
1252
  }
1226
1253
  return errors;
1227
1254
  }
1255
+ function validateMatrix(path, value, fieldDef, visibility, required) {
1256
+ const errors = [];
1257
+ const label = fieldDef.label ?? path;
1258
+ const validColumnValues = new Set(fieldDef.columns.map((c) => c.value));
1259
+ for (const row of fieldDef.rows) {
1260
+ const rowPath = `${path}.${row.id}`;
1261
+ if (visibility[rowPath] === false) {
1262
+ continue;
1263
+ }
1264
+ const rowValue = value[row.id];
1265
+ if (required && (rowValue === null || rowValue === void 0)) {
1266
+ errors.push({
1267
+ field: rowPath,
1268
+ message: `${row.label} is required`,
1269
+ severity: "error"
1270
+ });
1271
+ continue;
1272
+ }
1273
+ if (rowValue === null || rowValue === void 0) {
1274
+ continue;
1275
+ }
1276
+ if (fieldDef.multiSelect) {
1277
+ if (!Array.isArray(rowValue)) {
1278
+ errors.push({
1279
+ field: rowPath,
1280
+ message: `${row.label} must be a list of selections`,
1281
+ severity: "error"
1282
+ });
1283
+ continue;
1284
+ }
1285
+ for (const item of rowValue) {
1286
+ if (!validColumnValues.has(item)) {
1287
+ errors.push({
1288
+ field: rowPath,
1289
+ message: `${row.label} contains an invalid selection`,
1290
+ severity: "error"
1291
+ });
1292
+ break;
1293
+ }
1294
+ }
1295
+ } else {
1296
+ if (!validColumnValues.has(rowValue)) {
1297
+ errors.push({
1298
+ field: rowPath,
1299
+ message: `${row.label} has an invalid value for ${label}`,
1300
+ severity: "error"
1301
+ });
1302
+ }
1303
+ }
1304
+ }
1305
+ return errors;
1306
+ }
1228
1307
  function validateSingleField(fieldPath, data, spec) {
1229
1308
  const fieldDef = spec.fields[fieldPath];
1230
1309
  if (!fieldDef) {
@@ -1272,6 +1351,7 @@ function validateSingleField(fieldPath, data, spec) {
1272
1351
  isDisplayField,
1273
1352
  isEnabled,
1274
1353
  isFieldVisible,
1354
+ isMatrixField,
1275
1355
  isReadonly,
1276
1356
  isRequired,
1277
1357
  isSelectionField,