Skip to content

Commit 8baa02b

Browse files
committed
chore: use safeParse to catch issues in dataOut
1 parent 8ff2ab9 commit 8baa02b

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

packages/backend/src/apps/toolbox/__tests__/actions/for-each/get-data-out-metadata.test.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('getDataOutMetadata', () => {
1414
errorDetails: {},
1515
status: 'success' as const,
1616
appKey: 'toolbox',
17+
key: 'forEach',
1718
createdAt: '2023-01-01T00:00:00Z',
1819
updatedAt: '2023-01-01T00:00:00Z',
1920
metadata: {},
@@ -561,7 +562,17 @@ describe('getDataOutMetadata', () => {
561562
}
562563
const executionStep = createMockExecutionStep(dataOut)
563564

564-
await expect(getDataOutMetadata(executionStep)).rejects.toThrow()
565+
await expect(getDataOutMetadata(executionStep)).resolves.toBeNull()
566+
})
567+
568+
it('should throw an error when inputSource is null', async () => {
569+
const dataOut = {
570+
iterations: 1,
571+
inputSource: null as any,
572+
items: ['some', 'data'],
573+
}
574+
const executionStep = createMockExecutionStep(dataOut)
575+
await expect(getDataOutMetadata(executionStep)).resolves.toBeNull()
565576
})
566577
})
567578

@@ -574,7 +585,7 @@ describe('getDataOutMetadata', () => {
574585
const executionStep = createMockExecutionStep(dataOut)
575586

576587
// This should throw because dataOutSchema.parse will fail
577-
await expect(getDataOutMetadata(executionStep)).rejects.toThrow()
588+
await expect(getDataOutMetadata(executionStep)).resolves.toBeNull()
578589
})
579590

580591
it('should handle dataOut with missing iterations field', async () => {
@@ -585,7 +596,7 @@ describe('getDataOutMetadata', () => {
585596
}
586597
const executionStep = createMockExecutionStep(dataOut)
587598

588-
await expect(getDataOutMetadata(executionStep)).rejects.toThrow()
599+
await expect(getDataOutMetadata(executionStep)).resolves.toBeNull()
589600
})
590601

591602
it('should handle dataOut with missing items field', async () => {
@@ -596,7 +607,7 @@ describe('getDataOutMetadata', () => {
596607
}
597608
const executionStep = createMockExecutionStep(dataOut)
598609

599-
await expect(getDataOutMetadata(executionStep)).rejects.toThrow()
610+
await expect(getDataOutMetadata(executionStep)).resolves.toBeNull()
600611
})
601612
})
602613

packages/backend/src/apps/toolbox/actions/for-each/get-data-out-metadata.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ async function getDataOutMetadata(
1212
return null
1313
}
1414

15-
const dataOut = dataOutSchema.parse(rawDataOut)
16-
const { inputSource, items } = dataOut
15+
const dataOut = dataOutSchema.safeParse(rawDataOut)
16+
if (dataOut.success === false) {
17+
console.error(dataOut.error)
18+
return null
19+
}
20+
const { inputSource, items } = dataOut.data
1721

1822
const baseMetadata = {
1923
iterations: {
@@ -35,7 +39,7 @@ async function getDataOutMetadata(
3539
item: {
3640
label: 'Item',
3741
type: 'text',
38-
displayedValue: dataOut.items[0] ?? '',
42+
displayedValue: items[0] ?? '',
3943
},
4044
}
4145
}

0 commit comments

Comments
 (0)