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
Kyle Brodie 2020-05-16T01:02:47.429100Z

Whats the best way to spec numbers as strings so something like "12.53" will be generated?

seancorfield 2020-05-16T02:21:44.429500Z

@kyle Take a look at https://github.com/worldsingles/web-specs

seancorfield 2020-05-16T02:22:35.430700Z

It has specs that accept strings or longs and generate as strings that are parseable as longs. Should give you something to work from.

Kyle Brodie 2020-05-16T02:49:00.432400Z

@seancorfield Thank you! That library looks very helpful. I might have to write my own date though because mine are "YYYY-MM-DD". Unless "yyyy-M-d" is the right format for zero padded in java-time

seancorfield 2020-05-16T03:09:45.433100Z

Yeah, it'll do zero-padded. And you want yyyy and d lowercase for dates.

Kira McLean 2020-05-16T11:41:09.440600Z

Hi there.. I’m just getting started with spec, have a somewhat noob question that I’m having trouble finding an answer to: Is it possible to have two different specs for the value of a given un-qualified keyword? I.e. I have a map and I know that the values might have different requirements in different contexts, but I would like to keep re-using the same key. So I have specs like this (`fs` is [datoteka.core :as fs] ):

(s/def ::path fs/path?)
(s/def ::html-path (s/and ::path (partial matches-ext "html")))
(s/def ::content string?)

;; This one is great -- checks my maps like {:path ,,, :content ,,,}
(s/def ::page (s/keys :req-un [::path ::content]))

;; Here is the issue: I want to check a map that also has the keys [:path 
;; :content], not one with an html-path key
(s/def ::html-page (s/keys :req-un [:path ::html-path ::content])) 
In the ::html-page spec I want :path to match the html-path spec but for the key to still be called :path . Is this possible?

ghadi 2020-05-16T11:51:20.443Z

If you define two specs, :a.b.c/path and :x.y.z/path you can use them to spec unqualified keys in different maps

Kira McLean 2020-05-16T16:14:19.445Z

Makes perfect sense, thanks.

2020-05-16T18:17:12.445500Z

Are there good library to use with clojure.spec? 🙂

salam 2020-05-16T18:49:16.446100Z

@neo2551 you can find spec libraries (along with other libraries) here: https://www.clojure-toolbox.com/

Kira McLean 2020-05-16T20:57:07.447300Z

Can you write a function spec for a multi-method? If so, any chance anyone can share an example of this somewhere?

telekid 2020-05-19T00:04:02.456600Z

I’m not sure if this is useful or relevant to your goals, but: https://gist.github.com/telekid/f2e588718dbdfe48306d64e5388bdc15

Kira McLean 2020-05-21T21:33:18.009400Z

Ah this is really cool! Never thought of separating out the args and mm to spec separately. Perhaps a little unorthodox or is this normal? But definitely interesting. Thanks!

favila 2020-05-16T21:31:00.448100Z

You can spec the method, but afaik not the implementations

favila 2020-05-16T21:32:08.450Z

Caveat with fspec on mm: instrumentation changes the type to a fn, so future defmethods will fail with a type error

favila 2020-05-16T21:41:00.451Z

You need to unstrument before evaluating more defmethods

Kira McLean 2020-05-16T23:29:48.451300Z

Interesting.. good to know. Yeah I’m curious about whether it’s possible to spec the implementations of a multimethod. I couldn’t find any examples online, but so far I’ve found with clojure that doesn’t always mean it’s not possible.

alexmiller 2020-05-16T23:41:52.452100Z

You can spec the dispatch method used in the mm if that’s useful

alexmiller 2020-05-16T23:42:05.452700Z

But mms themselves, no

Kira McLean 2020-05-16T23:42:49.452900Z

That’s something! But good to know, thanks!