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