This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-07-30
Channels
- # aleph (39)
- # announcements (5)
- # babashka (7)
- # beginners (14)
- # biff (1)
- # clj-kondo (7)
- # clojure (38)
- # clojure-chicago (3)
- # clojure-europe (3)
- # clojure-norway (1)
- # clojurescript (8)
- # cursive (17)
- # data-science (6)
- # defnpodcast (3)
- # emacs (4)
- # figwheel-main (1)
- # honeysql (2)
- # hyperfiddle (2)
- # malli (20)
- # missionary (24)
- # off-topic (27)
- # reagent (4)
- # scittle (11)
- # shadow-cljs (51)
- # spacemacs (1)
- # xtdb (2)
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.
Here’s a video where Tommi explains also the motivation https://www.youtube.com/watch?v=MR83MhWQ61E
m/validate
returns true/false, what’s the function that throws exception when data is malformed?
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.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.
@annapawlicka Maybe I shall just do (assert (m/validate ,,,))
In the simplest case, this is what I would suggest doing.
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)))))))

@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?
Yes, closing over a validator is faster than calling validate every time. In what context are you using this?
Across the codebase, many functions accepts a map but the data type etc is not clear. I want to annotate the functions.
ah, you should have said so, you want this: https://github.com/metosin/malli/blob/master/docs/function-schemas.md#function-schema-metadata