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