-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.cljc
29 lines (25 loc) · 1.02 KB
/
api.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
(ns nedap.utils.spec.api
(:require
#?(:clj [clojure.spec.alpha :as spec] :cljs [cljs.spec.alpha :as spec])
[nedap.utils.spec.impl.check]
[spec-coerce.core :as coerce])
#?(:cljs (:require-macros [nedap.utils.spec.api :refer [check!]])))
#?(:clj
(defmacro check!
"Asserts validity, explaining the cause otherwise. Apt for :pre conditions.
`args` is a sequence of spec-val pairs."
[& args]
{:pre [(-> args count even?)]}
`(nedap.utils.spec.impl.check/check! ~@args)))
(defn coerce-map-indicating-invalidity
"Tries to coerce the map `m` according to spec `spec`.
If the coercion isn't possible, `::invalid? true` is associated to the map."
[spec m]
;; Very important: specs must be passed as keywords or symbols,
;; but never 'inline' as any other kind of objects.
;; Else spec-coerce will fail to coerce things.
{:pre [(check! qualified-ident? spec
map? m)]}
(let [m (coerce/coerce spec m)]
(cond-> m
(not (spec/valid? spec m)) (assoc ::invalid? true))))