Line data Source code
1 4 : (ns schemas.domain.stages
2 : (:require
3 : [malli.core :as m]
4 : [malli.error :as me]))
5 :
6 :
7 4 : (def StageRecordSchema
8 28 : [:map {:swagger/title "StageRecord"
9 : :swagger/description "Complete stage 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 : [:class-id [:ref :types/UuidString]]
16 12 : [:order :int]
17 8 : [:name
18 44 : [:string {:min 1
19 : :max 255
20 : :description "Stage name"
21 : :example "Group Stage"}]]
22 8 : [:state
23 28 : [:string {:description "Stage state"
24 : :example "pending"}]]
25 20 : [:ruleset-id {:optional true}
26 20 : [:maybe [:ref :types/UuidString]]]
27 24 : [:created-datetime {:optional true}
28 : :any]
29 24 : [:updated-datetime {:optional true}
30 : :any]
31 28 : [:source {:description "Source system that created the stage"
32 : :default "matchscore"}
33 12 : [:ref :matchscore/source]]
34 20 : [:config {:optional true}
35 24 : [:maybe [:map-of :keyword :any]]]])
36 :
37 :
38 4 : (defrecord
39 : StageRecord
40 : [uuid
41 : public-id
42 : class-id
43 : order
44 : name
45 : state
46 : ruleset-id
47 : created-datetime
48 : updated-datetime
49 : source
50 : config])
51 :
52 :
53 4 : (defn make-stage-record
54 : "Creates a Stage 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 StageRecordSchema] StageRecordSchema]}
58 : [{:keys [uuid
59 : public-id
60 : class-id
61 : order
62 : name
63 : state
64 : ruleset-id
65 : created-datetime
66 : updated-datetime
67 : source
68 : config]
69 : :as stage-data}]
70 20 : (if (m/validate StageRecordSchema stage-data)
71 8 : (map->StageRecord
72 52 : {:uuid uuid
73 4 : :public-id public-id
74 4 : :class-id class-id
75 4 : :order order
76 4 : :name name
77 4 : :state state
78 4 : :ruleset-id ruleset-id
79 4 : :created-datetime created-datetime
80 4 : :updated-datetime updated-datetime
81 4 : :source source
82 4 : :config config})
83 0 : (throw
84 0 : (ex-info
85 : "Invalid stage data"
86 0 : {:type :internal-validation-error
87 0 : :schema StageRecordSchema
88 0 : :data stage-data
89 0 : :humanized-errors (me/humanize (m/explain StageRecordSchema stage-data))}))))
|