malli

https://github.com/metosin/malli :malli:
borkdude 2020-10-10T07:48:39.083200Z

@ikitommi I'm reading through the Malli README and found a couple of spelling improvements: Homogenous -> Homogeneous

borkdude 2020-10-10T07:49:07.083600Z

And this sentence: > You can also decomplected maps keys and values using registry references. seems to be grammatically a bit off

ikitommi 2020-10-10T08:13:25.084700Z

@borkdude fixes are most welcome.

borkdude 2020-10-10T08:13:56.085Z

Just passing it here, do what you want with it :)

ikitommi 2020-10-10T08:15:13.085200Z

will fix

ikitommi 2020-10-10T08:17:27.085400Z

fixed

ikitommi 2020-10-10T08:18:08.086100Z

toyed with the declarative schema transformations, which might be useful when defining schemas in EDN:

(require '[malli.core :as m])
(require '[malli.util :as mu])
(require '[malli.error :as me])

(def registry (merge (m/default-schemas) (mu/schemas)))

(def XZ
  (m/schema
    [:select-keys
     [:merge
      [:map [:x int?]]
      [:map [:y int?]]
      [:map [:z int?]]]
     [:x :z]]
    {:registry registry}))

XZ
; [:select-keys
;  [:merge
;   [:map [:x int?]]
;   [:map [:y int?]]
;   [:map [:z int?]]]
;  [:x :z]]

;; get the effective schema
(m/deref XZ)
; [:map [:x int?] [:z int?]]

;; internally uses the pre-computed effective schema
(-> XZ
    (m/explain {:x 1})
    (me/humanize))
; {:z ["missing required key"]}

borkdude 2020-10-10T08:21:08.087100Z

I was wondering, does malli also do the destructuring that spec does e.g. on a sequential regex schema? (s/cat etc)

borkdude 2020-10-10T08:21:27.087300Z

or s/or, etc

borkdude 2020-10-10T08:22:01.087800Z

someone should probably write a comparison/migration page for malli <-> spec :)

👍 1
ikitommi 2020-10-10T08:22:45.088400Z

not yet, the internal api works, but not integrated into malli.core: https://github.com/metosin/malli/issues/180

ikitommi 2020-10-10T08:23:33.088900Z

… and https://github.com/metosin/malli/issues/241 after that.

borkdude 2020-10-10T08:24:01.089400Z

exactly! thanks!

zilti 2020-10-10T18:53:51.090500Z

When creating a validator, how do I use non-core functions in a :fn? Specifically, I want to use clojure.string/blank?

ikitommi 2020-10-10T19:48:42.096700Z

@zilti :fn takes any function as unquoted, so [:fn clojure.string/blank?]. They don't serialize correctly, but work on the same runtime. If you use sci, you need to add custom bindings for the function for all runtimes. I believe str/blank? is part of sci default bindings, so [:fn 'str/blank?] should work too.

ikitommi 2020-10-10T19:49:07.097500Z

also, you can say [:string {:min 1}].

borkdude 2020-10-10T19:49:27.098Z

clojure.string/blank? works in sci by default

👍 2
zilti 2020-10-10T19:49:43.098500Z

Oh, that :min seems to be the most straightforward for my usecase, I'll use that!

ikitommi 2020-10-10T19:52:17.100300Z

:string has good default error messages: https://github.com/metosin/malli/blob/master/test/malli/error_test.cljc#L262-L283

👍 1