Domain functions that take no input erroring when receiving input #122
Replies: 3 comments 5 replies
-
Hi @jakewtaylor thank you for the well written discussion! For the second question, in your case I'd probably leverage the currying by applying an unknown input to create my other DFs like so: const df = mdf(z.unknown())
const getSomething = df(async () => {
// do something
})
const getSomethingElse = df(async () => {
// do something else
})
const tryGetSomethingOrSomethingElse = branch(
checkSomething,
(canDoIt) => canDoIt ? getSomething : getSomethingElse
); I hope this helps! |
Beta Was this translation helpful? Give feedback.
-
@jakewtaylor the reason why the default is undefined is because the resulting code is simpler and easier to reason about. Perhaps we could ignore the input but I like to avoid adding conditionals as much as possible. Honestly the solution with On another tangential topic, the original design of the I see a few other possible approaches:
const tryGetSomethingOrSomethingElse =
mdf()(
() => fromSuccess(checkSomething)() ? fromSuccess(getSomething)() : fromSuccess(getSomethingElse)()
);
const branchWithoutPiping = (df: DomainFunction<boolean>, dfFalse: DomainFunction, dfTrue: DomainFunction) =>
branch(
checkSomething,
(canDoIt) => pipe(
stripInput,
canDoIt
? dfTrue
: dfFalse
),
); |
Beta Was this translation helpful? Give feedback.
-
Hey @jakewtaylor , just to ping you here: The new version of this lib - which is now called |
Beta Was this translation helpful? Give feedback.
-
Hey folks, I love this package so much, but I'm running into a little bit of a hurdle - which may just be me thinking about things the wrong way, but would like to open a discussion on it.
Basically, I seem to frequently have a problem with a DF that isn't expecting any input erroring because it's receiving something.
Take this (contrived) example;
With this code,
tryGetSomethingOrSomethingElse
always fails, because the boolean response fromcheckSomething
is passed into the next function, and it's expecting undefined! I understand why this is happening, and there's a couple of workarounds;Firstly I tried just adding
z.any()
/z.unknown()
as the schema to the 'get' functions. This works, but seems a little weird having to always specify a schema when the function just isn't expecting any input, it seems a little unergonomic to me.To avoid doing that, what I'm mostly doing is using another DF in the chain to strip out the return, i.e.
and then having my branch do something like this:
which is working for me, but again, it seems a little unnecessary having to do this.
So my question is kind of twofold;
Thanks again for this package!
Beta Was this translation helpful? Give feedback.
All reactions