Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 57 additions & 51 deletions src/xapi_schema/spec.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -659,55 +659,26 @@
(s/coll-of ::agent :kind vector? :into [] :gen-max 3))

(s/def ::identified-group
(s/keys :req [:group/objectType
(or :group/mbox
(s/with-gen
(s/and
(s/conformer
(partial conform-ns-map "group")
unform-ns-map)
(s/keys :req [:group/objectType
(or :group/mbox
:group/mbox_sha1sum
:group/openid
:group/account)]
:opt [:group/name
:group/member])
(restrict-keys :group/mbox
:group/mbox_sha1sum
:group/openid
:group/account)]
:opt [:group/name
:group/member]))

(s/def ::anonymous-group
(s/and
(s/keys :req [:group/objectType
:group/member]
:opt [:group/name])
#(-> % :group/member seq)))

(def identified-group?
(comp
some?
(some-fn :group/mbox
:group/mbox_sha1sum
:group/openid
:group/account)))

(defmulti group-type #(if (identified-group? %)
:group/identified
:group/anonymous))

(defmethod group-type :group/identified [_]
::identified-group)

(defmethod group-type :group/anonymous [_]
::anonymous-group)


(s/def ::group
(s/with-gen (s/and
(s/conformer
(partial conform-ns-map "group")
unform-ns-map)
(s/multi-spec group-type (fn [gen-val _]
gen-val))
(restrict-keys :group/mbox
:group/mbox_sha1sum
:group/openid
:group/account
:group/name
:group/objectType
:group/member)
max-one-ifi)
:group/account
:group/name
:group/objectType
:group/member)
max-one-ifi)
#(sgen/fmap
unform-ns-map
(s/gen (s/or :ifi-mbox
Expand All @@ -729,12 +700,47 @@
(s/keys :req [:group/account]
:opt [:group/member
:group/name
:group/objectType])
:anon
(s/keys :req [:group/member]
:opt [:group/name
:group/objectType]))))))

(s/def ::anonymous-group
(s/with-gen
(s/and
(s/conformer
(partial conform-ns-map "group")
unform-ns-map)
(s/keys :req [:group/objectType
:group/member]
:opt [:group/name])
(restrict-keys :group/objectType
:group/member
:group/name)
#(-> % :group/member not-empty))
#(sgen/fmap
unform-ns-map
(s/gen (s/keys :req [:group/member]
:opt [:group/name
:group/objectType])))))

(defn identified-group?
[group]
(-> group
(select-keys ["mbox" "mbox_sha1sum" "openid" "account"])
not-empty
boolean))

(defmulti group-type #(if (identified-group? %)
:group/identified
:group/anonymous))

(defmethod group-type :group/identified [_]
::identified-group)

(defmethod group-type :group/anonymous [_]
::anonymous-group)

(s/def ::group
(s/multi-spec group-type (fn [gen-val _] gen-val)))

;; Actor

(defmulti actor-type (fn [a]
Expand Down
17 changes: 12 additions & 5 deletions src/xapi_schema/spec/resources.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@
;; xAPI Resources

;; common

(s/def :xapi.common.param/agent
(json
(s/nonconforming ::xs/actor)))
(s/nonconforming
;; except for statement queries, groups are not allowed as agent params
::xs/agent)))

;; Statements
;; GET https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Communication.md#213-get-statements
Expand All @@ -71,7 +74,12 @@
:statement/id)

(s/def :xapi.statements.GET.request.params/agent
:xapi.common.param/agent)
(json
(s/nonconforming
;; anonymous groups are not allowed as agent params
;; identified gorups, on the other hand, are allowed
(s/or :agent ::xs/agent
:group ::xs/identified-group))))

(s/def :xapi.statements.GET.request.params/verb
::xs/iri)
Expand Down Expand Up @@ -194,8 +202,7 @@
:activity/id)

(s/def :xapi.document.params/agent
(json
(s/nonconforming ::xs/agent)))
:xapi.common.param/agent)

(s/def :xapi.document.params/registration
::xs/uuid)
Expand Down Expand Up @@ -249,7 +256,7 @@

;; Agents https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Communication.md#24-agents-resource
(s/def :xapi.agents.GET.request.params/agent
:xapi.document.params/agent)
:xapi.common.param/agent)

(s/def :xapi.agents.GET.request/params
(s/keys :req-un [:xapi.agents.GET.request.params/agent]))
Expand Down
21 changes: 17 additions & 4 deletions test/xapi_schema/spec/resources_test.cljc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns xapi-schema.spec.resources-test
(:require [clojure.test :refer [deftest is testing] :include-macros true]
(:require [clojure.test :refer [deftest is] :include-macros true]
[clojure.spec.alpha :as s :include-macros true]
[xapi-schema.spec.resources :as xsr :refer [*read-json-fn*
*write-json-fn*
Expand Down Expand Up @@ -30,18 +30,31 @@
{"foo" "bar"})))
(is (= "{\"foo\":\"bar\"}"
(s/unform json-string-conformer
"{\"foo\":\"bar\"}")))
)
"{\"foo\":\"bar\"}"))))

(deftest agent-param-test
(is (s/valid? :xapi.common.param/agent
"{\"mbox\":\"mailto:milt@yetanalytics.com\"}"))
(is (not (s/valid? :xapi.common.param/agent
"{\"mbox\":\"milt@yetanalytics.com\"}")))
(is (not (s/valid? :xapi.common.param/agent
"{\"email\":\"mailto:milt@yetanalytics.com\"}"))))
"{\"email\":\"mailto:milt@yetanalytics.com\"}")))
(is (not (s/valid? :xapi.common.param/agent
"{\"objectType\": \"Group\",
\"mbox\": \"mailto:group@example.com\",
\"member\": [{\"mbox\": \"mailto:foo@example.com\"}]}")))
(is (not (s/valid? :xapi.common.param/agent
"{\"objectType\": \"Group\",
\"member\": [{\"mbox\": \"mailto:foo@example.com\"}]}"))))

(deftest statements-get-params-test
(is (s/valid? :xapi.statements.GET.request.params/agent
"{\"objectType\": \"Group\",
\"mbox\": \"mailto:group@example.com\",
\"member\": [{\"mbox\": \"mailto:foo@example.com\"}]}"))
(is (not (s/valid? :xapi.statements.GET.request.params/agent
"{\"objectType\": \"Group\",
\"member\": [{\"mbox\": \"mailto:foo@example.com\"}]}")))
(is (s/valid? :xapi.statements.GET.request/params
{:statementId (str #?(:clj (java.util.UUID/randomUUID)
:cljs (random-uuid)))
Expand Down