Inconsistent clojure spec forms?

81 views
Skip to first unread message

marian...@vacuumlabs.com

unread,
Jan 19, 2017, 12:35:47 PM1/19/17
to Clojure
Hi,

this sequence of commands in Clojure 1.9.0-alpha14 suprised me:

=> (clojure.spec/def :test/name string?)
:test/name

=> (clojure.spec/form (clojure.spec/and :test/name))
(clojure.spec/and :test/name)

=> (clojure.spec/form (clojure.spec/nilable :test/name))
(clojure.spec/nilable clojure.core/string?)

Is there any reason, why clojure.spec/nilable resolves the keywords in its form and clojure.spec/and does not? If I understand it correctly, the keywords are resolved explicitly by the line (let [pf (res pred)].

In the following paragraph I'll try to explain, why this bothers me. We are developing the API that will be used by external applications. To validate input data, we decided to use the clojure specs, that serves this purpose perfectly. However, we would like to reuse our definitions to generate specifications in other languages like Swagger or JSON Schema. Using clojure.spec/form we were able to detructuralize the spec and translate it. It is not a surprise that there were some specs that cannot be translated automatically. The best solution is to associate the registered keyword of the unsupported spec with the valid translation. The occurence of clojure.spec/nilable (or, for example, clojure.spec/def) then looses the information about the keywords it was used on and therefore the translation is incorrect.

Do you think it is a good idea to translate the specs to the other data specification languages? Is so, is there a better way to do it?

Generally, I think that registering a spec to keyword gives some context to it - at least the name. It is a shame to throw this information away.

Thanks for your time,
Marián

Alex Miller

unread,
Jan 19, 2017, 12:47:06 PM1/19/17
to Clojure
I'd say that's a bug in s/nilable (and I wrote it, so I'll take the blame :). 

I created a ticket and a patch here: http://dev.clojure.org/jira/browse/CLJ-2100
Reply all
Reply to author
Forward
0 new messages