-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from Actyx/rku/machine-event-parse
add MachineEvent.Factory.parse() method
- Loading branch information
Showing
10 changed files
with
254 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
|
||
export const importZod = () => ({ | ||
zod: require('zod') as typeof import('zod'), | ||
zodError: require('zod-validation-error') as typeof import('zod-validation-error'), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
|
||
const ZOD = { | ||
zod: (await import('zod').catch( | ||
(e: any) => new Error(`cannot import zod, please install: ${e}`), | ||
)) as typeof import('zod') | Error, | ||
zodError: (await import('zod-validation-error').catch( | ||
(e: any) => new Error(`cannot import zod, please install: ${e}`), | ||
)) as typeof import('zod-validation-error') | Error, | ||
} | ||
|
||
export const importZod = () => { | ||
const { zod, zodError } = ZOD | ||
if (zod instanceof Error) throw zod | ||
if (zodError instanceof Error) throw zodError | ||
return { zod, zodError } | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import * as fs from 'fs' | ||
|
||
const copy = (fromDir: string, intoDir: string) => { | ||
const from = fs.readdirSync(fromDir) | ||
for (const entry of from) { | ||
const stat = fs.statSync(`${fromDir}/${entry}`) | ||
if (stat.isFile()) { | ||
fs.copyFileSync(`${fromDir}/${entry}`, `${intoDir}/${entry}`) | ||
} else if (stat.isDirectory()) { | ||
fs.mkdirSync(`${intoDir}/${entry}`, { recursive: true }) | ||
copy(`${fromDir}/${entry}`, `${intoDir}/${entry}`) | ||
} | ||
} | ||
} | ||
|
||
if (process.argv.length != 3) { | ||
console.error('Usage: node copy-cjs-esm <cjs|esm>') | ||
process.exit(1) | ||
} | ||
|
||
switch (process.argv[2]) { | ||
case 'cjs': | ||
copy('cjs', 'src') | ||
break | ||
case 'esm': | ||
copy('esm', 'src') | ||
break | ||
default: | ||
console.error('Usage: node copy-cjs-esm <cjs|esm>') | ||
process.exit(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
|
||
export const importZod = () => ({ | ||
zod: require('zod') as typeof import('zod'), | ||
zodError: require('zod-validation-error') as typeof import('zod-validation-error'), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { describe, expect, it } from '@jest/globals' | ||
import { MachineEvent } from '../../lib/esm/design/event.js' | ||
import { z } from 'zod' | ||
|
||
describe('MachineEvent', () => { | ||
it('should parse empty payload', () => { | ||
const event = MachineEvent.design('a').withoutPayload() | ||
expect(event.parse(null as any)).toEqual({ | ||
error: 'Event null is not an object', | ||
success: false, | ||
}) | ||
expect(event.parse({} as any)).toEqual({ | ||
error: 'Event type undefined does not match expected type a', | ||
success: false, | ||
}) | ||
expect(event.parse({ type: 'b' } as any)).toEqual({ | ||
error: 'Event type b does not match expected type a', | ||
success: false, | ||
}) | ||
expect(event.parse({ type: 'a' })).toEqual({ | ||
success: true, | ||
event: { type: 'a' }, | ||
}) | ||
}) | ||
|
||
it('should parse non-empty non-zod payload', () => { | ||
const event = MachineEvent.design('a').withPayload<{ a: number }>() | ||
expect(event.parse(null as any)).toEqual({ | ||
error: 'Event null is not an object', | ||
success: false, | ||
}) | ||
expect(event.parse({} as any)).toEqual({ | ||
error: 'Event type undefined does not match expected type a', | ||
success: false, | ||
}) | ||
expect(event.parse({ type: 'b' } as any)).toEqual({ | ||
error: 'Event type b does not match expected type a', | ||
success: false, | ||
}) | ||
expect(event.parse({ type: 'a' } as any)).toEqual({ | ||
success: true, | ||
event: { type: 'a' }, | ||
}) | ||
expect(event.parse({ type: 'a', a: 42 })).toEqual({ | ||
success: true, | ||
event: { type: 'a', a: 42 }, | ||
}) | ||
}) | ||
|
||
it('should parse non-empty zod payload', () => { | ||
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', | ||
success: false, | ||
}) | ||
expect(event.parse({} as any)).toEqual({ | ||
error: 'Event type undefined does not match expected type a', | ||
success: false, | ||
}) | ||
expect(event.parse({ type: 'b' } as any)).toEqual({ | ||
error: 'Event type b does not match expected type a', | ||
success: false, | ||
}) | ||
expect(event.parse({ type: 'a' } as any)).toEqual({ | ||
success: false, | ||
error: 'Validation error: Required at "a"', | ||
}) | ||
expect(event.parse({ type: 'a', a: null } as any)).toEqual({ | ||
success: false, | ||
error: 'Validation error: Expected number, received null at "a"', | ||
}) | ||
expect(event.parse({ type: 'a', a: 42 })).toEqual({ | ||
success: true, | ||
event: { type: 'a', a: 42 }, | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.