This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-10-20
Channels
- # announcements (33)
- # aws (1)
- # babashka (8)
- # beginners (100)
- # calva (59)
- # clara (4)
- # clj-kondo (33)
- # cljdoc (9)
- # cljs-dev (30)
- # cljsrn (1)
- # clojure (24)
- # clojure-australia (1)
- # clojure-boston (1)
- # clojure-dev (4)
- # clojure-europe (14)
- # clojure-france (5)
- # clojure-italy (7)
- # clojure-nl (1)
- # clojure-uk (36)
- # clojurescript (13)
- # clojureverse-ops (6)
- # conjure (2)
- # cursive (2)
- # datahike (11)
- # datalevin (1)
- # datomic (106)
- # graphql (3)
- # helix (10)
- # holy-lambda (24)
- # kaocha (2)
- # lambdaisland (3)
- # lsp (199)
- # malli (35)
- # off-topic (16)
- # pathom (7)
- # polylith (38)
- # portal (16)
- # quil (2)
- # re-frame (18)
- # reagent (57)
- # shadow-cljs (11)
- # testing (3)
- # xtdb (9)
hi all. there is bunch of quality PRs being held in the queue, sorry. I’m working on with the malli internals (schema ast, lifecycle, providers, registries) and want to find a good design before merging anything big in. Will review and pull the queued PRs after that. Any small fixes welcome anytime.
Necroing a question I asked some time ago, any idea for a transformer which removes invalid keys?
invalid… you could a) validate the keys when transforming or b) explain and remove based on that.
my assumption is that having validation and transformation as separate steps is the fastest way to do it. 2 simple sweeps instead of one (more complex) sweep.
originally, i though of doing all the workers using just -walk
. there would be walker to create a validator, decoder etc. but as all schemas should have all of those and as performance was a one of the primary goals - they got separate (protocol) methods.
the one walker would have allowed to compose a chain of validate + transform in an easier way, I think.
Figured out how to strip invalid optional keys, feel free to add it to tips, after some beautifying
(defn strip-invalid-optional-keys-transformer
([]
(let [transform
{:compile
(fn [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)))}]
(mt/transformer
{:decoders {:map transform}
:encoders {:map transform}}))))
(m/decode [:map [:a {:optional true} int?] [:b {:optional true} int?]] {:a 1 :b 2.2} strip-invalid-optional-keys-transformer)
:or
does also validation on transformation, as it needs to find the branch, which is valid after transformation.
spike about caching computations (`-form`, -validator
etc) with schemas:
(def schema
(m/schema
[:map
[:x boolean?]
[:y {:optional true} int?]
[:z [:map
[:x boolean?]
[:y {:optional true} int?]]]]))
;; 1.5µs -> 11ns (130x)
(p/bench
(m/validator schema)
;; 1.7µs -> 64ns (25x)
(p/bench
(m/validate schema {:x true, :z {:x true}})) ; => true
there is the initial cost of creating the thing, but just once opposed to every call. the results are cached with the actual schema instance, so when the schema instance is not needed, the cached results will also be GCd, so not leaking memory.