Fork me on GitHub

@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?


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.


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 😞


Maybe <gasp> generate code </gasp> ?


@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?


Some more “functional specs” might be coming to core, but while waiting, yes, there are some rogue versions at and the data-specs on top of those.


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)


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


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


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))


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


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?


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


maybe I didn't understand what you mean


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


right, forgot about &


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


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


or does it not check things that are defed?


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


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


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 :)


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


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


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


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


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 :)


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)) ?


I went with this:

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


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 --------------------


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))


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


that’s way better, thanks for the tip 🙂


thanks a ton for your help! 😄