Fork me on GitHub
#malli
<
2023-09-10
>
kokonut12:09:46

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) .

kokonut13:09:24

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.

Ben Sless13:09:11

Coercion needs a transformer

Ben Sless13:09:23

Think about a date coming in as a string

Ben Sless13:09:44

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

kokonut13:09:40

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"})}}

kokonut13:09:08

But the convention is we (almost) always use it with transformers?

ikitommi13:09:29

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.

ikitommi13:09:24

And, nil is nowadays a valid transformer, so you can just validate & throw with coerce. m/assert could use that.

ikitommi13:09:45

Could you write an issue of that @U022N1DU7GQ?

ikitommi13:09:34

and, welcome to malli! Curious to hear how do you perceive the differences

👍 2
kokonut13:09:19

Yes, I was looking for something like m/assert and thought m/coerce would be it.

kokonut13:09:15

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?)

kokonut13:09:07

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.

Ben Sless14:09:32

I think you want validate in that case, not coerce

jasonjckn19:09:31

@U022N1DU7GQ (s/check-asserts true) do you turn it on or off at runtime, or just set it at startup

jasonjckn19:09:24

s/check-asserts uses a volatile! for on/off switch, but i'm a fan of compile-time switches

kokonut19:09:35

I usually turn it on on development and testing phase. On production I turn it off.

👍 2
jasonjckn19:09:15

great, so don't need a volatile! for that

jasonjckn19:09:42

i have ~30 minutes before I have to leave but i'll try to send a malli PR before then

👍 2
gratitude-thank-you 2