-
Notifications
You must be signed in to change notification settings - Fork 14
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 #114 from seasonedcc/decouple-zod
Decouple zod
- Loading branch information
Showing
7 changed files
with
112 additions
and
42 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
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 |
---|---|---|
|
@@ -14,13 +14,6 @@ await build({ | |
deno: true, | ||
undici: true, | ||
}, | ||
mappings: { | ||
'https://deno.land/x/[email protected]/mod.ts': { | ||
name: 'zod', | ||
version: '^3.21.4', | ||
peerDependency: true, | ||
}, | ||
}, | ||
package: { | ||
name: 'domain-functions', | ||
version: pkg.version, | ||
|
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 |
---|---|---|
@@ -1,32 +1,46 @@ | ||
import { | ||
describe, | ||
it, | ||
assertEquals, | ||
assertObjectMatch, | ||
describe, | ||
it, | ||
} from './test-prelude.ts' | ||
import { z } from 'https://deno.land/x/[email protected]/mod.ts' | ||
|
||
import { mdf } from './constructor.ts' | ||
import { | ||
EnvironmentError, | ||
ResultError, | ||
InputError, | ||
InputErrors, | ||
ResultError, | ||
} from './errors.ts' | ||
import type { DomainFunction, SuccessResult } from './types.ts' | ||
import type { Equal, Expect } from './types.test.ts' | ||
|
||
describe('makeDomainFunction', () => { | ||
describe('when it has no input', async () => { | ||
const handler = mdf()(() => 'no input!') | ||
type _R = Expect<Equal<typeof handler, DomainFunction<string>>> | ||
describe('when it has no input', () => { | ||
it('uses zod parser to create parse the input and call the domain function', async () => { | ||
const handler = mdf()(() => 'no input!') | ||
type _R = Expect<Equal<typeof handler, DomainFunction<string>>> | ||
|
||
assertEquals(await handler(), { | ||
success: true, | ||
data: 'no input!', | ||
errors: [], | ||
inputErrors: [], | ||
environmentErrors: [], | ||
assertEquals(await handler(), { | ||
success: true, | ||
data: 'no input!', | ||
errors: [], | ||
inputErrors: [], | ||
environmentErrors: [], | ||
}) | ||
}) | ||
|
||
it('fails gracefully if gets something other than undefined', async () => { | ||
const handler = mdf()(() => 'no input!') | ||
type _R = Expect<Equal<typeof handler, DomainFunction<string>>> | ||
|
||
assertEquals(await handler('some input'), { | ||
success: false, | ||
errors: [], | ||
inputErrors: [{ path: [], message: 'Expected undefined' }], | ||
environmentErrors: [], | ||
}) | ||
}) | ||
}) | ||
|
||
|
@@ -46,6 +60,18 @@ describe('makeDomainFunction', () => { | |
}) | ||
}) | ||
|
||
it('fails gracefully if gets something other than empty record', async () => { | ||
const handler = mdf()(() => 'no input!') | ||
type _R = Expect<Equal<typeof handler, DomainFunction<string>>> | ||
|
||
assertEquals(await handler(undefined, ''), { | ||
success: false, | ||
errors: [], | ||
inputErrors: [], | ||
environmentErrors: [{ path: [], message: 'Expected an object' }], | ||
}) | ||
}) | ||
|
||
it('returns error when parsing fails', async () => { | ||
const parser = z.object({ id: z.preprocess(Number, z.number()) }) | ||
const handler = mdf(parser)(({ id }) => id) | ||
|
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import { z } from 'https://deno.land/x/[email protected]/mod.ts' | ||
|
||
import { | ||
EnvironmentError, | ||
InputError, | ||
|
@@ -8,7 +6,7 @@ import { | |
} from './errors.ts' | ||
import { schemaError, toErrorWithMessage } from './errors.ts' | ||
import { formatSchemaErrors } from './utils.ts' | ||
import type { DomainFunction, Result } from './types.ts' | ||
import type { DomainFunction, ParserSchema, Result } from './types.ts' | ||
|
||
/** | ||
* A functions that turns the result of its callback into a Result object. | ||
|
@@ -89,22 +87,17 @@ async function safeResult<T>(fn: () => T): Promise<Result<T>> { | |
* return { message: `${greeting} ${user.name}` } | ||
* }) | ||
*/ | ||
function makeDomainFunction< | ||
Schema extends z.ZodTypeAny, | ||
EnvSchema extends z.ZodTypeAny, | ||
>(inputSchema?: Schema, environmentSchema?: EnvSchema) { | ||
return function <Output>( | ||
handler: ( | ||
input: z.infer<Schema>, | ||
environment: z.infer<EnvSchema>, | ||
) => Output, | ||
) { | ||
function makeDomainFunction<I, E>( | ||
inputSchema?: ParserSchema<I>, | ||
environmentSchema?: ParserSchema<E>, | ||
) { | ||
return function <Output>(handler: (input: I, environment: E) => Output) { | ||
return function (input, environment = {}) { | ||
return safeResult(async () => { | ||
const envResult = await ( | ||
environmentSchema ?? z.object({}) | ||
environmentSchema ?? objectSchema | ||
).safeParseAsync(environment) | ||
const result = await (inputSchema ?? z.undefined()).safeParseAsync( | ||
const result = await (inputSchema ?? undefinedSchema).safeParseAsync( | ||
input, | ||
) | ||
|
||
|
@@ -118,10 +111,35 @@ function makeDomainFunction< | |
: formatSchemaErrors(envResult.error.issues), | ||
}) | ||
} | ||
return handler(result.data, envResult.data) | ||
return handler(result.data as I, envResult.data as E) | ||
}) | ||
} as DomainFunction<Awaited<Output>> | ||
} | ||
} | ||
|
||
export { safeResult, makeDomainFunction, makeDomainFunction as mdf } | ||
const objectSchema: ParserSchema<Record<PropertyKey, unknown>> = { | ||
safeParseAsync: (data: unknown) => { | ||
if (Object.prototype.toString.call(data) !== '[object Object]') { | ||
return Promise.resolve({ | ||
success: false, | ||
error: { issues: [{ path: [], message: 'Expected an object' }] }, | ||
}) | ||
} | ||
const someRecord = data as Record<PropertyKey, unknown> | ||
return Promise.resolve({ success: true, data: someRecord }) | ||
}, | ||
} | ||
|
||
const undefinedSchema: ParserSchema<undefined> = { | ||
safeParseAsync: (data: unknown) => { | ||
if (data !== undefined) { | ||
return Promise.resolve({ | ||
success: false, | ||
error: { issues: [{ path: [], message: 'Expected undefined' }] }, | ||
}) | ||
} | ||
return Promise.resolve({ success: true, data }) | ||
}, | ||
} | ||
|
||
export { makeDomainFunction, makeDomainFunction as mdf, safeResult } |
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
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 |
---|---|---|
@@ -1,8 +1,6 @@ | ||
import { z } from 'https://deno.land/x/[email protected]/mod.ts' | ||
import type { MergeObjs, ParserIssue, Result, SchemaError, SuccessResult } from './types.ts' | ||
|
||
import type { MergeObjs, Result, SchemaError, SuccessResult } from './types.ts' | ||
|
||
function formatSchemaErrors(errors: z.ZodIssue[]): SchemaError[] { | ||
function formatSchemaErrors(errors: ParserIssue[]): SchemaError[] { | ||
return errors.map((error) => { | ||
const { path, message } = error | ||
return { path: path.map(String), message } | ||
|