Fork me on GitHub
#malli
<
2020-04-08
>
ikitommi04:04:35

I’m not a fond of too much extra syntax for :map. If the spec-like :or or :and are liked, I guess there could be :keys schema for that?

Kevin07:04:08

Thanks! This does exactly what I need. One question for clarification though; When talking about too much extra syntax, do you mean :or / :and or are you referring to my pull request?

ikitommi07:04:32

just referring to the :or and :and , like spec1 has.

ikitommi07:04:57

will check the PRs as soon as have time. busy times.

Kevin07:04:17

Ahh ok thanks. Take your time!

yonatanel12:04:11

I need something similar and even a bit more complex. I want to require a value either at top level or nested:

;Valid:
{:key1 "yo"}

;Also valid
{:nested {:key2 "yo"}}
It’s not the same key but they have the same meaning and one must exist. Currently I have this schema:
[:or
 [:map
  [:key1 any?]
  [:nested {:optional true} [:map [:key2 {:optional true} any?]]]]
 [:map
  [:key1 {:optional true} any?]
  [:nested [:map [:key2 any?]]]]]
but I have more keys like this, and some options can be either of 3 keys so I need 3 maps for each key and then combine all of it with :and.

ikitommi04:04:54

@dcj easiest way to attach a decoder just for that enum would be:

(def Weighting
  [:enum {:decode/enum weighting->kw} :dB-A :dB-C :dB-Z])
and then applying it with:
(m/decode MyMap {:weighting 0} (mt/transformer {:name :enum}))
; => {:weighting :dB-C}

ikitommi04:04:14

if you want to have a transformer for that, you can do the same as I gisted about the :postgres/table => add some hint to the enum and create a transformer that attaches in :compile just for that schema. hope this helps.

Kevin13:04:06

Yeah that's a good option as well. But that will probably lead to a lot of duplication (since my example was just a stripped down version)

aisamu14:04:35

Yup, it was quite annoying