@critical-path/core 0.36.0 → 0.37.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.
Files changed (214) hide show
  1. package/README.md +1 -1
  2. package/dist/domain/authorization.d.ts.map +1 -1
  3. package/dist/domain/authorization.js +10 -7
  4. package/dist/domain/authorization.js.map +1 -1
  5. package/dist/engine/index.d.ts +6 -0
  6. package/dist/engine/index.d.ts.map +1 -1
  7. package/dist/engine/index.js +28 -2
  8. package/dist/engine/index.js.map +1 -1
  9. package/dist/schemas/index.d.ts +42 -12
  10. package/dist/schemas/index.d.ts.map +1 -1
  11. package/dist/schemas/index.js +4 -1
  12. package/dist/schemas/index.js.map +1 -1
  13. package/dist/store/firebase.d.ts +2 -2
  14. package/dist/store/firebase.d.ts.map +1 -1
  15. package/dist/store/firebase.js +4 -2
  16. package/dist/store/firebase.js.map +1 -1
  17. package/dist/store/index.d.ts +7 -2
  18. package/dist/store/index.d.ts.map +1 -1
  19. package/dist/store/index.js +3 -2
  20. package/dist/store/index.js.map +1 -1
  21. package/dist/store/sqlite.d.ts +2 -2
  22. package/dist/store/sqlite.d.ts.map +1 -1
  23. package/dist/store/sqlite.js +5 -3
  24. package/dist/store/sqlite.js.map +1 -1
  25. package/dist/testing/conformance.d.ts.map +1 -1
  26. package/dist/testing/conformance.js +9 -0
  27. package/dist/testing/conformance.js.map +1 -1
  28. package/dist/types/index.d.ts +13 -2
  29. package/dist/types/index.d.ts.map +1 -1
  30. package/package.json +5 -1
  31. package/CHANGELOG.md +0 -593
  32. package/dist/deliverables.test.d.ts +0 -2
  33. package/dist/deliverables.test.d.ts.map +0 -1
  34. package/dist/deliverables.test.js +0 -232
  35. package/dist/deliverables.test.js.map +0 -1
  36. package/dist/domain/calendar.test.d.ts +0 -2
  37. package/dist/domain/calendar.test.d.ts.map +0 -1
  38. package/dist/domain/calendar.test.js +0 -181
  39. package/dist/domain/calendar.test.js.map +0 -1
  40. package/dist/domain/cpm.test.d.ts +0 -2
  41. package/dist/domain/cpm.test.d.ts.map +0 -1
  42. package/dist/domain/cpm.test.js +0 -166
  43. package/dist/domain/cpm.test.js.map +0 -1
  44. package/dist/domain/ddd.test.d.ts +0 -2
  45. package/dist/domain/ddd.test.d.ts.map +0 -1
  46. package/dist/domain/ddd.test.js +0 -349
  47. package/dist/domain/ddd.test.js.map +0 -1
  48. package/dist/domain/ladder.test.d.ts +0 -2
  49. package/dist/domain/ladder.test.d.ts.map +0 -1
  50. package/dist/domain/ladder.test.js +0 -213
  51. package/dist/domain/ladder.test.js.map +0 -1
  52. package/dist/domain/metrics.test.d.ts +0 -2
  53. package/dist/domain/metrics.test.d.ts.map +0 -1
  54. package/dist/domain/metrics.test.js +0 -252
  55. package/dist/domain/metrics.test.js.map +0 -1
  56. package/dist/domain/task-duration.test.d.ts +0 -2
  57. package/dist/domain/task-duration.test.d.ts.map +0 -1
  58. package/dist/domain/task-duration.test.js +0 -307
  59. package/dist/domain/task-duration.test.js.map +0 -1
  60. package/dist/domain/workload.test.d.ts +0 -2
  61. package/dist/domain/workload.test.d.ts.map +0 -1
  62. package/dist/domain/workload.test.js +0 -261
  63. package/dist/domain/workload.test.js.map +0 -1
  64. package/dist/engine/actor.test.d.ts +0 -2
  65. package/dist/engine/actor.test.d.ts.map +0 -1
  66. package/dist/engine/actor.test.js +0 -54
  67. package/dist/engine/actor.test.js.map +0 -1
  68. package/dist/engine/authorization.test.d.ts +0 -2
  69. package/dist/engine/authorization.test.d.ts.map +0 -1
  70. package/dist/engine/authorization.test.js +0 -154
  71. package/dist/engine/authorization.test.js.map +0 -1
  72. package/dist/engine/cascade.test.d.ts +0 -2
  73. package/dist/engine/cascade.test.d.ts.map +0 -1
  74. package/dist/engine/cascade.test.js +0 -99
  75. package/dist/engine/cascade.test.js.map +0 -1
  76. package/dist/engine/config.test.d.ts +0 -2
  77. package/dist/engine/config.test.d.ts.map +0 -1
  78. package/dist/engine/config.test.js +0 -46
  79. package/dist/engine/config.test.js.map +0 -1
  80. package/dist/engine/entity-events.test.d.ts +0 -2
  81. package/dist/engine/entity-events.test.d.ts.map +0 -1
  82. package/dist/engine/entity-events.test.js +0 -61
  83. package/dist/engine/entity-events.test.js.map +0 -1
  84. package/dist/index.test.d.ts +0 -2
  85. package/dist/index.test.d.ts.map +0 -1
  86. package/dist/index.test.js +0 -429
  87. package/dist/index.test.js.map +0 -1
  88. package/dist/plugins/plugins.test.d.ts +0 -2
  89. package/dist/plugins/plugins.test.d.ts.map +0 -1
  90. package/dist/plugins/plugins.test.js +0 -137
  91. package/dist/plugins/plugins.test.js.map +0 -1
  92. package/dist/schemas/schemas.test.d.ts +0 -2
  93. package/dist/schemas/schemas.test.d.ts.map +0 -1
  94. package/dist/schemas/schemas.test.js +0 -58
  95. package/dist/schemas/schemas.test.js.map +0 -1
  96. package/dist/status.test.d.ts +0 -2
  97. package/dist/status.test.d.ts.map +0 -1
  98. package/dist/status.test.js +0 -295
  99. package/dist/status.test.js.map +0 -1
  100. package/dist/storage/storage.test.d.ts +0 -2
  101. package/dist/storage/storage.test.d.ts.map +0 -1
  102. package/dist/storage/storage.test.js +0 -236
  103. package/dist/storage/storage.test.js.map +0 -1
  104. package/dist/storage/upload-limits.test.d.ts +0 -2
  105. package/dist/storage/upload-limits.test.d.ts.map +0 -1
  106. package/dist/storage/upload-limits.test.js +0 -31
  107. package/dist/storage/upload-limits.test.js.map +0 -1
  108. package/dist/store/conformance.test.d.ts +0 -2
  109. package/dist/store/conformance.test.d.ts.map +0 -1
  110. package/dist/store/conformance.test.js +0 -15
  111. package/dist/store/conformance.test.js.map +0 -1
  112. package/dist/store/firebase.test.d.ts +0 -2
  113. package/dist/store/firebase.test.d.ts.map +0 -1
  114. package/dist/store/firebase.test.js +0 -103
  115. package/dist/store/firebase.test.js.map +0 -1
  116. package/dist/store/query.test.d.ts +0 -2
  117. package/dist/store/query.test.d.ts.map +0 -1
  118. package/dist/store/query.test.js +0 -89
  119. package/dist/store/query.test.js.map +0 -1
  120. package/dist/store/sqlite.test.d.ts +0 -2
  121. package/dist/store/sqlite.test.d.ts.map +0 -1
  122. package/dist/store/sqlite.test.js +0 -177
  123. package/dist/store/sqlite.test.js.map +0 -1
  124. package/dist/task-state.test.d.ts +0 -2
  125. package/dist/task-state.test.d.ts.map +0 -1
  126. package/dist/task-state.test.js +0 -423
  127. package/dist/task-state.test.js.map +0 -1
  128. package/dist/use-case-validation.test.d.ts +0 -2
  129. package/dist/use-case-validation.test.d.ts.map +0 -1
  130. package/dist/use-case-validation.test.js +0 -281
  131. package/dist/use-case-validation.test.js.map +0 -1
  132. package/dist/utils/fractional-index.test.d.ts +0 -2
  133. package/dist/utils/fractional-index.test.d.ts.map +0 -1
  134. package/dist/utils/fractional-index.test.js +0 -182
  135. package/dist/utils/fractional-index.test.js.map +0 -1
  136. package/dist/utils/key.test.d.ts +0 -2
  137. package/dist/utils/key.test.d.ts.map +0 -1
  138. package/dist/utils/key.test.js +0 -65
  139. package/dist/utils/key.test.js.map +0 -1
  140. package/dist/utils/mentions.test.d.ts +0 -2
  141. package/dist/utils/mentions.test.d.ts.map +0 -1
  142. package/dist/utils/mentions.test.js +0 -114
  143. package/dist/utils/mentions.test.js.map +0 -1
  144. package/dist/webhooks/webhooks.test.d.ts +0 -2
  145. package/dist/webhooks/webhooks.test.d.ts.map +0 -1
  146. package/dist/webhooks/webhooks.test.js +0 -154
  147. package/dist/webhooks/webhooks.test.js.map +0 -1
  148. package/src/deliverables.test.ts +0 -284
  149. package/src/domain/authorization.ts +0 -82
  150. package/src/domain/calendar.test.ts +0 -216
  151. package/src/domain/calendar.ts +0 -502
  152. package/src/domain/cpm.test.ts +0 -189
  153. package/src/domain/cpm.ts +0 -258
  154. package/src/domain/custom-fields.ts +0 -172
  155. package/src/domain/ddd.test.ts +0 -442
  156. package/src/domain/entities.ts +0 -798
  157. package/src/domain/errors.ts +0 -38
  158. package/src/domain/events.ts +0 -369
  159. package/src/domain/graph.ts +0 -70
  160. package/src/domain/index.ts +0 -12
  161. package/src/domain/ladder.test.ts +0 -251
  162. package/src/domain/ladder.ts +0 -474
  163. package/src/domain/metrics.test.ts +0 -288
  164. package/src/domain/metrics.ts +0 -497
  165. package/src/domain/task-duration.test.ts +0 -354
  166. package/src/domain/workload.test.ts +0 -311
  167. package/src/domain/workload.ts +0 -457
  168. package/src/engine/actor.test.ts +0 -63
  169. package/src/engine/authorization.test.ts +0 -199
  170. package/src/engine/cascade.test.ts +0 -121
  171. package/src/engine/config.test.ts +0 -51
  172. package/src/engine/entity-events.test.ts +0 -67
  173. package/src/engine/index.ts +0 -2495
  174. package/src/index.test.ts +0 -514
  175. package/src/index.ts +0 -9
  176. package/src/plugins/index.ts +0 -103
  177. package/src/plugins/plugins.test.ts +0 -161
  178. package/src/schemas/index.ts +0 -402
  179. package/src/schemas/schemas.test.ts +0 -116
  180. package/src/status.test.ts +0 -354
  181. package/src/storage/file-storage.ts +0 -200
  182. package/src/storage/firebase-storage.ts +0 -209
  183. package/src/storage/index.ts +0 -3
  184. package/src/storage/s3.ts +0 -151
  185. package/src/storage/storage.test.ts +0 -274
  186. package/src/storage/upload-limits.test.ts +0 -41
  187. package/src/store/conformance.test.ts +0 -15
  188. package/src/store/firebase.test.ts +0 -131
  189. package/src/store/firebase.ts +0 -658
  190. package/src/store/index.ts +0 -623
  191. package/src/store/query.test.ts +0 -110
  192. package/src/store/query.ts +0 -113
  193. package/src/store/sqlite.test.ts +0 -218
  194. package/src/store/sqlite.ts +0 -1480
  195. package/src/task-state.test.ts +0 -506
  196. package/src/testing/conformance.ts +0 -319
  197. package/src/testing/index.ts +0 -1
  198. package/src/types/index.ts +0 -871
  199. package/src/use-case-validation.test.ts +0 -324
  200. package/src/utils/fractional-index.test.ts +0 -228
  201. package/src/utils/fractional-index.ts +0 -254
  202. package/src/utils/index.ts +0 -7
  203. package/src/utils/key.test.ts +0 -76
  204. package/src/utils/key.ts +0 -62
  205. package/src/utils/mentions.test.ts +0 -132
  206. package/src/utils/mentions.ts +0 -131
  207. package/src/utils/status.ts +0 -419
  208. package/src/utils/workflow.ts +0 -306
  209. package/src/webhooks/dispatcher.ts +0 -190
  210. package/src/webhooks/index.ts +0 -2
  211. package/src/webhooks/signature.ts +0 -57
  212. package/src/webhooks/webhooks.test.ts +0 -201
  213. package/tsconfig.json +0 -8
  214. package/tsconfig.tsbuildinfo +0 -1
