-
Notifications
You must be signed in to change notification settings - Fork 1
/
asserts.cljc
67 lines (59 loc) · 2.57 KB
/
asserts.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(ns com.yetanalytics.persephone.utils.asserts
(:require [clojure.spec.alpha :as s]
[com.yetanalytics.pan :as pan]
[com.yetanalytics.pan.objects.template :as pan-template]))
;; FIXME: We need to set the :relation? key to true, but currently this will
;; cause errors because external IRIs are not supported yet in project-pan.
(defn assert-profile
"Assert that `profile` conforms to the xAPI spec, or else throw an
exception."
[profile]
(when-some [err (pan/validate-profile profile :ids? true :print-errs? false)]
(throw (ex-info "Invalid Profile!"
{:kind ::invalid-profile
:errors err}))))
(defn assert-template
"Assert that `template` conforms to the xAPI spec, or else throw an
exception."
[template]
(when-some [err (s/explain-data ::pan-template/template template)]
(throw (ex-info "Invalid Statement Template!"
{:kind ::invalid-template
:errors err}))))
;; TODO: Make these asserts Project Pan's responsibility
(defn assert-profile-ids
"Assert that the Profile IDs do not clash, or else throw an exception."
[profiles]
(let [prof-ids (map :id profiles)]
(when (not= prof-ids (distinct prof-ids))
(throw (ex-info "Profile IDs are not unique!"
{:type ::non-unique-profile-ids
:ids prof-ids})))))
(defn assert-profile-template-ids
"Assert that the Profiles' Template IDs do not clash,
or else throw an exception."
[profiles]
(let [temp-ids (mapcat (fn [{:keys [templates]}] (map :id templates))
profiles)]
(when (not= temp-ids (distinct temp-ids))
(throw (ex-info "Template IDs are not unique!"
{:type ::non-unique-template-ids
:ids temp-ids})))))
(defn assert-profile-pattern-ids
"Assert that the Profiles' Pattern IDs do not clash,
or else throw an exception."
[profiles]
(let [pat-ids (mapcat (fn [{:keys [patterns]}] (map :id patterns))
profiles)]
(when (not= pat-ids (distinct pat-ids))
(throw (ex-info "Pattern IDs are not unique!"
{:type ::non-unique-pattern-ids
:ids pat-ids})))))
(defn assert-template-ids
"Assert that the Template IDs do not clash, or else throw an exception."
[templates]
(let [temp-ids (map :id templates)]
(when (not= temp-ids (distinct temp-ids))
(throw (ex-info "Template IDs are not unique!"
{:type ::non-unique-template-ids
:ids temp-ids})))))