funcool

A channel for discussing and asking questions about Funcool libraries https://github.com/funcool/
jaen 2016-01-30T10:18:16.000140Z

Maybe I'm misunderstanding how to use applicatives, but here it goes. I have something like (m/<$> validation-fn (m/ok value-to-validate)) and I want it to return (m/fail {:some #{:error}}) when validation-fn fails and (m/ok validated-value) - that is validation-fn could change the constructor from ok to fail if the validation failed; but if I try to return m/ok / m/fail from the validation-fn m/<$> just wraps that again resulting in #<Ok #<Ok ...>> or #<Ok #<Fail ...>>.

jaen 2016-01-30T10:18:54.000141Z

Is there anything that would make sense in such context or am I approaching it from a wrong angle?

jaen 2016-01-30T10:23:04.000142Z

My idea is to do be able to and those validations so they can act on previous values; for example there would be integer? which would validate whether the value is an integer and/or coercer it, so for (m/ok "12") you'll get (m/ok 12) (or (m/fail #{:not-integer}) when not an integer) and then you could pass it to, say between? that would check whether the value fits in some range and either return (m/ok 12) again or (m/fail #{[:not-between min max]}).

jaen 2016-01-30T10:23:12.000143Z

But however I try it, I just seem to nest the wrappers.

jaen 2016-01-30T10:24:47.000144Z

Maybe I should just not try to string it like that, but create separate instances for each of validator and or/ and composition; I'll see if I can come up with something meaningful

jaen 2016-01-30T10:29:15.000145Z

Hah

jaen 2016-01-30T10:29:19.000146Z

Now that I think about it

jaen 2016-01-30T10:29:23.000147Z

It seems I just want a monad

niwinz 2016-01-30T10:29:30.000148Z

I think that you are approaching it in a wrong way, let me explain it

jaen 2016-01-30T10:29:33.000149Z

Sure

niwinz 2016-01-30T10:30:06.000151Z

You ara putting the validation function into the applicative fapply...

niwinz 2016-01-30T10:30:23.000152Z

but is wrong, you should create a bunch of "validation functions" that return ok, or fail

niwinz 2016-01-30T10:30:44.000153Z

and then use the applicative composition for just "join" all validations in one

niwinz 2016-01-30T10:30:49.000154Z

collectiing it output

jaen 2016-01-30T10:32:43.000155Z

Yes, I'm doing something like right now and it works, but it has one downside, unless I'm missing something

niwinz 2016-01-30T10:36:03.000156Z

Sure, we had plans in the past build a validation library basing on the cats applicative foundation, but we havent had enough time for it

niwinz 2016-01-30T10:36:37.000157Z

so I don't know at this moment if the validation impl/approach is good enough for solve all cases

jaen 2016-01-30T10:39:00.000158Z

Yeah, possibly; I probably should have used bouncer or something, but thought this would be fun to try.

niwinz 2016-01-30T10:39:32.000159Z

in fact I'm mayself using bouncer on my applications

niwinz 2016-01-30T10:39:37.000160Z

😛

jaen 2016-01-30T10:42:18.000161Z

Basically my issue is I can do

((integer?) "12")
=> #<Ok #validators.core.ResultContainer{:v 12}>
((between? 10 14) 12)
=> #<Ok #validators.core.ResultContainer{:v 12}>
and it works, but I'm then not sure how to chain those two; so I can use between? on the result of integer? so they can both coerce and validate.

jaen 2016-01-30T10:43:12.000162Z

This all started because I couldn't figure how to make bouncer not short-circuit validations, but maybe it's just wrongheaded and I should just use it, instead of trying to figure it out '

jaen 2016-01-30T10:48:43.000163Z

I'll just probably use bouncer for now and come back to this later when I have more time. Thanks for all the help!