Fork me on GitHub
#malli
<
2022-07-30
>
Alejandro14:07:34

Hello. Is there a blog post, a documentation page, anything that discusses motivation behind malli? I'd like to know how spec, plumatic/schema, and malli relate to each other.

valtteri15:07:42

Here’s a video where Tommi explains also the motivation https://www.youtube.com/watch?v=MR83MhWQ61E

☝️ 1
valtteri15:07:34

Comparison and reasoning wrt other tools (schema, spec) starts around 3:30

pinkfrog14:07:55

m/validate returns true/false, what’s the function that throws exception when data is malformed?

pinkfrog14:07:13

The purpose of doing this is, I want to add code like

(m/validate :int foobar)
inside a function, and when foobar is not int, I want an exception is thrown so I can know some thing bad happens in sentry. Another good to have is, the m/validate (equivalent) can be turned on in dev and off in prod.

annarcana14:07:07

You'll most likely want some combination of explain/`humanize`. (https://github.com/metosin/malli#error-messages) I don't believe malli has any function that deliberately throws an exception, by design, but you can always use throw with an Exception whose payload is the malli explain output.

pinkfrog14:07:44

@annapawlicka Maybe I shall just do (assert (m/validate ,,,))

Noah Bogart17:07:18

In the simplest case, this is what I would suggest doing.

Ben Sless15:07:44

Do you want this with decode, too?

Ben Sless15:08:31

you should build something like:

(defn coercer
  [schema options transformer]
  (let [schema (m/schema schema options)
        validator (m/validator schema options)
        decoder (m/decoder schema options transformer)
        explainer (m/explainer schema options)]
    (fn [x success failure]
      (let [ret (decoder x)]
        (if (validator ret)
          (success ret)
          (failure (explainer ret)))))))

thanks3 1
pinkfrog04:08:33

@UK0810AQ2 Say I do not want to define the validators in advance. So is it meaningful to create the validators on the fly or shall I just directly use the m/validate function? Is there any performance discrepancy regarding the two?

Ben Sless05:08:20

Yes, closing over a validator is faster than calling validate every time. In what context are you using this?

pinkfrog05:08:03

Across the codebase, many functions accepts a map but the data type etc is not clear. I want to annotate the functions.

pinkfrog08:08:54

Using function schemas, there is a tendency to spec on all the arguments, but I’d rather spec on one of them.

Ben Sless08:08:02

Why not spec the others as any?

pinkfrog08:08:36

That causes confusion.

Ben Sless08:08:03

I think you don't have to spec all the arguments with this macro, no?