From 75f783b3324e2ad80ebea80f1a70ffb74ac43278 Mon Sep 17 00:00:00 2001 From: Roland Kuhn Date: Thu, 25 Jan 2024 15:09:04 +0100 Subject: [PATCH] make ParseResult.success a boolean --- machine-runner/src/design/event.ts | 14 ++++++------ machine-runner/tests/esm/design.test.ts | 30 ++++++++++++------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/machine-runner/src/design/event.ts b/machine-runner/src/design/event.ts index 65320e9..b020cfb 100644 --- a/machine-runner/src/design/event.ts +++ b/machine-runner/src/design/event.ts @@ -65,31 +65,31 @@ export namespace MachineEvent { return (event: MachineEvent): ParseResult => { if (typeof event !== 'object' || event === null) { return { - type: 'error', + success: false, error: `Event ${event} is not an object`, } } if (event.type !== key) { return { - type: 'error', + success: false, error: `Event type ${event.type} does not match expected type ${key}`, } } if (!zod) { return { - type: 'success', + success: true, payload: event, } } const result = zod.safeParse(event) if (result.success) { return { - type: 'success', + success: true, payload: result.data, } } else { return { - type: 'error', + success: false, error: fromZodError(result.error).toString(), } } @@ -191,11 +191,11 @@ export namespace MachineEvent { export type ParseResult = | { - type: 'success' + success: true payload: Payload } | { - type: 'error' + success: false error: string } diff --git a/machine-runner/tests/esm/design.test.ts b/machine-runner/tests/esm/design.test.ts index 5c549d0..3d6f982 100644 --- a/machine-runner/tests/esm/design.test.ts +++ b/machine-runner/tests/esm/design.test.ts @@ -8,18 +8,18 @@ describe('MachineEvent', () => { const event = MachineEvent.design('a').withoutPayload() expect(event.parse(null as any)).toEqual({ error: 'Event null is not an object', - type: 'error', + success: false, }) expect(event.parse({} as any)).toEqual({ error: 'Event type undefined does not match expected type a', - type: 'error', + success: false, }) expect(event.parse({ type: 'b' } as any)).toEqual({ error: 'Event type b does not match expected type a', - type: 'error', + success: false, }) expect(event.parse({ type: 'a' })).toEqual({ - type: 'success', + success: true, payload: { type: 'a' }, }) }) @@ -28,22 +28,22 @@ describe('MachineEvent', () => { const event = MachineEvent.design('a').withPayload<{ a: number }>() expect(event.parse(null as any)).toEqual({ error: 'Event null is not an object', - type: 'error', + success: false, }) expect(event.parse({} as any)).toEqual({ error: 'Event type undefined does not match expected type a', - type: 'error', + success: false, }) expect(event.parse({ type: 'b' } as any)).toEqual({ error: 'Event type b does not match expected type a', - type: 'error', + success: false, }) expect(event.parse({ type: 'a' } as any)).toEqual({ - type: 'success', + success: true, payload: { type: 'a' }, }) expect(event.parse({ type: 'a', a: 42 })).toEqual({ - type: 'success', + success: true, payload: { type: 'a', a: 42 }, }) }) @@ -52,26 +52,26 @@ describe('MachineEvent', () => { const event = MachineEvent.design('a').withZod(z.object({ a: z.number() })) expect(event.parse(null as any)).toEqual({ error: 'Event null is not an object', - type: 'error', + success: false, }) expect(event.parse({} as any)).toEqual({ error: 'Event type undefined does not match expected type a', - type: 'error', + success: false, }) expect(event.parse({ type: 'b' } as any)).toEqual({ error: 'Event type b does not match expected type a', - type: 'error', + success: false, }) expect(event.parse({ type: 'a' } as any)).toEqual({ - type: 'error', + success: false, error: 'Validation error: Required at "a"', }) expect(event.parse({ type: 'a', a: null } as any)).toEqual({ - type: 'error', + success: false, error: 'Validation error: Expected number, received null at "a"', }) expect(event.parse({ type: 'a', a: 42 })).toEqual({ - type: 'success', + success: true, payload: { type: 'a', a: 42 }, }) })