@critical-path/core 0.35.0 → 0.36.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/README.md +11 -0
- package/dist/engine/index.d.ts +2 -0
- package/dist/engine/index.d.ts.map +1 -1
- package/dist/engine/index.js +25 -2
- package/dist/engine/index.js.map +1 -1
- package/dist/types/index.d.ts +11 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +5 -1
- package/CHANGELOG.md +0 -584
- package/dist/deliverables.test.d.ts +0 -2
- package/dist/deliverables.test.d.ts.map +0 -1
- package/dist/deliverables.test.js +0 -232
- package/dist/deliverables.test.js.map +0 -1
- package/dist/domain/calendar.test.d.ts +0 -2
- package/dist/domain/calendar.test.d.ts.map +0 -1
- package/dist/domain/calendar.test.js +0 -181
- package/dist/domain/calendar.test.js.map +0 -1
- package/dist/domain/cpm.test.d.ts +0 -2
- package/dist/domain/cpm.test.d.ts.map +0 -1
- package/dist/domain/cpm.test.js +0 -166
- package/dist/domain/cpm.test.js.map +0 -1
- package/dist/domain/ddd.test.d.ts +0 -2
- package/dist/domain/ddd.test.d.ts.map +0 -1
- package/dist/domain/ddd.test.js +0 -349
- package/dist/domain/ddd.test.js.map +0 -1
- package/dist/domain/ladder.test.d.ts +0 -2
- package/dist/domain/ladder.test.d.ts.map +0 -1
- package/dist/domain/ladder.test.js +0 -213
- package/dist/domain/ladder.test.js.map +0 -1
- package/dist/domain/metrics.test.d.ts +0 -2
- package/dist/domain/metrics.test.d.ts.map +0 -1
- package/dist/domain/metrics.test.js +0 -252
- package/dist/domain/metrics.test.js.map +0 -1
- package/dist/domain/task-duration.test.d.ts +0 -2
- package/dist/domain/task-duration.test.d.ts.map +0 -1
- package/dist/domain/task-duration.test.js +0 -307
- package/dist/domain/task-duration.test.js.map +0 -1
- package/dist/domain/workload.test.d.ts +0 -2
- package/dist/domain/workload.test.d.ts.map +0 -1
- package/dist/domain/workload.test.js +0 -261
- package/dist/domain/workload.test.js.map +0 -1
- package/dist/engine/actor.test.d.ts +0 -2
- package/dist/engine/actor.test.d.ts.map +0 -1
- package/dist/engine/actor.test.js +0 -54
- package/dist/engine/actor.test.js.map +0 -1
- package/dist/engine/authorization.test.d.ts +0 -2
- package/dist/engine/authorization.test.d.ts.map +0 -1
- package/dist/engine/authorization.test.js +0 -154
- package/dist/engine/authorization.test.js.map +0 -1
- package/dist/engine/cascade.test.d.ts +0 -2
- package/dist/engine/cascade.test.d.ts.map +0 -1
- package/dist/engine/cascade.test.js +0 -99
- package/dist/engine/cascade.test.js.map +0 -1
- package/dist/engine/config.test.d.ts +0 -2
- package/dist/engine/config.test.d.ts.map +0 -1
- package/dist/engine/config.test.js +0 -46
- package/dist/engine/config.test.js.map +0 -1
- package/dist/engine/entity-events.test.d.ts +0 -2
- package/dist/engine/entity-events.test.d.ts.map +0 -1
- package/dist/engine/entity-events.test.js +0 -61
- package/dist/engine/entity-events.test.js.map +0 -1
- package/dist/index.test.d.ts +0 -2
- package/dist/index.test.d.ts.map +0 -1
- package/dist/index.test.js +0 -429
- package/dist/index.test.js.map +0 -1
- package/dist/plugins/plugins.test.d.ts +0 -2
- package/dist/plugins/plugins.test.d.ts.map +0 -1
- package/dist/plugins/plugins.test.js +0 -137
- package/dist/plugins/plugins.test.js.map +0 -1
- package/dist/schemas/schemas.test.d.ts +0 -2
- package/dist/schemas/schemas.test.d.ts.map +0 -1
- package/dist/schemas/schemas.test.js +0 -58
- package/dist/schemas/schemas.test.js.map +0 -1
- package/dist/status.test.d.ts +0 -2
- package/dist/status.test.d.ts.map +0 -1
- package/dist/status.test.js +0 -295
- package/dist/status.test.js.map +0 -1
- package/dist/storage/storage.test.d.ts +0 -2
- package/dist/storage/storage.test.d.ts.map +0 -1
- package/dist/storage/storage.test.js +0 -236
- package/dist/storage/storage.test.js.map +0 -1
- package/dist/store/conformance.test.d.ts +0 -2
- package/dist/store/conformance.test.d.ts.map +0 -1
- package/dist/store/conformance.test.js +0 -15
- package/dist/store/conformance.test.js.map +0 -1
- package/dist/store/firebase.test.d.ts +0 -2
- package/dist/store/firebase.test.d.ts.map +0 -1
- package/dist/store/firebase.test.js +0 -103
- package/dist/store/firebase.test.js.map +0 -1
- package/dist/store/query.test.d.ts +0 -2
- package/dist/store/query.test.d.ts.map +0 -1
- package/dist/store/query.test.js +0 -89
- package/dist/store/query.test.js.map +0 -1
- package/dist/store/sqlite.test.d.ts +0 -2
- package/dist/store/sqlite.test.d.ts.map +0 -1
- package/dist/store/sqlite.test.js +0 -177
- package/dist/store/sqlite.test.js.map +0 -1
- package/dist/task-state.test.d.ts +0 -2
- package/dist/task-state.test.d.ts.map +0 -1
- package/dist/task-state.test.js +0 -423
- package/dist/task-state.test.js.map +0 -1
- package/dist/use-case-validation.test.d.ts +0 -2
- package/dist/use-case-validation.test.d.ts.map +0 -1
- package/dist/use-case-validation.test.js +0 -281
- package/dist/use-case-validation.test.js.map +0 -1
- package/dist/utils/fractional-index.test.d.ts +0 -2
- package/dist/utils/fractional-index.test.d.ts.map +0 -1
- package/dist/utils/fractional-index.test.js +0 -182
- package/dist/utils/fractional-index.test.js.map +0 -1
- package/dist/utils/key.test.d.ts +0 -2
- package/dist/utils/key.test.d.ts.map +0 -1
- package/dist/utils/key.test.js +0 -65
- package/dist/utils/key.test.js.map +0 -1
- package/dist/utils/mentions.test.d.ts +0 -2
- package/dist/utils/mentions.test.d.ts.map +0 -1
- package/dist/utils/mentions.test.js +0 -114
- package/dist/utils/mentions.test.js.map +0 -1
- package/dist/webhooks/webhooks.test.d.ts +0 -2
- package/dist/webhooks/webhooks.test.d.ts.map +0 -1
- package/dist/webhooks/webhooks.test.js +0 -154
- package/dist/webhooks/webhooks.test.js.map +0 -1
- package/src/deliverables.test.ts +0 -284
- package/src/domain/authorization.ts +0 -82
- package/src/domain/calendar.test.ts +0 -216
- package/src/domain/calendar.ts +0 -502
- package/src/domain/cpm.test.ts +0 -189
- package/src/domain/cpm.ts +0 -258
- package/src/domain/custom-fields.ts +0 -172
- package/src/domain/ddd.test.ts +0 -442
- package/src/domain/entities.ts +0 -798
- package/src/domain/errors.ts +0 -38
- package/src/domain/events.ts +0 -369
- package/src/domain/graph.ts +0 -70
- package/src/domain/index.ts +0 -12
- package/src/domain/ladder.test.ts +0 -251
- package/src/domain/ladder.ts +0 -474
- package/src/domain/metrics.test.ts +0 -288
- package/src/domain/metrics.ts +0 -497
- package/src/domain/task-duration.test.ts +0 -354
- package/src/domain/workload.test.ts +0 -311
- package/src/domain/workload.ts +0 -457
- package/src/engine/actor.test.ts +0 -63
- package/src/engine/authorization.test.ts +0 -199
- package/src/engine/cascade.test.ts +0 -121
- package/src/engine/config.test.ts +0 -51
- package/src/engine/entity-events.test.ts +0 -67
- package/src/engine/index.ts +0 -2472
- package/src/index.test.ts +0 -514
- package/src/index.ts +0 -9
- package/src/plugins/index.ts +0 -103
- package/src/plugins/plugins.test.ts +0 -161
- package/src/schemas/index.ts +0 -402
- package/src/schemas/schemas.test.ts +0 -116
- package/src/status.test.ts +0 -354
- package/src/storage/file-storage.ts +0 -200
- package/src/storage/firebase-storage.ts +0 -209
- package/src/storage/index.ts +0 -3
- package/src/storage/s3.ts +0 -151
- package/src/storage/storage.test.ts +0 -274
- package/src/store/conformance.test.ts +0 -15
- package/src/store/firebase.test.ts +0 -131
- package/src/store/firebase.ts +0 -658
- package/src/store/index.ts +0 -623
- package/src/store/query.test.ts +0 -110
- package/src/store/query.ts +0 -113
- package/src/store/sqlite.test.ts +0 -218
- package/src/store/sqlite.ts +0 -1480
- package/src/task-state.test.ts +0 -506
- package/src/testing/conformance.ts +0 -319
- package/src/testing/index.ts +0 -1
- package/src/types/index.ts +0 -859
- package/src/use-case-validation.test.ts +0 -324
- package/src/utils/fractional-index.test.ts +0 -228
- package/src/utils/fractional-index.ts +0 -254
- package/src/utils/index.ts +0 -7
- package/src/utils/key.test.ts +0 -76
- package/src/utils/key.ts +0 -62
- package/src/utils/mentions.test.ts +0 -132
- package/src/utils/mentions.ts +0 -131
- package/src/utils/status.ts +0 -419
- package/src/utils/workflow.ts +0 -306
- package/src/webhooks/dispatcher.ts +0 -190
- package/src/webhooks/index.ts +0 -2
- package/src/webhooks/signature.ts +0 -57
- package/src/webhooks/webhooks.test.ts +0 -201
- package/tsconfig.json +0 -8
- 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
|
-
}
|