Line data Source code
1 4 : (ns schemas.domain.advancements
2 : (:require
3 : [malli.core :as m]
4 : [malli.error :as me]))
5 :
6 :
7 4 : (def AdvancementRecordSchema
8 28 : [:map {:swagger/title "AdvancementRecord"
9 : :swagger/description "Complete advancement record from database"}
10 20 : [:uuid [:ref :types/UuidString]]
11 20 : [:rule-id [:ref :types/UuidString]]
12 20 : [:participant-id [:ref :types/UuidString]]
13 20 : [:to-draw-id [:ref :types/UuidString]]
14 20 : [:to-group-id {:optional true}
15 20 : [:maybe [:ref :types/UuidString]]]
16 20 : [:to-bracket-match-id {:optional true}
17 20 : [:maybe [:ref :types/UuidString]]]
18 20 : [:assigned-seed {:optional true}
19 12 : [:maybe :int]]
20 20 : [:assigned-slot {:optional true}
21 12 : [:maybe :int]]
22 24 : [:advanced-at {:optional true}
23 : :any]
24 20 : [:source [:ref :matchscore/source]]])
25 :
26 :
27 4 : (defrecord
28 : AdvancementRecord
29 : [uuid
30 : rule-id
31 : participant-id
32 : to-draw-id
33 : to-group-id
34 : to-bracket-match-id
35 : assigned-seed
36 : assigned-slot
37 : advanced-at
38 : source])
39 :
40 :
41 4 : (defn make-advancement-record
42 : "Creates an Advancement record with validation.
43 : Note: This validation uses the default registry. Ensure schemas.registry/init-registry!
44 : has been called before using this function."
45 : {:malli/schema [:=> [:cat AdvancementRecordSchema] AdvancementRecordSchema]}
46 : [{:keys [uuid
47 : rule-id
48 : participant-id
49 : to-draw-id
50 : to-group-id
51 : to-bracket-match-id
52 : assigned-seed
53 : assigned-slot
54 : advanced-at
55 : source]
56 : :as advancement-data}]
57 20 : (if (m/validate AdvancementRecordSchema advancement-data)
58 8 : (map->AdvancementRecord
59 48 : {:uuid uuid
60 4 : :rule-id rule-id
61 4 : :participant-id participant-id
62 4 : :to-draw-id to-draw-id
63 4 : :to-group-id to-group-id
64 4 : :to-bracket-match-id to-bracket-match-id
65 4 : :assigned-seed assigned-seed
66 4 : :assigned-slot assigned-slot
67 4 : :advanced-at advanced-at
68 4 : :source source})
69 0 : (throw
70 0 : (ex-info
71 : "Invalid advancement data"
72 0 : {:type :internal-validation-error
73 0 : :schema AdvancementRecordSchema
74 0 : :data advancement-data
75 0 : :humanized-errors (me/humanize (m/explain AdvancementRecordSchema advancement-data))}))))
|