Skip to content

Commit

Permalink
make ParseResult.success a boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
rkuhn committed Jan 25, 2024
1 parent cffd1b4 commit 75f783b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
14 changes: 7 additions & 7 deletions machine-runner/src/design/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,31 +65,31 @@ export namespace MachineEvent {
return (event: MachineEvent<Key, Payload>): ParseResult<Payload> => {
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(),
}
}
Expand Down Expand Up @@ -191,11 +191,11 @@ export namespace MachineEvent {

export type ParseResult<Payload> =
| {
type: 'success'
success: true
payload: Payload
}
| {
type: 'error'
success: false
error: string
}

Expand Down
30 changes: 15 additions & 15 deletions machine-runner/tests/esm/design.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
})
})
Expand All @@ -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 },
})
})
Expand All @@ -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 },
})
})
Expand Down

0 comments on commit 75f783b

Please sign in to comment.