From de8471bf1ddd261a3c512491d52035732983a5fc Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 22 Oct 2024 14:50:44 +0200 Subject: [PATCH] nilable --- src/sci/configs/cljs/spec/alpha.cljs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/sci/configs/cljs/spec/alpha.cljs b/src/sci/configs/cljs/spec/alpha.cljs index 08e3761..92dace6 100644 --- a/src/sci/configs/cljs/spec/alpha.cljs +++ b/src/sci/configs/cljs/spec/alpha.cljs @@ -89,12 +89,22 @@ (clojure.core/assert (clojure.core/and (even? (count key-pred-forms)) (every? keyword? keys)) "spec/or expects k1 p1 k2 p2..., where ks are keywords") `(s/or-spec-impl ~keys '~pf ~pred-forms nil))) +(macros/defmacro nilable + "returns a spec that accepts nil and values satisfiying pred" + [pred] + (let [&env (ctx/get-ctx) + pf (res &env pred)] + `(s/nilable-impl '~pf ~pred nil))) + (def namespaces {'cljs.spec.alpha {'def (sci/copy-var def* sns) 'def-impl (sci/copy-var s/def-impl sns) 'and (sci/copy-var and sns) 'and-spec-impl (sci/copy-var s/and-spec-impl sns) 'or (sci/copy-var or sns) 'or-spec-impl (sci/copy-var s/or-spec-impl sns) - 'valid? (sci/copy-var s/valid? sns)}}) + 'valid? (sci/copy-var s/valid? sns) + 'conform (sci/copy-var s/conform sns) + 'nilable (sci/copy-var nilable sns) + 'nilable-impl (sci/copy-var s/nilable-impl sns)}}) (def config {:namespaces namespaces})