Line data Source code
1 4 : (ns schemas.domain.participants
2 : (:require
3 : [malli.core :as m]
4 : [malli.error :as me]))
5 :
6 :
7 4 : (def ParticipantRecordSchema
8 28 : [:map {:swagger/title "ParticipantRecord"
9 : :swagger/description "Complete participant record from database"}
10 20 : [:uuid [:ref :types/UuidString]]
11 8 : [:public-id
12 36 : [:string {:min 1
13 : :description "Public-facing nanoid identifier"
14 : :example "V1StGXR8_Z5j"}]]
15 28 : [:source {:description "Source system that created the participant"
16 : :default "matchscore"}
17 12 : [:ref :matchscore/source]]
18 20 : [:user-id {:optional true}
19 20 : [:maybe [:ref :types/UuidString]]]
20 20 : [:user-id-rankedin {:optional true}
21 20 : [:maybe [:ref :types/UuidString]]]
22 20 : [:team-id {:optional true}
23 20 : [:maybe [:ref :types/UuidString]]]
24 8 : [:type
25 40 : [:enum {:description "Participant type"
26 : :example "person"}
27 : "person"
28 : "team"
29 : "pair"]]
30 24 : [:created-datetime {:optional true}
31 : :any]])
32 :
33 :
34 4 : (defrecord
35 : ParticipantRecord
36 : [uuid public-id source user-id user-id-rankedin team-id type created-datetime])
37 :
38 :
39 4 : (defn make-participant-record
40 : "Creates a Participant record with validation.
41 : Note: This validation uses the default registry. Ensure schemas.registry/init-registry!
42 : has been called before using this function."
43 : {:malli/schema [:=> [:cat ParticipantRecordSchema] ParticipantRecordSchema]}
44 : [{:keys [uuid public-id source user-id user-id-rankedin team-id type created-datetime]
45 : :as participant-data}]
46 20 : (if (m/validate ParticipantRecordSchema participant-data)
47 8 : (map->ParticipantRecord
48 40 : {:uuid uuid
49 4 : :public-id public-id
50 4 : :source source
51 4 : :user-id user-id
52 4 : :user-id-rankedin user-id-rankedin
53 4 : :team-id team-id
54 4 : :type type
55 4 : :created-datetime created-datetime})
56 0 : (throw
57 0 : (ex-info
58 : "Invalid participant data"
59 0 : {:type :internal-validation-error
60 0 : :schema ParticipantRecordSchema
61 0 : :data participant-data
62 0 : :humanized-errors (me/humanize (m/explain ParticipantRecordSchema participant-data))}))))
|