malli

https://github.com/metosin/malli :malli:
Zaymon 2021-04-22T01:12:48.005800Z

Hey all. I might be missing something essential here but I don’t fundamentally understand why this won’t compile:

(defn longer-than-5? [x] (<= 5 (count x)))
(def MySchema [:map [:some-key [:and string? longer-than-5?]]])

; :malli.core/invalid-schema {:schema #function[example/longer-than-5?]}
Is there something special about string?, int? ect that makes them more than just a function from a -> bool ?

nilern 2021-04-22T13:12:32.012300Z

What is special is that they are in the m/predicate-schemas registry which is merged into the default registry

Zaymon 2021-04-23T01:37:15.013Z

@nilern That’s interesting. Thank you

Zaymon 2021-04-22T01:36:02.006200Z

Looks like I need to specify the function with [:fn longer-than-5?] !

👌 2
Yevgeni Tsodikov 2021-04-22T09:21:21.008500Z

Hi, I think the strip-extra-keys-transformer removes mandatory fields if there are in an [:or ... ] vector:

(def my-schema [:and
                [:map [:a int?]]
                [:or
                 [:map [:b int?]]
                 [:map [:c int?]]]])
=> #'...
(m/validate my-schema {:a 1})
=> false
(m/validate my-schema {:a 1 :b 1 :c 1})
=> true
(m/decode my-schema {:a 1 :b 1 :c 1} (mt/transformer mt/strip-extra-keys-transformer))
=> {:a 1}
Did I configure the schema wrong? How can I work around it?

ikitommi 2021-04-22T09:25:12.009200Z

maybe:

[:and
 [:map 
  [:a int?]
  [:b {:optional true} int?]
  [:c {:optional true} int?]]
 [:fn {:error/messag "only :a or :b is allowed"}
  (fn [{:keys [b c]}] (not (and b c)))]]
or:
[:or
 [:map
  [:a int?]
  [:b int?]]
 [:map
  [:a int?]
  [:c int?]]]
would those work for you?

Yevgeni Tsodikov 2021-04-22T09:25:58.009300Z

Checking, I want to see how they integrate with reitit ’s swagger docs

ikitommi 2021-04-22T09:26:59.009500Z

you can run from repl:

(malli.swagger/transform
  [:and
   [:map 
    [:a int?]
    [:b {:optional true} int?]
    [:c {:optional true} int?]]
   [:fn {:error/messag "only :a or :b is allowed"}
    (fn [{:keys [b c]}] (not (and b c)))]])
to see what comes out.

ikitommi 2021-04-22T09:27:29.009700Z

swagger doesn’t support anyOf JSON Schema, so the latter will not show in swaggr docs.

ikitommi 2021-04-22T09:27:39.009900Z

openapi has that, but not integrated into reiti.

Dosbol 2021-04-22T09:51:15.010900Z

Hi @ikitommi I am facing interesting behavior with explain. I don't understand why order matters...

Dosbol 2021-04-22T09:53:25.011200Z

(def params-function?
  (malli/schema
   [:function
    [:=> [:cat map? [:maybe map?] map?] map?]]
   {::malli/function-checker malli.generator/function-checker}))

(def next-function?
  (malli/schema
   [:function
    [:=> [:cat map? [:maybe map?] map?] keyword?]]
   {::malli/function-checker malli.generator/function-checker}))

Yevgeni Tsodikov 2021-04-22T10:26:05.011500Z

I’ve used the first option, it worked for me. Thanks!

ikitommi 2021-04-22T13:47:00.012500Z

@dos oh, that’s bad. will fix it.

ikitommi 2021-04-22T14:52:33.012700Z

Fixed in master: https://github.com/metosin/malli/pull/421

👍 1