Fork me on GitHub
#clojure-spec
<
2022-10-06
>
pavlosmelissinos10:10:29

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?

Alex Miller (Clojure team)12:10:05

I don’t think you can easily do that

πŸ™ 1
pavlosmelissinos12:10:01

Alright, thanks πŸ™‚ It would have been nice to have but I can work around it, so it's not a big deal.

Cora (she/her)20:10:02

πŸ‘‹:skin-tone-2:

Cora (she/her)20:10:06

is there a way to spec maps but without defining entities for the keys?

Alex Miller (Clojure team)20:10:35

map? is a valid spec :)

Cora (she/her)20:10:56

I mean a spec for a combination of specific keys with specific values

Alex Miller (Clojure team)20:10:10

what kind of keys / what kind of values ?

Alex Miller (Clojure team)20:10:38

you can s/and any custom predicate you might have

Cora (she/her)20:10:18

say I want a spec for a map of {:key1 "a string" :key2 :a-keyword} without s/def :key1 and s/def :key2

Cora (she/her)20:10:24

I can write arbitrary functions for these but it would be nice if there were a way to go just {:key1 string? :key2 keyword?}

Alex Miller (Clojure team)20:10:23

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

Cora (she/her)20:10:57

that doesn't validate the values, though?

Alex Miller (Clojure team)20:10:00

but no, you can't get the value check without also def'ing the key specs

Cora (she/her)20:10:35

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?]]]]]

souenzzo15:10:23

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

Cora (she/her)16:10:46

for sure, I just greatly prefer structural and was working on a project that only had spec

souenzzo17:10:55

I just greatly prefer named and I'm working on a project that only had malli let's switch chairs πŸ˜‚

πŸ˜‚ 1
Cora (she/her)20:10:02

I'm on a project using spec, though, and was hoping for something similar. sounds like I need to define the key specs