Fork me on GitHub
#clojure-spec
<
2020-03-08
>
ag00:03:31

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.

seancorfield00:03:40

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?

ag00:03:13

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

ag00:03:36

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

seancorfield00:03:39

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

ro607:03:39

I went down that road a bit once too, mostly for want of the automatic error descriptions Spec gives, but extending down into a String (eg "That's not a valid email because X, Y, Z"). Spec definitely does a good job of that for aggregates/shapes. I don't recall finding a satisfactory solution at the time.

seancorfield00:03:10

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

seancorfield01:03:04

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?

seancorfield01:03:00

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.

derpocious16:03:24

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

Eddie16:03:33

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

Eddie17:03:30

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

andy.fingerhut07:03:24

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.

ro607:03:39

I went down that road a bit once too, mostly for want of the automatic error descriptions Spec gives, but extending down into a String (eg "That's not a valid email because X, Y, Z"). Spec definitely does a good job of that for aggregates/shapes. I don't recall finding a satisfactory solution at the time.