Skip to content

Commit 7187f8f

Browse files
committed
CLJS-2844: [spec] Add support for undefining a spec
1 parent e58cf89 commit 7187f8f

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/main/cljs/cljs/spec/alpha.cljc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@
6161
(symbol (str ana/*cljs-ns*) (str s))))
6262

6363
(defmacro def
64-
"Given a namespace-qualified keyword or resolveable symbol k, and a spec,
65-
spec-name, predicate or regex-op makes an entry in the registry mapping k to
66-
the spec"
64+
"Given a namespace-qualified keyword or resolveable symbol k, and a
65+
spec, spec-name, predicate or regex-op makes an entry in the
66+
registry mapping k to the spec. Use nil to remove an entry in
67+
the registry for k."
6768
[k spec-form]
6869
(let [k (if (symbol? k) (ns-qualify &env k) k)
6970
form (res &env spec-form)]

src/main/cljs/cljs/spec/alpha.cljs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,13 @@
294294
"Do not call this directly, use 'def'"
295295
[k form spec]
296296
(assert (c/and (ident? k) (namespace k)) "k must be namespaced keyword or resolveable symbol")
297-
(let [spec (if (c/or (spec? spec) (regex? spec) (get @registry-ref spec))
298-
spec
299-
(spec-impl form spec nil nil))]
300-
(swap! registry-ref assoc k (with-name spec k))
301-
k))
297+
(if (nil? spec)
298+
(swap! registry-ref dissoc k)
299+
(let [spec (if (c/or (spec? spec) (regex? spec) (get @registry-ref spec))
300+
spec
301+
(spec-impl form spec nil nil))]
302+
(swap! registry-ref assoc k (with-name spec k))))
303+
k)
302304

303305
(defn registry
304306
"returns the registry map, prefer 'get-spec' to lookup a spec by name"

src/test/cljs/cljs/spec_test.cljs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@
307307
(deftest keys-explain-pred
308308
(is (= 'cljs.core/map? (-> (s/explain-data (s/keys :req [::x]) :a) ::s/problems first :pred))))
309309

310+
(deftest remove-def
311+
(is (= ::ABC (s/def ::ABC string?)))
312+
(is (= ::ABC (s/def ::ABC nil)))
313+
(is (nil? (s/get-spec ::ABC))))
314+
310315
(s/fdef foo.bar/cljs-2275
311316
:args (s/cat :k keyword?)
312317
:ret string?)

0 commit comments

Comments
 (0)