Skip to content

Commit

Permalink
Added missing default generic parameters when parsers are not given. …
Browse files Browse the repository at this point in the history
…Also remove the validation from our default undefined parser and simply ignore the input, since that seems more useful on compositions than erroring in case something is passed.
  • Loading branch information
diogob committed Nov 29, 2023
1 parent bf5cb0a commit f6f2c2f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
9 changes: 5 additions & 4 deletions src/constructor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ describe('makeDomainFunction', () => {
})

it('fails gracefully if gets something other than undefined', async () => {
const handler = mdf()(() => 'no input!')
type _R = Expect<Equal<typeof handler, DomainFunction<string>>>
const handler = mdf()((args) => args)
type _R = Expect<Equal<typeof handler, DomainFunction<undefined>>>

assertEquals(await handler('some input'), {
success: false,
success: true,
data: undefined,
errors: [],
inputErrors: [{ path: [], message: 'Expected undefined' }],
inputErrors: [],
environmentErrors: [],
})
})
Expand Down
12 changes: 3 additions & 9 deletions src/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async function safeResult<T>(fn: () => T): Promise<Result<T>> {
* return { message: `${greeting} ${user.name}` }
* })
*/
function makeDomainFunction<I, E>(
function makeDomainFunction<I = undefined, E = Record<PropertyKey, unknown>>(
inputSchema?: ParserSchema<I>,
environmentSchema?: ParserSchema<E>,
) {
Expand Down Expand Up @@ -131,14 +131,8 @@ const objectSchema: ParserSchema<Record<PropertyKey, unknown>> = {
}

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 })
safeParseAsync: (_data: unknown) => {
return Promise.resolve({ success: true, data: undefined })
},
}

Expand Down

0 comments on commit f6f2c2f

Please sign in to comment.