Fork me on GitHub
#malli
<
2021-04-22
>
Zaymon01:04:48

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 ?

Zaymon01:04:02

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

👌 5
nilern13:04:32

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

Zaymon01:04:15

@U4MB6UKDL That’s interesting. Thank you

Yevgeni Tsodikov09:04:21

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?

ikitommi09:04:12

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 Tsodikov09:04:58

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

ikitommi09:04:59

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.

ikitommi09:04:29

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

ikitommi09:04:39

openapi has that, but not integrated into reiti.

Yevgeni Tsodikov10:04:05

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

Dos09:04:15

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

Dos09:04:25

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

ikitommi13:04:00

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