Whats the best way to spec numbers as strings so something like "12.53" will be generated?
@kyle Take a look at https://github.com/worldsingles/web-specs
It has specs that accept strings or longs and generate as strings that are parseable as longs. Should give you something to work from.
@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
Yeah, it'll do zero-padded. And you want yyyy
and d
lowercase for dates.
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?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
Makes perfect sense, thanks.
Are there good library to use with clojure.spec? 🙂
@neo2551 you can find spec libraries (along with other libraries) here: https://www.clojure-toolbox.com/
Can you write a function spec for a multi-method? If so, any chance anyone can share an example of this somewhere?
I’m not sure if this is useful or relevant to your goals, but: https://gist.github.com/telekid/f2e588718dbdfe48306d64e5388bdc15
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!
You can spec the method, but afaik not the implementations
Caveat with fspec on mm: instrumentation changes the type to a fn, so future defmethods will fail with a type error
You need to unstrument before evaluating more defmethods
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.
You can spec the dispatch method used in the mm if that’s useful
But mms themselves, no
That’s something! But good to know, thanks!