clojure-spec

About: http://clojure.org/about/spec Guide: http://clojure.org/guides/spec API: https://clojure.github.io/spec.alpha/clojure.spec.alpha-api.html
ag 2020-03-08T00:12:31.104700Z

Do we have a repo with a collection of arbitrary specs for all sorts different cases? I often need to create a spec for something so trivial but end up spending more time than intended, only to remember later that I did it already in some other Clojure project. Sometimes I just need to perform Github search and find some gems. Would be nice to have a community driven repo of good examples.

seancorfield 2020-03-08T00:16:40.105700Z

Hmm, I see Specs as being tied to domains rather than being that sort of reusable -- what sort of things do you have in mind @ag?

ag 2020-03-08T00:28:13.107400Z

things like various datetime, timestamps, comma separated lists of currencies or/and US states and US-state abbrevs, etc.

ag 2020-03-08T00:29:36.108600Z

You right though, maybe not a collection, but perhaps something like a cookbook

seancorfield 2020-03-08T00:53:39.109200Z

You're talking about strings? Parsing strings? That's not a good use for Spec tho'...

seancorfield 2020-03-08T00:57:10.110Z

Specs for datetime/timestamps -- inst?, s/inst-in.

seancorfield 2020-03-08T01:01:04.112300Z

Locales, country codes, currency codes -- all easily available from Java classes but I guess ready made Specs for sets of those things would save you writing a single line of Clojure?

seancorfield 2020-03-08T01:04:00.113800Z

US states -- that's domain-specific: do you just want the 50 states, plus DC? Plus the various US territories/islands? But stuff like that belongs in databases and you can construct a set spec from that.

derpocious 2020-03-08T16:06:24.114500Z

hi, I am wondering if it is possible to instrument the return value of a function?

2020-03-09T07:10:24.115600Z

I have not used it before, but the orchestra library aims to do this: https://github.com/jeaye/orchestra. It is not included in spec because it is instead recommended to only do checking of return values during testing, e.g. property-based testing using randomly generated arguments.

Eddie 2020-03-08T16:59:33.115100Z

I like to use post-assertions combined with spec. They look like this.

(defn foo
	[x y]
	{:post [(s/valid? ::my-spec %)]}
	(do-something x y))

Eddie 2020-03-08T17:01:30.115400Z

You can also validate your arguments similarly using a pre-assertion.

(defn foo
	[x y]
	{:pre  [(s/valid? ::x-spec x)
	        (s/valid? ::y-spec y)]
	 :post [(s/valid? ::my-spec %)]}
	(do-something x y))