Fork me on GitHub
#clojure-spec
<
2017-09-28
>
shark8me02:09:38

Hi! is there a (short) way to reference a spec defined in a different namespace?

(ns a.b.c 
   (:require [cljs.spec.alpha :as s]))
(s/def ::foo int?)
;;; in a different ns
(ns a.d 
 (:require [cljs.spec.alpha :as s])
(s/valid :a.b.c/foo 10) 
Is there a way to avoid typing the fully qualified :a.b.c/foo by using :require :as ?

gfredericks02:09:49

(require [a.b.c :as a-really-short-name-that-doesn't-take-a-while-to-read]) ::a-really-short-name-that-doesn't-take-a-while-to-read/foo @shark8me

shark8me02:09:24

Thanks! 🙂

shark8me02:09:02

I couldn't find this documented, I assumed it would be :ns/foo instead of ::ns/foo

jumar06:09:54

No, it's not, the proper syntax really is ::specs-ns/my-spec

witek06:09:55

Hello. I have a function: (defn f [m]). I expect m to be a map containing a value unter the key :name. The value has to be a keyword. How do I spec this function using (s/fdef f :args #!%&)? What do I have to put in :args?

jumar07:09:08

@witek If I understand your problem, then s/keys should work just fine:

(s/def ::name keyword?)
(s/valid? (s/keys :req-un [::name]) {:name :john}) ;=> true
(s/valid? (s/keys :req-un [::name]) {:name "john"}) ;=> false

jumar07:09:16

Or rather for the function:

(defn f [m]
  (:name m))

(s/fdef f
        :args (s/cat :m (s/keys :req-un [::name])))

hkjels07:09:56

it seems that coll-of messes with ordering, how do I specify a sorted set of maps

curlyfry09:09:53

@hkjels What do you mean by "messes with ordering"? Do you mean when conforming?

bfabry15:09:09

kinda seems like a sorted-map? predicate that takes the comparator makes sense

souenzzo17:09:13

maybe (s/coll-of string? :kind set? :sorted true). It will be able to sort map, set, vec, list...

jeaye23:09:07

:sorted? true -- now I'm just nitpicking