Fork me on GitHub
#clojure-spec
<
2016-06-26
>
seantempesta07:06:20

is there a way to test if a spec exists for a namespaced key?

Alex Miller (Clojure team)11:06:18

There is a 'registry' function

Alex Miller (Clojure team)11:06:55

In master for the next alpha there will be 'get-spec'

fenton18:06:23

I have defined an api spec for front end to communicate with backend. The messages include an auth token that I strip off soon and forward the rest of the message to internal handling functions. It would be cool to be able to create a spec on my backend that takes a spec defined for the API and removes aspects of it, like for example the auth-token key of the map. I guess the generic manipulating of a spec like a regular hash or something would be interesting.

fenton18:06:46

otherwise i have to duplicate the definition of the message....trying to keep it DRY.

seancorfield18:06:41

@fenton: why not define the spec once for the back end, then define the spec for the front end as "back end" and "auth token"?

fenton18:06:22

@seancorfield: I've pulled my api spec into a third project that is *.cljc with reader conditionals so it can be included as a library for both front and backend. I don't think I want to pull in my backend as a library to my front end....

fenton18:06:19

not sure if thats the best way to do it tho?

seancorfield18:06:57

Just the spec for the shared portion is what I'm suggesting.

fenton18:06:40

ahh that makes sense

seancorfield18:06:43

So if your API and your back end traffic in a common subset of data, that spec needs to be external to both as a shared spec.

fenton19:06:09

yeah, i like that...thanks for the idea!

fenton19:06:02

hmmm...might run into same problem about how to make a spec like that... can't merge a spec map.

seancorfield19:06:57

You use s/and to specify the API must conform to common spec and also to the auth token spec. You don't merge the maps.

seancorfield19:06:26

Alex talked about that technique here a day or two ago.

seancorfield19:06:11

I'm on my phone so I can't dig into code.

fenton19:06:25

i didn't try that approach, but wondering if that doesn't make data that looks like: [{:auth-token "abc123"} {:method :search :search-term "blah"}]

fenton19:06:11

which is okay...i guess...

seancorfield19:06:18

Since maps are open, saying some data conforms to multiple map specs should still leave you with one map?

seancorfield19:06:58

(Replete doesn't seem to have clojure.spec or I could try that out)

fenton19:06:03

hmmm, not sure, making the (s/keys :req ... might be a problem the :req part...meaning each needs all?

mfikes19:06:31

@seancorfield: It does, but it is under cljs.spec and slightly older at this point.

seancorfield19:06:57

@fenton: if A needs key X and it needs key Y then that is additive and it needs keys X and Y - which is a merge and therefore what you want.

gfredericks19:06:41

is (s/and (s/keys ...) (s/keys ...)) going to be special-cased to aid things like generation?

seancorfield19:06:57

(s/conform (s/and (s/keys :req [::a]) (s/keys :req [::b])) {::a 1, ::b 2}) => {:cljs.user/a 1, :cljs.user/b 2}

seancorfield19:06:15

(In cljs.spec in Replete)

seancorfield19:06:36

Dang, I love that I can test that on my phone while lazing in bed on a Sunday!!!

gfredericks19:06:42

I can't tell if that is supposed to answer my question or not :)

gfredericks19:06:05

I can try it myself though, let's see...

mfikes19:06:11

@seancorfield: And if we figure it out, clojure.spec will act as an alias for cljs.spec in bootstrapped ClojureScript πŸ™‚

seancorfield19:06:14

It was for @fenton - I'm typing on my phone, excuse the slow responses.

fenton19:06:28

@seancorfield: yeah, that looks good...

seancorfield19:06:53

Replete is truly awesome.

fenton19:06:01

what is replete?

gfredericks19:06:16

nope, the generator doesn't work

mfikes19:06:36

It is for lazy people who don’t want to crawl out of bed to their computer to start a REPL.

mfikes19:06:49

(Like me. πŸ™‚ )

fenton19:06:52

happy lazing folks, thanks for the help! πŸ™‚

seancorfield19:06:16

@gfredericks: the generator doesn't work for and with two keys?

gfredericks19:06:16

seancorfield: right, which makes sense given what I know about s/and

gfredericks19:06:28

it'd have to be special-cased to have any chance of succeeding

gfredericks19:06:41

because the s/and generator just generates the first thing and filters based on matching the rest of them

seancorfield19:06:39

with-gen to the rescue! πŸ˜†

seancorfield19:06:30

I have nothing.

gfredericks19:06:42

there'll be a helper for this in my inevitable utility library