This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-02-16
Channels
- # announcements (33)
- # asami (37)
- # autochrome-github (1)
- # aws (5)
- # babashka (26)
- # babashka-sci-dev (18)
- # beginners (63)
- # biff (1)
- # calva (66)
- # cider (15)
- # clj-kondo (24)
- # cljs-dev (2)
- # cljsrn (14)
- # clojure (62)
- # clojure-doc (1)
- # clojure-europe (15)
- # clojure-nl (11)
- # clojure-spec (12)
- # clojure-uk (4)
- # clojurescript (45)
- # community-development (2)
- # cursive (34)
- # datahike (7)
- # datomic (7)
- # events (1)
- # figwheel-main (5)
- # fulcro (19)
- # gratitude (7)
- # holy-lambda (85)
- # hyperfiddle (4)
- # jobs (7)
- # jobs-discuss (25)
- # kaocha (4)
- # lsp (11)
- # off-topic (1)
- # polylith (20)
- # portal (14)
- # practicalli (6)
- # proletarian (5)
- # rdf (52)
- # re-frame (13)
- # reagent (48)
- # releases (1)
- # remote-jobs (3)
- # shadow-cljs (7)
- # tools-deps (17)
Is there any way to spec a map that can mostly be described with map-of, but has a few special keys with their own types? E.g.
{::special-1 {:I-should-be :a-map}
::special-2 :I-should-be-a-keyword
"standard-key-1" "standard-val-1"
"standard-key-2" "standard-val-2"
; ...
"standard-key-n" "standard-val-n"}
(This is a structure already in use that I'm speccing for documentation purposes. I wouldn't generally design something that functions both as a record and as a map of arbitrary well-typed values.)
That was my first thought, but I'm not seeing a way to write the map-of part. If I did (map-of string? string?)
, the special keys and values would fail validation.
yes, you can do this by spec'ing it as a collection of kv pairs and then describe the pairs. I have a blog post about the destructuring spec at https://cognitect.com/blog/2017/1/3/spec-destructuring that has this characteristic
the key is you want (s/every (s/or ...kv-tuple-shapes...) :into {})
Hmm, I've been defaulting to using coll-of rather than every, but I'm seeing that it blows up when I try to do that with :into {}
blows up how?
(s/conform (s/coll-of (s/cat :k string? :v string?) :into {}) {"foo" "bar" "baz" "bork"})
Execution error (Error) at (<cljs repl>:1).
nth not supported on this type cljs.core/PersistentArrayMap
=> :repl/exception!
Could be a quirk of the cljs implementationno, that's why the example above uses s/every
instead
Okay, figured that might be the case, it's just surprising to me
coll-of assumes the coll is nth-able (which maps aren't). we may actually have a ticket to consider that, don't remember