Line data Source code
1 4 : (ns schemas.domain.class-templates
2 : (:require
3 : [malli.core :as m]
4 : [malli.error :as me]))
5 :
6 :
7 4 : (def ClassTemplateRecordSchema
8 28 : [:map {:swagger/title "ClassTemplateRecord"
9 : :swagger/description "Complete class template 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 : [:template-id [:ref :types/UuidString]]
16 8 : [:name
17 44 : [:string {:min 1
18 : :max 255
19 : :description "Class template name"
20 : :example "Beginner"}]]
21 20 : [:max-players {:optional true}
22 8 : [:maybe
23 36 : [:int {:min 1
24 : :description "Maximum number of players allowed"
25 : :example 16}]]]
26 12 : [:created-datetime :any]
27 12 : [:updated-datetime :any]
28 20 : [:created-by {:optional true}
29 20 : [:maybe [:ref :types/UuidString]]]])
30 :
31 :
32 4 : (defrecord
33 : ClassTemplateRecord
34 : [uuid public-id template-id name max-players created-datetime updated-datetime created-by])
35 :
36 :
37 4 : (defn make-class-template-record
38 : "Creates a ClassTemplate 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 :map] ClassTemplateRecordSchema]}
42 : [{:keys [uuid public-id template-id name max-players created-datetime updated-datetime created-by]
43 : :as class-template-data}]
44 20 : (if (m/validate ClassTemplateRecordSchema class-template-data)
45 8 : (map->ClassTemplateRecord
46 40 : {:uuid uuid
47 4 : :public-id public-id
48 4 : :template-id template-id
49 4 : :name name
50 4 : :max-players max-players
51 4 : :created-datetime created-datetime
52 4 : :updated-datetime updated-datetime
53 4 : :created-by created-by})
54 0 : (throw
55 0 : (ex-info
56 : "Invalid class template data"
57 0 : {:type :internal-validation-error
58 0 : :schema ClassTemplateRecordSchema
59 0 : :data class-template-data
60 0 : :humanized-errors (me/humanize (m/explain ClassTemplateRecordSchema class-template-data))}))))
|