This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-05-14
Channels
I was trying to write a function schema such that specs of second argument are dependent on the value of first argument e.g.
first argument can be a :keyword
with value :id
or :contact-id
second argument has to be a :map
that needs to contain the key supplied in first argument
So far, I have done below:
[:=>
[:cat
[:alt [:= :id] [:= :contact-id]]
[:alt [:map [:id :string]] [:map [:contact-id :string]]]]
:string]
(defn test-fn
{:malli/schema =>test-fn}
[key value]
(str (name key) "_" (key value)))
However, above allows (test-fn :contact-id {:id "op"})
as a valid case in instrumentation as there is no correlation b/w first and second argument.
I would greatly appreciate some help.
EDIT:
Below schema works:
(def =>take2
[:=>
[:cat
[:alt
[:cat [:= :id] [:map [:id :string]]]
[:cat [:= :contact-id] [:map [:contact-id :string]]]]]
:string])
(defn test-fn-2
{:malli/schema =>take2}
[key value]
(str (name key) "_" (key value)))
(test-fn :contact-id {:id "op"})
is invalid with instrumentation on. It is a little verbose though.