Fork me on GitHub
#malli
<
2022-11-21
>
Hankstenberg07:11:06

Hi guys, how can I repeat an element of a tuple one or more times? E.g. I'd like to have a tuple: [:tuple :int :string] and only the string part should occur one or more times. When I try :repeat or anything, the strings get wrapped into a vector. Or is there a better way to get a heterogeneous list like [12 "a" "b" "c" ...]?

1
ikitommi09:11:25

try: [:cat :int [:+ :string]]

1
🙌 1
Hankstenberg09:11:30

Ah, thank you very much!

jeroenvandijk15:11:31

Is there a way to extend a :multi like how clojure.spec allows you to extend a multispec?

jeroenvandijk15:11:58

So far I found that all the keys of the :multi need to be in the body at definition. I would like to add entries later.

jeroenvandijk15:11:48

With a mutable registry and some hackery I can add entries, but it is not very clean

jeroenvandijk15:11:18

I’m looking for something like this where I can register the dispatch type outside of the multi

(let [schema [:schema
              {:registry {:x [:map [:hello :int]]}}
              [:multi {:dispatch (fn [x] [:ref (:type x)])}]]]
  (or (->
       (m/explain schema {:type :x
                          :hello 1
                          })
       (me/humanize))
      :ok)) ;=> ["invalid dispatch value"]

ikitommi16:11:29

You can use references (strings or qualified keys) in multi body, like with map: [:multi {:dispatch :type} :domain/user :domain/order]

ikitommi16:11:14

e.g. just reg the impls into registry reference them with the key

jeroenvandijk16:11:29

Ah I see now. Didn’t try qualified keys

ikitommi16:11:42

there is also an example of lazy registry, could be used too: https://github.com/metosin/malli#lazy-registries

jeroenvandijk16:11:55

But it’s not possible to leave the multi without the key entries, right?

jeroenvandijk16:11:07

I want to extend my schema after the first definition

jeroenvandijk16:11:01

So something like

(def CloudFormation
  (m/schema
    [:multi {:dispatch :Type, :lazy-refs true}]
    {:registry registry}))

ikitommi16:11:33

I would be easy to ad a new key e.g. :`methods` into multi, that is used for the lookups. Would that work?

jeroenvandijk16:11:16

Yeah that would be perfect

jeroenvandijk16:11:44

something like

.. :methods (fn [] @*my-schema)
And then I would change *my-schema somewhere else, right?

jeroenvandijk16:11:43

I think this would more or less cover what multispec can do

ikitommi16:11:51

Please write a PR

ikitommi14:11:40

@U0FT7SRLP I meant an Issue is enough 🙂

jeroenvandijk09:11:04

@U055NJ5CC 😅 I actually tried to implement your suggestion by adding a :methods option. And it works, but after the schema is loaded it doesn't revaluate when the state/multimethod updates. Ill create an issue with an explanation and push what i have so far in a separate draft PR