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?
I donβt think you can easily do that
Alright, thanks π It would have been nice to have but I can work around it, so it's not a big deal.
ππ»
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})
truethat doesn't validate the values, though?
but no, you can't get the value check without also def'ing the key specs
gotcha
ok
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