@fogpipe/forma-core 0.15.0 → 0.17.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.
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,
@@ -60,6 +61,7 @@ module.exports = __toCommonJS(index_exports);
60
61
  // src/types.ts
61
62
  var ADORNABLE_TYPES = /* @__PURE__ */ new Set([
62
63
  "text",
64
+ "phone",
63
65
  "email",
64
66
  "url",
65
67
  "password",
@@ -79,6 +81,9 @@ function isSelectionField(field) {
79
81
  function isArrayField(field) {
80
82
  return field.type === "array";
81
83
  }
84
+ function isMatrixField(field) {
85
+ return field.type === "matrix";
86
+ }
82
87
  function isDataField(field) {
83
88
  return field.type !== "display";
84
89
  }
@@ -485,6 +490,9 @@ function evaluateFieldVisibility(path, fieldDef, data, context, result) {
485
490
  evaluateArrayItemVisibility(path, fieldDef, arrayData, context, result);
486
491
  }
487
492
  }
493
+ if (isMatrixField(fieldDef) && fieldDef.rows) {
494
+ evaluateMatrixRowVisibility(path, fieldDef, context, result);
495
+ }
488
496
  }
489
497
  function evaluateArrayItemVisibility(arrayPath, fieldDef, arrayData, baseContext, result) {
490
498
  if (!fieldDef.itemFields) return;
@@ -510,6 +518,16 @@ function evaluateArrayItemVisibility(arrayPath, fieldDef, arrayData, baseContext
510
518
  }
511
519
  }
512
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
+ }
513
531
  function isFieldVisible(fieldPath, data, spec, options = {}) {
514
532
  const fieldDef = spec.fields[fieldPath];
515
533
  if (!fieldDef) {
@@ -883,6 +901,16 @@ function validateField(path, value, fieldDef, schemaProperty, spec, data, comput
883
901
  );
884
902
  errors.push(...arrayErrors);
885
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
+ }
886
914
  return errors;
887
915
  }
888
916
  function isEmpty(value) {
@@ -1224,6 +1252,58 @@ function validateArrayItem(arrayPath, index, item, itemFields, itemSchema, spec,
1224
1252
  }
1225
1253
  return errors;
1226
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
+ }
1227
1307
  function validateSingleField(fieldPath, data, spec) {
1228
1308
  const fieldDef = spec.fields[fieldPath];
1229
1309
  if (!fieldDef) {
@@ -1271,6 +1351,7 @@ function validateSingleField(fieldPath, data, spec) {
1271
1351
  isDisplayField,
1272
1352
  isEnabled,
1273
1353
  isFieldVisible,
1354
+ isMatrixField,
1274
1355
  isReadonly,
1275
1356
  isRequired,
1276
1357
  isSelectionField,