Fork me on GitHub
#clojure-spec
<
2018-09-09
>
basti01:09:49

I there a nested version of s/form ?

basti01:09:06

to back up a little bit.. suppose I have a deeply nested map data structure, how can I inspect its structure without manually traversing all its composed (s/def)s ? I could generate an example, but that doesn’t tell me that a specific leaf is specd with a string? predicate e.g.

basti01:09:32

plus the example generation is also kinda limited because its predicated on the fact that every predicate I use has a generator

basti05:09:07

Other question, how do I spec a collection which has the sequence: [string? int? keyword?] .. Whereby int? keyword? can repeat arbitrary. So something like [sting? int? keyword? int? keyword? int? keyword?] would be valid?

seancorfield05:09:36

@basti untested but I think (s/cat :s string? :rest (s/+ (s/cat int? keyword?)))

4
seancorfield06:09:07

Yeah, (s/def ::x (s/cat :s string? :rest (s/+ (s/cat :i int? :k keyword?)))) should do it @basti

basti06:09:36

that’s awesome, thanks @seancorfield 🙂

misha07:09:44

well, until skynet comes around, it's up to you to assess which spec is trivial to generate automatically, and which ones need your help, @basti

misha07:09:05

what exactly do you mean by inspect? there is an s/conform, which destructures and assigns labels to most of the nodes. in which usecase it is not enough for you?

basti07:09:49

basically convert

(s/def ::name string?)
(s/def ::person (s/keys :req-un [::name]))
(s/def ::registration (s/keys :req-un [::person]))
to
(mapify ::registration)
=> {:person {:name 'cljs.core/string?}}
So that I see the whole structure at one glance. Meanwhile I wrote a small utility function that converts map specs to my desired structure - will write a blog post about that eventually 🙂

ikitommi07:09:30

@basti at least spec-tools has a spec form parser & visitor, which can walk over all(?) specs. But I guess you already rolled your own.

basti08:09:00

awesome, yeah I hand-rolled my own visitor, but happy to switch to a library 🙂 I’ll have a look!