Line data Source code
1 4 : (ns schemas.api.common
2 : (:require
3 : [matchscore.config :as config])
4 : (:import
5 : (java.util Date)
6 : (java.time Instant)
7 : (java.time.format DateTimeParseException)))
8 :
9 :
10 : ;; ==============================
11 : ;; COMMON SCHEMAS
12 : ;; ==============================
13 36 : (def day-of-week-options #{"mon" "tue" "wed" "thu" "fri" "sat" "sun"})
14 :
15 :
16 16 : (def currency-options #{"UAH" "USD"})
17 :
18 :
19 16 : (def sport-options #{"squash" "tennis"})
20 :
21 :
22 24 : (def activity-type-options #{"friendly-play" "friendly-tournament" "tournament" "league"})
23 :
24 :
25 20 : (def score-entry-mode-options #{"sets" "game" "either"})
26 :
27 :
28 16 : (def score-entry-default-options #{"sets" "game"})
29 :
30 :
31 20 : (def visibility-options #{"public" "unlisted" "private"})
32 :
33 :
34 4 : (def NonNegativeNumber
35 8 : [:and
36 4 : number?
37 24 : [:fn {:error/message "Value must be non-negative"}
38 0 : #(not (neg? %))]])
39 :
40 :
41 16 : (def timezone-options #{"Europe/Kyiv" "UTC"})
42 :
43 :
44 4 : (def stages-template-type-options
45 24 : #{"groups_monrad" "groups_groups" "groups" "monrad" "qualification_monrad"})
46 :
47 :
48 56 : (def Datetime [:or [:fn #(instance? Date %)] [:fn #(instance? Instant %)]])
49 :
50 :
51 4 : (defn valid-date-time-string?
52 : [date-time]
53 0 : (try
54 0 : (Instant/parse date-time)
55 : true
56 : (catch DateTimeParseException _ false)))
57 :
58 :
59 4 : (def DatetimeStringSchema
60 8 : [:and
61 4 : string?
62 20 : [:fn {:error/message "Inappropriate ISO 8601 datetime string"}
63 4 : valid-date-time-string?]])
64 :
65 :
66 : ;; Validation functions
67 : ;; todo: refactor this function
68 : ;; https://grok.com/share/bGVnYWN5LWNvcHk_af451023-c761-400c-9e82-4704c054fb30
69 : ;; reason is that it fails even when the format of the datetime is wrong
70 4 : (defn valid-datetimes?
71 : [{:keys [start-datetime end-datetime]}]
72 0 : (try
73 0 : (let [start (Instant/parse start-datetime)
74 0 : end (Instant/parse end-datetime)]
75 0 : (.isBefore start end))
76 : (catch Exception _ false)))
77 :
78 :
79 : ;; Final schema with custom constraint for hours
80 4 : (defn valid-hours?
81 : [{:keys [start-hour end-hour frequency]}]
82 0 : (or (not= frequency "recurring") (and start-hour end-hour (> end-hour start-hour))))
83 :
84 :
85 8 : (def PostgresConn :any)
86 :
87 :
88 8 : (def RedisConn :any)
89 :
90 :
91 8 : (def GooseScheduler :any)
92 :
93 :
94 4 : (def AuthHeaders
95 8 : [:map
96 20 : [:twa-auth {:optional true}
97 12 : [:maybe :string]]
98 20 : [:telegram/id [:maybe :int]]
99 20 : [:user/uuid [:maybe :string]]])
100 :
101 :
102 : ;; Request shape for public endpoints (no auth) — the component keys injected by
103 : ;; `wrap-components`, without the `:auth-headers`/`:parsed-headers` that
104 : ;; `wrap-parse-headers` only attaches on authenticated routes.
105 4 : (def PublicRingRequest
106 8 : [:map
107 12 : [:config config/ConfigSchema]
108 12 : [:pg PostgresConn]
109 12 : [:redis RedisConn]
110 12 : [:goose GooseScheduler]])
111 :
112 :
113 40 : (def RingRequest (conj PublicRingRequest [:auth-headers AuthHeaders] [:parsed-headers AuthHeaders]))
114 :
115 :
116 8 : (def RingResponse :any)
117 :
118 :
119 4 : (def ValidationResultSchema
120 : "Return schema like [success? error-reason error-message] where error-reason is a keyword."
121 12 : [:tuple
122 : :boolean ; success?
123 16 : [:or :keyword :nil] ; error-reason
124 16 : [:or :string :nil]])
|