This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-03-20
Channels
- # announcements (5)
- # aws (15)
- # babashka (12)
- # beginners (87)
- # calva (13)
- # cider (16)
- # clj-kondo (4)
- # clojure (22)
- # clojure-argentina (1)
- # clojure-europe (9)
- # clojure-houston (1)
- # clojure-nl (2)
- # clojure-norway (25)
- # clojure-uk (5)
- # clojurescript (12)
- # core-typed (37)
- # cursive (15)
- # datomic (40)
- # editors (8)
- # emacs (4)
- # events (1)
- # hyperfiddle (29)
- # keechma (8)
- # leiningen (6)
- # lsp (7)
- # malli (25)
- # off-topic (26)
- # pathom (10)
- # portal (3)
- # re-frame (22)
- # reitit (1)
- # releases (1)
- # ring (2)
- # shadow-cljs (18)
- # yamlscript (1)
is there a way to specify what keys a map has to contain, without saying what that key is?
(malli.core/validate [:map [:my/uuid uuid?]] {:my/uuid (random-uuid)}) ;; works
(malli.core/validate [:map {:registry {:my/uuid uuid?}} :my/uuid] {:my/uuid (random-uuid)}) ;; also works
(malli.core/validate [:map :my/uuid] {:my/uuid (random-uuid)}) ;; throws :malli.core/invalid-schema
I think this answers it: https://clojurians.slack.com/archives/CLDK6MFMK/p1710950351557149?thread_ts=1710923722.360299&cid=CLDK6MFMK
as I understand it, clojure.spec made the intentional choice to separate what keys are in a map, from what the contents of these keys are, see https://clojure.org/about/spec#_map_specs_should_be_of_keysets_only and https://clojuredocs.org/clojure.spec.alpha/keys. I was wondering if there’s a similar construct in malli.
it doesn’t reflect what I actually want to say, which is I may or may not specify this spec elsewhere, but it probably won’t be any?
(I haven’t tried yet how [:map [:my/uuid any?]]
would interact with the global registry)
This looks relevant https://github.com/metosin/malli?tab=readme-ov-file#qualified-keys-in-a-map
I don't think malli checks namespaced keys that aren't declared in the :map
though.
i.e., I don't think it has this feature of keys
:
In addition, the values of *all* namespace-qualified keys will be validated
(and possibly destructured) by any registered specs.
yes, the qualified keys in a map example with the local registry is my second example but it fails without a registry argument
Looking at the implementation of :map
I don't think this is supported by :map
. Could do this:
user=> (m/validate [:and [:map] [:fn `#(contains? % :my/key)]] {})
false
user=> (m/validate [:and [:map] [:fn `#(contains? % :my/key)]] {:my/key true})
true
@U055XFK8V is :map
a generator? Do you think supporting (malli.core/validate [:map :my/uuid] {:my/uuid (random-uuid)})
could be a sensible addition to the syntax?
I mean that trying to generate values will fail:
user=> (mg/generate [:and [:map] [:fn `#(contains? % :my/key)]])
Execution error (ExceptionInfo) at clojure.test.check.generators/fn (generators.cljc:435).
Couldn't satisfy such-that predicate after 100 tries.
Will need a property like :gen/fmap
to fix it:
user=> (mg/generate [:and [:map {:gen/fmap `#(assoc % :my/key :whatever)} ] [:fn `#(contains? % :my/key)]])
#:my{:key :whatever}
Could probably make a case for at least supporting a property for the behavior you want, like say [:map {:unregistered-qualified-kw true} ...]
.also noticed when running the example from https://github.com/metosin/malli/blob/master/docs/function-schemas.md#development-instrumentation that I needed to add a call to (mi/instrument!)
for the example to throw. Is that expected? This was with malli 0.11, trying 0.14… Same there.