This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-10-25
Channels
- # announcements (14)
- # aws (1)
- # babashka (23)
- # beginners (442)
- # calva (50)
- # chlorine-clover (1)
- # cider (32)
- # clojure (124)
- # clojure-europe (35)
- # clojure-france (5)
- # clojure-gamedev (5)
- # clojure-nl (2)
- # clojure-portugal (3)
- # clojure-uk (4)
- # clojurescript (56)
- # conjure (5)
- # cursive (24)
- # datalevin (1)
- # datomic (57)
- # fulcro (35)
- # helix (15)
- # holy-lambda (8)
- # introduce-yourself (1)
- # jobs (5)
- # kaocha (1)
- # lsp (99)
- # malli (10)
- # music (1)
- # off-topic (22)
- # pathom (38)
- # podcasts-discuss (10)
- # polylith (10)
- # reitit (1)
- # releases (1)
- # remote-jobs (4)
- # shadow-cljs (18)
- # spacemacs (6)
- # tools-build (22)
- # vim (66)
- # xtdb (22)
It's pretty terrible but it works. Anything here a bad idea?
(defn -strip-invalid-optional-keys-transformer
[schema _]
(let [entries (filter #(:optional (m/properties (second %))) (m/entries schema))
fs (map (fn [[k v]]
(let [validator (m/validator v)]
(fn [m]
(if-let [e' (find m k)]
(let [v' (val e')]
(if (validator v')
m
(dissoc m k)))
m))))
entries)]
(reduce comp fs)))
(def strip-invalid-optional-keys-transformer
(let [transform {:compile
(fn [schema _]
{:leave -strip-invalid-optional-keys-transformer})}
encoders {:map transform}]
(mt/transformer
{:decoders encoders
:encoders encoders})))
with a quick look, looks good. the runtime is bare minimum, everything possible has been pushed to creation time.
e.g. you have valid value, after encoding, the keys could be different, (e.g. stringified) so the transforming functions misses all the keys.
I must say the order where things happen with regards to transformers compilation was very hard to follow 😞
It's pretty terrible but it works. Anything here a bad idea?
(defn -strip-invalid-optional-keys-transformer
[schema _]
(let [entries (filter #(:optional (m/properties (second %))) (m/entries schema))
fs (map (fn [[k v]]
(let [validator (m/validator v)]
(fn [m]
(if-let [e' (find m k)]
(let [v' (val e')]
(if (validator v')
m
(dissoc m k)))
m))))
entries)]
(reduce comp fs)))
(def strip-invalid-optional-keys-transformer
(let [transform {:compile
(fn [schema _]
{:leave -strip-invalid-optional-keys-transformer})}
encoders {:map transform}]
(mt/transformer
{:decoders encoders
:encoders encoders})))
It seems that most times where relations about map keys are requires by users it is to specify mutual exclusion. Could it be worth it to add a mutex property which takes a collection of keys which are mutually exclusive?