Line data Source code
1 4 : (ns schemas.domain.brackets
2 : (:require
3 : [malli.core :as m]
4 : [malli.error :as me]))
5 :
6 :
7 4 : (def BracketRecordSchema
8 28 : [:map {:swagger/title "BracketRecord"
9 : :swagger/description "Complete bracket 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 20 : [:draw-id [:ref :types/UuidString]]
16 8 : [:name
17 44 : [:string {:min 1
18 : :max 255
19 : :description "Bracket name"
20 : :example "Winners Bracket"}]]
21 20 : [:type {:optional true}
22 12 : [:maybe :string]]
23 20 : [:ruleset-id {:optional true}
24 20 : [:maybe [:ref :types/UuidString]]]
25 24 : [:config {:optional true}
26 : :any]
27 24 : [:created-datetime {:optional true}
28 : :any]
29 20 : [:source [:ref :matchscore/source]]])
30 :
31 :
32 4 : (defrecord
33 : BracketRecord
34 : [uuid public-id draw-id name type ruleset-id config created-datetime source])
35 :
36 :
37 4 : (defn make-bracket-record
38 : "Creates a Bracket record with validation.
39 : Note: This validation uses the default registry. Ensure schemas.registry/init-registry!
40 : has been called before using this function."
41 : {:malli/schema [:=> [:cat BracketRecordSchema] BracketRecordSchema]}
42 : [{:keys [uuid public-id draw-id name type ruleset-id config created-datetime source]
43 : :as bracket-data}]
44 20 : (if (m/validate BracketRecordSchema bracket-data)
45 8 : (map->BracketRecord
46 44 : {:uuid uuid
47 4 : :public-id public-id
48 4 : :draw-id draw-id
49 4 : :name name
50 4 : :type type
51 4 : :ruleset-id ruleset-id
52 4 : :config config
53 4 : :created-datetime created-datetime
54 4 : :source source})
55 0 : (throw
56 0 : (ex-info
57 : "Invalid bracket data"
58 0 : {:type :internal-validation-error
59 0 : :schema BracketRecordSchema
60 0 : :data bracket-data
61 0 : :humanized-errors (me/humanize (m/explain BracketRecordSchema bracket-data))}))))
|