package/src/domain/cpm.ts DELETED
@@ -1,258 +0,0 @@
1
- import type {
2
- Task,
3
- TaskDependency,
4
- TaskCriticalPathSchedule,
5
- CriticalPathAnalysis,
6
- WorkSchedule
7
- } from '../types/index.js';
8
- import { DEFAULT_WORK_SCHEDULE, addWorkingHours, parseDate } from './calendar.js';
9
-
10
- export function getTaskDurationHours(task: Task): number {
11
- if (typeof task.estimatedHours === 'number' && task.estimatedHours >= 0) {
12
- return task.estimatedHours;
13
- }
14
- if (typeof task.estimatedDurationMinutes === 'number' && task.estimatedDurationMinutes >= 0) {
15
- return task.estimatedDurationMinutes / 60;
16
- }
17
- if (typeof task.actualHours === 'number' && task.actualHours > 0) {
18
- return task.actualHours;
19
- }
20
- // Default to 1 hour for non-empty tasks without explicit estimate
21
- return 1;
22
- }
23
-
24
- export interface CPMOptions {
25
- projectStartDate?: string | Date;
26
- schedule?: WorkSchedule;
27
- }
28
-
29
- /**
30
- * Calculates the Critical Path Method (CPM) schedule for a set of tasks and their dependencies.
31
- * - Forward Pass: computes earlyStart and earlyFinish (in working hours and calendar dates)
32
- * - Backward Pass: computes lateStart, lateFinish, and totalSlack
33
- * - Respects working days, working hours, and holidays via WorkSchedule
34
- * - Identifies critical tasks (totalSlack === 0) that dictate the minimum project duration.
35
- */
36
- export function calculateCPM(
37
- projectId: string,
38
- tasks: Task[],
39
- dependencies: TaskDependency[],
40
- options: CPMOptions = {}
41
- ): CriticalPathAnalysis {
42
- const calculatedAt = new Date().toISOString();
43
- const schedule = options.schedule || DEFAULT_WORK_SCHEDULE;
44
- const projectStartDate = options.projectStartDate ? parseDate(options.projectStartDate) : undefined;
45
-
46
- if (tasks.length === 0) {
47
- return {
48
- projectId,
49
- calculatedAt,
50
- totalDurationHours: 0,
51
- totalWorkingHours: 0,
52
- projectStartDate: projectStartDate ? projectStartDate.toISOString() : undefined,
53
- projectEndDate: projectStartDate ? projectStartDate.toISOString() : undefined,
54
- criticalTaskIds: [],
55
- tasks: []
56
- };
57
- }
58
-
59
- const taskMap = new Map<string, Task>(tasks.map((t) => [t.id, t]));
60
- const taskIds = Array.from(taskMap.keys());
61
-
62
- // Build adjacency graph:
63
- // prerequisites[v] = tasks that v depends on (must finish before v starts)
64
- // successors[u] = tasks that depend on u (can only start after u finishes)
65
- const prerequisites = new Map<string, Set<string>>();
66
- const successors = new Map<string, Set<string>>();
67
-
68
- for (const id of taskIds) {
69
- prerequisites.set(id, new Set<string>());
70
- successors.set(id, new Set<string>());
71
- }
72
-
73
- for (const dep of dependencies) {
74
- let sourceId: string | null = null;
75
- let targetId: string | null = null;
76
-
77
- if (dep.type === 'blocking') {
78
- // taskId depends on dependsOnTaskId (dependsOnTaskId -> taskId)
79
- sourceId = dep.dependsOnTaskId;
80
- targetId = dep.taskId;
81
- } else if (dep.type === 'blocked_by') {
82
- // dependsOnTaskId is blocked by taskId (taskId -> dependsOnTaskId)
83
- sourceId = dep.taskId;
84
- targetId = dep.dependsOnTaskId;
85
- }
86
-
87
- if (sourceId && targetId && taskMap.has(sourceId) && taskMap.has(targetId) && sourceId !== targetId) {
88
- prerequisites.get(targetId)!.add(sourceId);
89
- successors.get(sourceId)!.add(targetId);
90
- }
91
- }
92
-
93
- // Topological sorting via Kahn's algorithm
94
- const inDegree = new Map<string, number>();
95
- for (const id of taskIds) {
96
- inDegree.set(id, prerequisites.get(id)!.size);
97
- }
98
-
99
- const queue: string[] = [];
100
- for (const [id, deg] of inDegree.entries()) {
101
- if (deg === 0) {
102
- queue.push(id);
103
- }
104
- }
105
-
106
- const topoOrder: string[] = [];
107
- while (queue.length > 0) {
108
- const current = queue.shift()!;
109
- topoOrder.push(current);
110
-
111
- for (const next of successors.get(current)!) {
112
- const updatedDeg = inDegree.get(next)! - 1;
113
- inDegree.set(next, updatedDeg);
114
- if (updatedDeg === 0) {
115
- queue.push(next);
116
- }
117
- }
118
- }
119
-
120
- // If there's a cycle or disconnected nodes not processed in topoOrder, append them
121
- if (topoOrder.length < taskIds.length) {
122
- for (const id of taskIds) {
123
- if (!topoOrder.includes(id)) {
124
- topoOrder.push(id);
125
- }
126
- }
127
- }
128
-
129
- // --- FORWARD PASS ---
130
- const earlyStart = new Map<string, number>();
131
- const earlyFinish = new Map<string, number>();
132
-
133
- for (const id of topoOrder) {
134
- const task = taskMap.get(id)!;
135
- const duration = getTaskDurationHours(task);
136
- const prereqs = prerequisites.get(id) || new Set();
137
-
138
- let maxPrereqFinish = 0;
139
- for (const p of prereqs) {
140
- const pFinish = earlyFinish.get(p) ?? 0;
141
- if (pFinish > maxPrereqFinish) {
142
- maxPrereqFinish = pFinish;
143
- }
144
- }
145
-
146
- const es = maxPrereqFinish;
147
- const ef = es + duration;
148
-
149
- earlyStart.set(id, es);
150
- earlyFinish.set(id, ef);
151
- }
152
-
153
- // Total project duration
154
- let totalDurationHours = 0;
155
- for (const ef of earlyFinish.values()) {
156
- if (ef > totalDurationHours) {
157
- totalDurationHours = ef;
158
- }
159
- }
160
-
161
- // --- BACKWARD PASS ---
162
- const lateStart = new Map<string, number>();
163
- const lateFinish = new Map<string, number>();
164
-
165
- // Process in reverse topological order
166
- for (let i = topoOrder.length - 1; i >= 0; i--) {
167
- const id = topoOrder[i];
168
- const task = taskMap.get(id)!;
169
- const duration = getTaskDurationHours(task);
170
- const succs = successors.get(id) || new Set();
171
-
172
- let minSuccLateStart = totalDurationHours;
173
- if (succs.size > 0) {
174
- minSuccLateStart = Infinity;
175
- for (const s of succs) {
176
- const sLateStart = lateStart.get(s);
177
- if (sLateStart !== undefined && sLateStart < minSuccLateStart) {
178
- minSuccLateStart = sLateStart;
179
- }
180
- }
181
- if (!Number.isFinite(minSuccLateStart)) {
182
- minSuccLateStart = totalDurationHours;
183
- }
184
- }
185
-
186
- const lf = minSuccLateStart;
187
- const ls = lf - duration;
188
-
189
- lateFinish.set(id, lf);
190
- lateStart.set(id, ls);
191
- }
192
-
193
- // --- SLACK & CRITICAL PATH ---
194
- const schedules: TaskCriticalPathSchedule[] = [];
195
- const criticalTaskIds: string[] = [];
196
-
197
- for (const id of taskIds) {
198
- const es = earlyStart.get(id) ?? 0;
199
- const ef = earlyFinish.get(id) ?? 0;
200
- const ls = lateStart.get(id) ?? 0;
201
- const lf = lateFinish.get(id) ?? 0;
202
-
203
- const rawSlack = lf - ef;
204
- const totalSlack = Math.round(rawSlack * 100) / 100;
205
- const isCritical = totalSlack <= 0.001;
206
-
207
- if (isCritical) {
208
- criticalTaskIds.push(id);
209
- }
210
-
211
- const task = taskMap.get(id)!;
212
- const duration = getTaskDurationHours(task);
213
-
214
- const earlyStartDate = projectStartDate
215
- ? addWorkingHours(projectStartDate, es, schedule).toISOString()
216
- : undefined;
217
- const earlyFinishDate = projectStartDate
218
- ? addWorkingHours(projectStartDate, ef, schedule).toISOString()
219
- : undefined;
220
- const lateStartDate = projectStartDate
221
- ? addWorkingHours(projectStartDate, ls, schedule).toISOString()
222
- : undefined;
223
- const lateFinishDate = projectStartDate
224
- ? addWorkingHours(projectStartDate, lf, schedule).toISOString()
225
- : undefined;
226
-
227
- schedules.push({
228
- taskId: id,
229
- earlyStart: Math.round(es * 100) / 100,
230
- earlyFinish: Math.round(ef * 100) / 100,
231
- lateStart: Math.round(ls * 100) / 100,
232
- lateFinish: Math.round(lf * 100) / 100,
233
- totalSlack,
234
- isCritical,
235
- durationHours: duration,
236
- slackWorkingHours: totalSlack,
237
- earlyStartDate,
238
- earlyFinishDate,
239
- lateStartDate,
240
- lateFinishDate
241
- });
242
- }
243
-
244
- const projectEndDate = projectStartDate
245
- ? addWorkingHours(projectStartDate, totalDurationHours, schedule).toISOString()
246
- : undefined;
247
-
248
- return {
249
- projectId,
250
- calculatedAt,
251
- totalDurationHours: Math.round(totalDurationHours * 100) / 100,
252
- totalWorkingHours: Math.round(totalDurationHours * 100) / 100,
253
- projectStartDate: projectStartDate ? projectStartDate.toISOString() : undefined,
254
- projectEndDate,
255
- criticalTaskIds,
256
- tasks: schedules
257
- };
258
- }
@@ -1,172 +0,0 @@
1
- import type { CustomFieldDefinition, CustomFieldType } from '../types/index.js';
2
- import { BUILT_IN_CUSTOM_FIELD_TYPES } from '../plugins/index.js';
3
-
4
- export class CustomFieldValidationError extends Error {
5
- public readonly fieldKey: string;
6
- public readonly expectedType?: string;
7
- public readonly receivedValue?: unknown;
8
-
9
- constructor(message: string, fieldKey: string, expectedType?: string, receivedValue?: unknown) {
10
- super(message);
11
- this.name = 'CustomFieldValidationError';
12
- this.fieldKey = fieldKey;
13
- this.expectedType = expectedType;
14
- this.receivedValue = receivedValue;
15
- }
16
- }
17
-
18
- export function validateCustomFieldValues(
19
- definitions: CustomFieldDefinition[] | undefined,
20
- values: Record<string, unknown> | undefined,
21
- customTypes?: ReadonlyMap<string, CustomFieldType>
22
- ): void {
23
- if (!definitions || definitions.length === 0) return;
24
- const customValues = values || {};
25
-
26
- for (const def of definitions) {
27
- const value = customValues[def.key];
28
-
29
- // Check required invariant
30
- if (def.required && (value === undefined || value === null || value === '')) {
31
- throw new CustomFieldValidationError(
32
- `Custom field "${def.label || def.key}" (${def.key}) is required.`,
33
- def.key,
34
- def.type,
35
- value
36
- );
37
- }
38
-
39
- if (value === undefined || value === null) {
40
- continue;
41
- }
42
-
43
- // Type and schema validation
44
- switch (def.type) {
45
- case 'number':
46
- if (typeof value !== 'number' || isNaN(value)) {
47
- throw new CustomFieldValidationError(
48
- `Custom field "${def.label || def.key}" must be a valid number. Received: ${typeof value}`,
49
- def.key,
50
- 'number',
51
- value
52
- );
53
- }
54
- break;
55
-
56
- case 'text':
57
- case 'user':
58
- if (typeof value !== 'string') {
59
- throw new CustomFieldValidationError(
60
- `Custom field "${def.label || def.key}" must be a string. Received: ${typeof value}`,
61
- def.key,
62
- def.type,
63
- value
64
- );
65
- }
66
- break;
67
-
68
- case 'boolean':
69
- if (typeof value !== 'boolean') {
70
- throw new CustomFieldValidationError(
71
- `Custom field "${def.label || def.key}" must be a boolean. Received: ${typeof value}`,
72
- def.key,
73
- 'boolean',
74
- value
75
- );
76
- }
77
- break;
78
-
79
- case 'date':
80
- if (typeof value !== 'string' || isNaN(new Date(value).getTime())) {
81
- throw new CustomFieldValidationError(
82
- `Custom field "${def.label || def.key}" must be a valid ISO date string. Received: ${JSON.stringify(value)}`,
83
- def.key,
84
- 'date',
85
- value
86
- );
87
- }
88
- break;
89
-
90
- case 'single_select':
91
- if (typeof value !== 'string') {
92
- throw new CustomFieldValidationError(
93
- `Custom field "${def.label || def.key}" must be a string option. Received: ${typeof value}`,
94
- def.key,
95
- 'single_select',
96
- value
97
- );
98
- }
99
- if (def.options && def.options.length > 0 && !def.options.includes(value)) {
100
- throw new CustomFieldValidationError(
101
- `Custom field "${def.label || def.key}" value "${value}" is not an allowed option [${def.options.join(', ')}].`,
102
- def.key,
103
- 'single_select',
104
- value
105
- );
106
- }
107
- break;
108
-
109
- case 'multi_select':
110
- if (!Array.isArray(value)) {
111
- throw new CustomFieldValidationError(
112
- `Custom field "${def.label || def.key}" must be an array of string options. Received: ${typeof value}`,
113
- def.key,
114
- 'multi_select',
115
- value
116
- );
117
- }
118
- if (def.options && def.options.length > 0) {
119
- for (const item of value) {
120
- if (typeof item !== 'string' || !def.options.includes(item)) {
121
- throw new CustomFieldValidationError(
122
- `Custom field "${def.label || def.key}" option "${item}" is not allowed in [${def.options.join(', ')}].`,
123
- def.key,
124
- 'multi_select',
125
- item
126
- );
127
- }
128
- }
129
- }
130
- break;
131
- default: {
132
- const custom = customTypes?.get(def.type);
133
- if (!custom) {
134
- throw new CustomFieldValidationError(
135
- `Custom field "${def.label || def.key}" has unknown type "${def.type}". Register it with a plugin's customFieldTypes.`,
136
- def.key,
137
- def.type,
138
- value
139
- );
140
- }
141
- const problem = custom.validate(value, def);
142
- if (problem) {
143
- throw new CustomFieldValidationError(
144
- `Custom field "${def.label || def.key}" ${problem}`,
145
- def.key,
146
- def.type,
147
- value
148
- );
149
- }
150
- }
151
- }
152
- }
153
- }
154
-
155
- /**
156
- * Checks that every definition uses a built-in type or one registered by a plugin, so projects
157
- * cannot be configured with fields that would reject every value.
158
- */
159
- export function validateCustomFieldDefinitions(
160
- definitions: CustomFieldDefinition[] | undefined,
161
- customTypes?: ReadonlyMap<string, CustomFieldType>
162
- ): void {
163
- for (const def of definitions ?? []) {
164
- if (!(BUILT_IN_CUSTOM_FIELD_TYPES as readonly string[]).includes(def.type) && !customTypes?.has(def.type)) {
165
- throw new CustomFieldValidationError(
166
- `Custom field "${def.label || def.key}" has unknown type "${def.type}". Register it with a plugin's customFieldTypes.`,
167
- def.key,
168
- def.type
169
- );
170
- }
171
- }
172
- }