Fork me on GitHub
#clojure-spec
<
2018-12-20
>
jaihindhreddy-duplicate17:12:19

Will there be a way to s/select into a subset of dispatch keys of a s/multispec?

jaihindhreddy-duplicate17:12:29

Say I have a polymorphic map called ::payment-info which is polymorphic, like this: (s/def ::payment-info (s/multi-spec pi ::type)) where (s/def ::type #{::credit-card ::debit-card ::net-banking ::bitcoin}) Because cards are processed one way, and each of the other types another way, is s/selecting into ::credit-card and ::debit-card a good idea? Was just wondering about this, s/select isn't even a thing yet so these are potential future questions 🙂

Alex Miller (Clojure team)18:12:35

it’s not real enough yet that I can’t answer those questions

😅 4
ShaneLester18:12:53

How would I write a spec to say- in this map I expect to have X Y and Z keys (which have their own specs) and then I don’t care about the rest of the map? Does specifying those 3 in the :req of the keys function assume that I don’t care about the rest?

taylor18:12:20

yeah the s/keys specs are "open" by default

ShaneLester18:12:04

Okay cool, thanks

ShaneLester20:12:48

So I have these 3 specs here… I am trying to say, hey there is going to be any number of inputs here in this input-type. But apparently something like {:inputs [:email {:value nil}]} doesn’t meet this spec? It’s saying it should satisfy map? but it seems to me that that IS a map. Anyone have a clue what I’m doing wrong here?

taylor21:12:40

::inputs is expecting a collection of ::input-types which are maps/`s/keys` specs, but your input is a single map

ShaneLester21:12:03

ohhh I see. thank you

taylor21:12:56

[{:value nil}] would conform to your existing spec

taylor21:12:49

you may have some other issues judging from the vector [:email {:value nil}], with the spec as it is

ShaneLester21:12:13

Right… yeah I may be doing some things in not the best way 🙂 time to re-evaluate

taylor21:12:46

feel free to post some other sample inputs in here and I could help write some sample specs for them

ShaneLester21:12:31

Cool, I appreciate that

👍 4
ag22:12:24

can someone throw an example of recursive spec… e.g. directory structure?

ag22:12:19

I want something like:

(s/def :folder/type #{"folder" "file"})
(s/def :folder/id (s/and string?  (partial re-matches #"\d*") #(< 8 (count %))))
(s/def :folder/name string?)

(s/def ::entries
  (s/coll-of ::folder))

(s/def ::folder
  (s/keys :req-un [:folder/type :folder/name ::entries]))
but obviously that doesn’t work

taylor22:12:39

this will work with (s/def ::entries (s/* ::folder))

taylor22:12:37

(binding [clojure.spec.alpha/*recursion-limit* 2]
  (gen/sample (s/gen ::folder)))
...
{:type "file",
  :name "8y4FOn",
  :entries ({:type "folder",
             :name "",
             :entries ({:type "folder", :name "x2RcQC", :entries []}
                       {:type "folder", :name "6on7", :entries []}
                       {:type "folder", :name "", :entries []}
                       {:type "folder", :name "aakQc3q", :entries []}
                       {:type "folder", :name "6g4748z", :entries []})}
            {:type "file",
             :name "1f",
             :entries ({:type "folder", :name "j52GS", :entries []} {:type "folder", :name "e223Z", :entries []})}
            {:type "file",
             :name "DUuXT6",
             :entries ({:type "folder", :name "t", :entries []}
                       {:type "folder", :name "720S2cy", :entries []}
                       {:type "file", :name "", :entries []}
                       {:type "folder", :name "", :entries []})}
            {:type "folder",
             :name "18EZK",
             :entries ({:type "folder", :name "27", :entries []} {:type "file", :name "Q", :entries []})}
            {:type "file",
             :name "2K8w",
             :entries ({:type "folder", :name "Pv", :entries []} {:type "file", :name "xl4346Q", :entries []})}
            {:type "folder",
             :name "j",
             :entries ({:type "file", :name "21C", :entries []}
                       {:type "file", :name "3", :entries []}
                       {:type "folder", :name "2", :entries []}
                       {:type "folder", :name "1dFvly", :entries []}
                       {:type "folder", :name "zczGQf", :entries []})})}
...

ag22:12:45

right.. right… I vaguely remembered, but I couldn’t figure it out… Thanks!

👍 4
ag22:12:54

now I wonder if there is a way to make

binding [clojure.spec.alpha/*recursion-limit* 2]
part of (s/with-gen?

ag22:12:18

so it never generates more than 2 levels deep?