Fork me on GitHub
#clojure-spec
<
2018-01-25
>
mrchance08:01:12

@ikitommi Thanks, but I don't want to generate swagger, but the data consumed by the interfaces specified in a swagger file. It doesn't look like that is possible with swagger-spec?

ikitommi09:01:58

oh, the endpoint data. With vanilla swagger spec, there needs to be a JSON-Schema -> Spec converter, I don’t know if such exists yet. But if you describe your endpoints with spec, you get the data generation for free.

mrchance10:01:51

Yes, already doing that, unfortunately some of the other services in my project aren't written in Clojure. If one were to write such a converter, how would one go about it? I invested an afternoon because I didn't really find anything too, but it seemed really hard and messy to generate specs at runtime, due to its reliance on global mutable state 😞

mrchance10:01:30

Maybe <gasp> generate code </gasp> ?

mrchance10:01:43

@ikitommi I saw that you had schema like maps in your spec tools, which could be handled like normal data. Would that be an option for this case?

ikitommi10:01:07

Some more “functional specs” might be coming to core, but while waiting, yes, there are some rogue versions at https://github.com/metosin/spec-tools/blob/master/src/spec_tools/data_spec.cljc#L8-L10 and the data-specs on top of those.

ikitommi10:01:29

most/all of the core specs have functional versions already in the core, but currently not documented and the apis are bit hairy. Maybe those will be polished and published as part of the public api? The forms need to be created manually but for most core predicates, it’s easy to resolve those (and spec-tools does that already)

mrchance12:01:35

Hm, I see. Thanks, I'll give it a go, and hope for the release of the functional api in core.

mpenet15:01:49

is there something like s/cat but that doesn't do any labeling? need to match a subsequence within a sequence without too much ceremony

mpenet15:01:53

it's a seq of chars (just playing), so nothing generic, I need equality matching (ex match (b a r) in (f o o b a r b a z))

mpenet15:01:16

I know I can mix s/cat and sets of chars or predicates, but that's quite ugly

mpenet15:01:19

I am also forced to use regex context (my question is part of a wider parsing thing)

Alex Miller (Clojure team)15:01:48

if you don’t want labeling, just use s/valid?

mpenet15:01:33

I dont need labeling per char, I do for the whole subseq (up one level)

mpenet15:01:56

maybe I didn't understand what you mean

mpenet15:01:05

all the surounding value have a meaning that I need to retain also, I can't just check "does this seq contains this sub-seq"

Alex Miller (Clojure team)16:01:52

you can use s/& to apply a custom predicate to a regex spec

Alex Miller (Clojure team)16:01:29

which could be helpful here

mpenet16:01:04

right, forgot about &

mpenet16:01:16

that's brilliant, I was messing with conformers but I think & might just do all this for me

schmee21:01:08

is it just me or do tools.namespace and instrument not get along?

schmee22:01:11

or does it not check things that are defed?

schmee22:01:53

that is, if I reload a namespace that has (def foo (some-instrumented-fn invalid-value)), does reloading the ns throw an exception?

Alex Miller (Clojure team)22:01:10

Reloading is going to remove your instrumented vars

schmee22:01:12

ok, so those checks should be done through tests instead?

schmee22:01:55

let me change the question: what’s the best way to check specs with clojure.test?

Alex Miller (Clojure team)22:01:13

that seems pretty far from the original questions :)

schmee22:01:13

(is (s/valid? ::my-spec (some-fn)) works, but it doesn’t give helpful output

schmee22:01:40

I’m not very familiar with either clojure.test or spec, so I’m fumbling around a bit 🙂

Alex Miller (Clojure team)22:01:45

there used to be a pin here to an example, but maybe it has aged out

Alex Miller (Clojure team)22:01:45

by “check specs”, I’m assuming you mean to run clojure.spec.test.alpha/check on one or more spec’ed functions

schmee22:01:41

actually, I want to check with s/valid?, but use s/explain instead of the usual error message provided by is

Alex Miller (Clojure team)22:01:54

so s/valid? is not going to be as useful in checking spec’ed functions as is calling check

Alex Miller (Clojure team)22:01:19

unless ::my-spec here refers to a data spec

schmee22:01:16

::my-spec in this case is a data spec, yes

schmee22:01:35

sorry if my questions are a bit confused, it’s hard to ask good questions when you’re not familiar with something

Alex Miller (Clojure team)22:01:09

I am similarly trying to re-interpret your questions :)

Alex Miller (Clojure team)22:01:38

so you want to check if some data matches a spec and if not, return a message that is s/explain

Alex Miller (Clojure team)22:01:35

(is (nil? (s/explain ::spec data)))

Alex Miller (Clojure team)22:01:33

although I guess you want the explain result as a value not as a print

Alex Miller (Clojure team)22:01:30

(is (not= "Success!\n" (s/explain ::spec data))) would do that I think

Alex Miller (Clojure team)22:01:38

that’s quite ugly of course :)

schmee22:01:50

explain-data did the trick!

Alex Miller (Clojure team)22:01:22

yeah, can do same idea with explain-data

Alex Miller (Clojure team)22:01:50

you can supply a final custom error message to is too

Alex Miller (Clojure team)22:01:38

so something like: (is (valid? ::spec data) (s/explain-str ::spec data)) ?

schmee22:01:39

I went with this:

(defmacro spec-is [spec value]
  `(is (nil? (s/explain-data ~spec ~value))))

bbrinck01:01:16

A shameless plug, but you can also add a message to is. With expound you could do something like

(defmacro spec-is2 [spec value]
  `(is (s/valid? ~spec ~value)
       (expound/expound-str ~spec ~value)))
which will give you output like:
FAIL in (test2) (alpha_test.cljc:2152)
-- Spec failed --------------------

  1

should satisfy

  #{:c :b :a}



-------------------------
Detected 1 error

expected: (clojure.spec.alpha/valid? #{:c :b :a} 1)
  actual: (not (clojure.spec.alpha/valid? #{:c :b :a} 1))

schmee08:01:01

haha, I don’t mind shameless plugs when they are great! 😄

schmee08:01:06

that’s way better, thanks for the tip 🙂

schmee22:01:50

thanks a ton for your help! 😄