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