Line data Source code
1 4 : (ns schemas.domain.matches
2 : (:require
3 : [malli.core :as m]
4 : [malli.error :as me]))
5 :
6 :
7 4 : (def MatchRecordSchema
8 28 : [:map {:swagger/title "MatchRecord"
9 : :swagger/description "Complete match record from database"}
10 20 : [:uuid [:ref :types/UuidString]]
11 20 : [:home-participant-id {:optional true}
12 20 : [:maybe [:ref :types/UuidString]]]
13 20 : [:away-participant-id {:optional true}
14 20 : [:maybe [:ref :types/UuidString]]]
15 20 : [:event-id {:optional true}
16 20 : [:maybe [:ref :types/UuidString]]]
17 20 : [:stage-id {:optional true}
18 20 : [:maybe [:ref :types/UuidString]]]
19 20 : [:group-id {:optional true}
20 20 : [:maybe [:ref :types/UuidString]]]
21 20 : [:bracket-match-id {:optional true}
22 20 : [:maybe [:ref :types/UuidString]]]
23 20 : [:series-id {:optional true}
24 20 : [:maybe [:ref :types/UuidString]]]
25 20 : [:game-number {:optional true}
26 12 : [:maybe :int]]
27 20 : [:match-order {:optional true}
28 12 : [:maybe :int]]
29 8 : [:state
30 28 : [:string {:description "Match state"
31 : :example "scheduled"}]]
32 20 : [:ruleset-id {:optional true}
33 20 : [:maybe [:ref :types/UuidString]]]
34 24 : [:datetime-start {:optional true}
35 : :any]
36 24 : [:datetime-end {:optional true}
37 : :any]
38 20 : [:court {:optional true}
39 12 : [:maybe :string]]
40 24 : [:extra {:optional true}
41 : :any]
42 24 : [:created-datetime {:optional true}
43 : :any]
44 24 : [:updated-datetime {:optional true}
45 : :any]
46 20 : [:source [:ref :matchscore/source]]])
47 :
48 :
49 4 : (defrecord
50 : MatchRecord
51 : [uuid
52 : home-participant-id
53 : away-participant-id
54 : event-id
55 : stage-id
56 : group-id
57 : bracket-match-id
58 : series-id
59 : game-number
60 : match-order
61 : state
62 : ruleset-id
63 : datetime-start
64 : datetime-end
65 : court
66 : extra
67 : created-datetime
68 : updated-datetime
69 : source])
70 :
71 :
72 4 : (defn make-match-record
73 : "Creates a Match record with validation.
74 : Note: This validation uses the default registry. Ensure schemas.registry/init-registry!
75 : has been called before using this function."
76 : {:malli/schema [:=> [:cat MatchRecordSchema] MatchRecordSchema]}
77 : [{:keys [uuid
78 : home-participant-id
79 : away-participant-id
80 : event-id
81 : stage-id
82 : group-id
83 : bracket-match-id
84 : series-id
85 : game-number
86 : match-order
87 : state
88 : ruleset-id
89 : datetime-start
90 : datetime-end
91 : court
92 : extra
93 : created-datetime
94 : updated-datetime
95 : source]
96 : :as match-data}]
97 20 : (if (m/validate MatchRecordSchema match-data)
98 8 : (map->MatchRecord
99 84 : {:uuid uuid
100 4 : :home-participant-id home-participant-id
101 4 : :away-participant-id away-participant-id
102 4 : :event-id event-id
103 4 : :stage-id stage-id
104 4 : :group-id group-id
105 4 : :bracket-match-id bracket-match-id
106 4 : :series-id series-id
107 4 : :game-number game-number
108 4 : :match-order match-order
109 4 : :state state
110 4 : :ruleset-id ruleset-id
111 4 : :datetime-start datetime-start
112 4 : :datetime-end datetime-end
113 4 : :court court
114 4 : :extra extra
115 4 : :created-datetime created-datetime
116 4 : :updated-datetime updated-datetime
117 4 : :source source})
118 0 : (throw
119 0 : (ex-info
120 : "Invalid match data"
121 0 : {:type :internal-validation-error
122 0 : :schema MatchRecordSchema
123 0 : :data match-data
124 0 : :humanized-errors (me/humanize (m/explain MatchRecordSchema match-data))}))))
|