This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
hi, I am new to malli coming from spec and it looks like coerce
is what is equivalent to spec's assert
. Is it correct?
Then I am wondering if coerce
in malli can be turned on and off like spec does by (s/check-asserts true)
.
Otherwise, I am thinking of doing like
(comment
(require '[malli.core :as m])
(def mode (atom true))
(defn my-coerce [definition data]
(if @mode
(m/coerce definition data)
data))
(reset! mode true)
(my-coerce :int "42") ;; thrown
(reset! mode false)
(my-coerce :int "42") ;; "42"
)
though not sure if this is a good practice.You know it should be a date,you want a date,so you turn it into a date before you check if it's valid
It seems coerce
is a larger concept than I thought. What I am trying to do is simply checking type in the middle of a function (so not instrumentation), so I am thinking coerce without transformer. The github readme says
Coercion can be applied without transformer, doing just validation:
(m/coerce :int 42)
; 42
(m/coerce :int "42")
; =throws=> :malli.core/invalid-input {:value "42", :schema :int, :explain {:schema :int, :value "42", :errors ({:path [], :in [], :schema :int, :value "42"})}}
I think m/assert
would make sense. Currently working with a codebase which has both spec and malli, and lot's of spec asserts. So, need that too before can fully migrate to malli.
And, nil
is nowadays a valid transformer, so you can just validate & throw with coerce. m/assert
could use that.
Could you write an issue of that @U022N1DU7GQ?
I am still new to malli but feel malli have more powerful features and is very flexible (data-driven). I also learned that if I want to do registry malli also supports it (so I guess it covers what spec does?)
The bigger question I had is how much/often I should use namespaced keywords in maps but usually I find I personally prefer plain keywords as they just look clean to me. With flexible validation tools like malli, I think I can go with plain keywords for lots of cases.
@U022N1DU7GQ (s/check-asserts true)
do you turn it on or off at runtime, or just set it at startup
s/check-asserts uses a volatile! for on/off switch, but i'm a fan of compile-time switches
I usually turn it on on development and testing phase. On production I turn it off.