This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-10-06
Channels
- # aleph (70)
- # announcements (9)
- # babashka (43)
- # babashka-sci-dev (6)
- # beginners (97)
- # cider (2)
- # clj-commons (3)
- # clj-kondo (41)
- # clojure (88)
- # clojure-europe (44)
- # clojure-nl (2)
- # clojure-spec (22)
- # clojurescript (65)
- # community-development (6)
- # conjure (10)
- # cursive (6)
- # datahike (13)
- # datomic (4)
- # eastwood (11)
- # events (1)
- # fulcro (45)
- # graalvm (1)
- # graphql (3)
- # hyperfiddle (3)
- # integrant (7)
- # jobs (1)
- # lambdaisland (1)
- # lsp (58)
- # nbb (4)
- # nrepl (3)
- # pathom (15)
- # shadow-cljs (27)
- # tools-deps (1)
I have a (s/def ::items (s/coll-of ::item :min-count 1))
.
How do I create a new spec from it that has some extra constraints, e.g. (s/def ::items-2 (s/coll-of ::item :min-count 1 :gen-max 6))
without repeating the common ones?
Alright, thanks π It would have been nice to have but I can work around it, so it's not a big deal.
π:skin-tone-2:
is there a way to spec maps but without defining entities for the keys?
map?
is a valid spec :)
I mean a spec for a combination of specific keys with specific values
what kind of keys / what kind of values ?
you can s/and
any custom predicate you might have
say I want a spec for a map of {:key1 "a string" :key2 :a-keyword}
without s/def :key1
and s/def :key2
I can write arbitrary functions for these but it would be nice if there were a way to go just {:key1 string? :key2 keyword?}
user=> (s/def ::m (s/keys :req-un [:foo/key1 :foo/key2]))
:user/m
user=> (s/valid? ::m {:key1 "a string" :key2 :a-keyword})
true
that doesn't validate the values, though?
but no, you can't get the value check without also def'ing the key specs
gotcha
for reference, with malli you can do:
[:map
[:id string?]
[:tags [:set keyword?]]
[:address
[:map
[:street string?]
[:city string?]
[:zip int?]
[:lonlat [:tuple double? double?]]]]]
btw malli is more structural https://en.wikipedia.org/wiki/Structural_type_system spec is more nominal https://en.wikipedia.org/wiki/Nominal_type_system spec will force you to give a unique name to each piece of data that you have. it is nice to have both available in clojure not a better vs worst. each one has it own advantages and penalties
for sure, I just greatly prefer structural and was working on a project that only had spec
I just greatly prefer named and I'm working on a project that only had malli let's switch chairs π
I'm on a project using spec, though, and was hoping for something similar. sounds like I need to define the key specs
thanks, Alex