@makehq/forman-schema 1.8.0 → 1.8.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
@@ -89,6 +89,15 @@ function normalizeFormanFieldType(field) {
89
89
  }
90
90
  };
91
91
  }
92
+ function findValueInSelectOptions(field, value, optionsAndGroups) {
93
+ if (!optionsAndGroups) return void 0;
94
+ if (field.grouped) {
95
+ const found2 = optionsAndGroups.flatMap((group) => "options" in group ? group.options : []).find((option) => option.value === value);
96
+ if (found2) return found2;
97
+ }
98
+ const found = optionsAndGroups.find((option) => "value" in option && option.value === value);
99
+ return found;
100
+ }
92
101
  function buildRestoreStructure(items) {
93
102
  const result = {};
94
103
  for (const item of items) {
@@ -463,12 +472,15 @@ function handleSelectOrPathType(field, result, context) {
463
472
  value: appendQueryString(optionsOrGroups, context.domain, context.tail)
464
473
  });
465
474
  } else {
466
- const options = optionsOrGroups?.some(isOptionGroup) ? optionsOrGroups.flatMap(
467
- (group) => group.options.map((option) => ({
468
- ...option,
469
- label: `${group.label}: ${option.label || option.value}`
470
- }))
471
- ) : optionsOrGroups || [];
475
+ const options = optionsOrGroups?.flatMap((optionOrGroup) => {
476
+ if (isOptionGroup(optionOrGroup)) {
477
+ return optionOrGroup.options.map((option) => ({
478
+ ...option,
479
+ label: `${optionOrGroup.label}: ${option.label || option.value}`
480
+ }));
481
+ }
482
+ return optionOrGroup;
483
+ });
472
484
  if (options?.some((option) => {
473
485
  if (isOptionGroup(option)) return option.options.some((option2) => option2.label || option2.nested);
474
486
  return option.label || option.nested;
@@ -1134,9 +1146,11 @@ async function handleSelectType(value, field, context) {
1134
1146
  };
1135
1147
  }
1136
1148
  for (const singleValue of value) {
1137
- const found = field.grouped ? optionsOrGroups.some(
1138
- (group) => group.options.some((option) => option.value === singleValue)
1139
- ) : optionsOrGroups.some((option) => option.value === singleValue);
1149
+ const found = findValueInSelectOptions(
1150
+ field,
1151
+ singleValue,
1152
+ optionsOrGroups
1153
+ );
1140
1154
  if (!found) {
1141
1155
  errors.push({
1142
1156
  domain: context.domain,
@@ -1162,7 +1176,7 @@ async function handleSelectType(value, field, context) {
1162
1176
  }
1163
1177
  }
1164
1178
  } else {
1165
- const item = field.grouped ? optionsOrGroups.find((group) => group.options.some((option) => option.value === value))?.options.find((option) => option.value === value) : optionsOrGroups.find((option) => option.value === value);
1179
+ const item = findValueInSelectOptions(field, value, optionsOrGroups);
1166
1180
  if (!item) {
1167
1181
  return {
1168
1182
  valid: false,
package/dist/index.d.cts CHANGED
@@ -36,7 +36,7 @@ type FormanSchemaField = {
36
36
  /** Default value for the field */
37
37
  default?: FormanSchemaValue;
38
38
  /** Available options for fields which support them */
39
- options?: FormanSchemaOption[] | FormanSchemaOptionGroup[] | FormanSchemaDirectoryOption[] | FormanSchemaPathExtendedOptions | FormanSchemaExtendedOptions | string;
39
+ options?: (FormanSchemaOption | FormanSchemaOptionGroup)[] | FormanSchemaDirectoryOption[] | FormanSchemaPathExtendedOptions | FormanSchemaExtendedOptions | string;
40
40
  /** Help text or description for the field */
41
41
  help?: string;
42
42
  /** Sub-fields specification for collection or array types */
package/dist/index.d.ts CHANGED
@@ -36,7 +36,7 @@ type FormanSchemaField = {
36
36
  /** Default value for the field */
37
37
  default?: FormanSchemaValue;
38
38
  /** Available options for fields which support them */
39
- options?: FormanSchemaOption[] | FormanSchemaOptionGroup[] | FormanSchemaDirectoryOption[] | FormanSchemaPathExtendedOptions | FormanSchemaExtendedOptions | string;
39
+ options?: (FormanSchemaOption | FormanSchemaOptionGroup)[] | FormanSchemaDirectoryOption[] | FormanSchemaPathExtendedOptions | FormanSchemaExtendedOptions | string;
40
40
  /** Help text or description for the field */
41
41
  help?: string;
42
42
  /** Sub-fields specification for collection or array types */
package/dist/index.js CHANGED
@@ -60,6 +60,15 @@ function normalizeFormanFieldType(field) {
60
60
  }
61
61
  };
62
62
  }
63
+ function findValueInSelectOptions(field, value, optionsAndGroups) {
64
+ if (!optionsAndGroups) return void 0;
65
+ if (field.grouped) {
66
+ const found2 = optionsAndGroups.flatMap((group) => "options" in group ? group.options : []).find((option) => option.value === value);
67
+ if (found2) return found2;
68
+ }
69
+ const found = optionsAndGroups.find((option) => "value" in option && option.value === value);
70
+ return found;
71
+ }
63
72
  function buildRestoreStructure(items) {
64
73
  const result = {};
65
74
  for (const item of items) {
@@ -434,12 +443,15 @@ function handleSelectOrPathType(field, result, context) {
434
443
  value: appendQueryString(optionsOrGroups, context.domain, context.tail)
435
444
  });
436
445
  } else {
437
- const options = optionsOrGroups?.some(isOptionGroup) ? optionsOrGroups.flatMap(
438
- (group) => group.options.map((option) => ({
439
- ...option,
440
- label: `${group.label}: ${option.label || option.value}`
441
- }))
442
- ) : optionsOrGroups || [];
446
+ const options = optionsOrGroups?.flatMap((optionOrGroup) => {
447
+ if (isOptionGroup(optionOrGroup)) {
448
+ return optionOrGroup.options.map((option) => ({
449
+ ...option,
450
+ label: `${optionOrGroup.label}: ${option.label || option.value}`
451
+ }));
452
+ }
453
+ return optionOrGroup;
454
+ });
443
455
  if (options?.some((option) => {
444
456
  if (isOptionGroup(option)) return option.options.some((option2) => option2.label || option2.nested);
445
457
  return option.label || option.nested;
@@ -1105,9 +1117,11 @@ async function handleSelectType(value, field, context) {
1105
1117
  };
1106
1118
  }
1107
1119
  for (const singleValue of value) {
1108
- const found = field.grouped ? optionsOrGroups.some(
1109
- (group) => group.options.some((option) => option.value === singleValue)
1110
- ) : optionsOrGroups.some((option) => option.value === singleValue);
1120
+ const found = findValueInSelectOptions(
1121
+ field,
1122
+ singleValue,
1123
+ optionsOrGroups
1124
+ );
1111
1125
  if (!found) {
1112
1126
  errors.push({
1113
1127
  domain: context.domain,
@@ -1133,7 +1147,7 @@ async function handleSelectType(value, field, context) {
1133
1147
  }
1134
1148
  }
1135
1149
  } else {
1136
- const item = field.grouped ? optionsOrGroups.find((group) => group.options.some((option) => option.value === value))?.options.find((option) => option.value === value) : optionsOrGroups.find((option) => option.value === value);
1150
+ const item = findValueInSelectOptions(field, value, optionsOrGroups);
1137
1151
  if (!item) {
1138
1152
  return {
1139
1153
  valid: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@makehq/forman-schema",
3
- "version": "1.8.0",
3
+ "version": "1.8.1",
4
4
  "description": "Forman Schema Tools",
5
5
  "license": "MIT",
6
6
  "author": "Make",