Fork me on GitHub
#clojure-spec
<
2019-01-11
>
mping14:01:41

how can I create a spec for a map like

{"2018-01-01": {:value 10}, "2018-03-22: {:value 20}}

mping14:01:52

map where keys are dates

alexmiller14:01:50

write a function that decides whether that is a valid string - that function is your predicate

alexmiller14:01:47

(s/def ::date valid-date?)
(s/def ::value int?)
(s/def ::data (s/keys :req-un [::value]))
(s/def ::m (map-of ::date ::data))

mping14:01:53

tks Alex, pretty much what I had, problem must be somewhere else.

ikitommi15:01:59

@mping you might want to add :conform-keys true to map-of

alexmiller15:01:22

with a pred, the keys will will just conform to themselves, so that’s just making it slower

mping15:01:24

@alexmiller in what cases would conform-keys be needed?

mping15:01:32

core specs?

alexmiller15:01:38

cases where the keys need to be conformed

alexmiller15:01:59

most maps have keys that are strings, numbers, keywords, uuids, etc - all of which conform to themselves, so there is no reason to conform the map keys

alexmiller15:01:51

but if you had a map whose keys conformed to a value other than themselves (a regex spec, an s/or, a nested map, etc) then you would want to consider it

alexmiller15:01:48

but even in that case, you have to think carefully to make sure the conformed values don’t create a case where two map keys have the same conformed value. if so, they will collide in the conformed map.

mping15:01:00

thanks guys