@skalfa/skalfa-api-core 1.0.3 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +45 -0
- package/LICENSE +21 -0
- package/README.md +60 -0
- package/dist/auth/auth.d.ts +18 -15
- package/dist/auth/auth.js +20 -203
- package/dist/auth/auth.js.map +1 -1
- package/dist/auth/create-access-token.d.ts +4 -0
- package/dist/auth/create-access-token.js +26 -0
- package/dist/auth/create-access-token.js.map +1 -0
- package/dist/auth/create-user-mail-token.d.ts +4 -0
- package/dist/auth/create-user-mail-token.js +19 -0
- package/dist/auth/create-user-mail-token.js.map +1 -0
- package/dist/auth/helpers/generate-agent-id.d.ts +1 -0
- package/dist/auth/helpers/generate-agent-id.js +7 -0
- package/dist/auth/helpers/generate-agent-id.js.map +1 -0
- package/dist/auth/helpers/get-request-ip.d.ts +1 -0
- package/dist/auth/helpers/get-request-ip.js +4 -0
- package/dist/auth/helpers/get-request-ip.js.map +1 -0
- package/dist/auth/helpers/get-user-permissions.d.ts +1 -0
- package/dist/auth/helpers/get-user-permissions.js +9 -0
- package/dist/auth/helpers/get-user-permissions.js.map +1 -0
- package/dist/auth/helpers/index.d.ts +3 -0
- package/dist/auth/helpers/index.js +4 -0
- package/dist/auth/helpers/index.js.map +1 -0
- package/dist/auth/list-user-sessions.d.ts +1 -0
- package/dist/auth/list-user-sessions.js +10 -0
- package/dist/auth/list-user-sessions.js.map +1 -0
- package/dist/auth/revalidate-user-permissions-by-role.d.ts +1 -0
- package/dist/auth/revalidate-user-permissions-by-role.js +12 -0
- package/dist/auth/revalidate-user-permissions-by-role.js.map +1 -0
- package/dist/auth/revalidate-user-permissions.d.ts +1 -0
- package/dist/auth/revalidate-user-permissions.js +21 -0
- package/dist/auth/revalidate-user-permissions.js.map +1 -0
- package/dist/auth/revoke-access-token.d.ts +1 -0
- package/dist/auth/revoke-access-token.js +5 -0
- package/dist/auth/revoke-access-token.js.map +1 -0
- package/dist/auth/verify-access-token.d.ts +1 -0
- package/dist/auth/verify-access-token.js +47 -0
- package/dist/auth/verify-access-token.js.map +1 -0
- package/dist/auth/verify-user-mail-token.d.ts +1 -0
- package/dist/auth/verify-user-mail-token.js +21 -0
- package/dist/auth/verify-user-mail-token.js.map +1 -0
- package/dist/commands/cli.js +5 -3
- package/dist/commands/cli.js.map +1 -1
- package/dist/commands/make/basic-controller.js +1 -1
- package/dist/commands/make/basic-controller.js.map +1 -1
- package/dist/commands/make/basic-migration.d.ts +1 -1
- package/dist/commands/make/basic-migration.js +2 -2
- package/dist/commands/make/basic-migration.js.map +1 -1
- package/dist/commands/make/basic-model.js +1 -1
- package/dist/commands/make/basic-model.js.map +1 -1
- package/dist/commands/make/basic-seeder.js +1 -1
- package/dist/commands/make/basic-seeder.js.map +1 -1
- package/dist/commands/make/blueprint.js +1 -1
- package/dist/commands/make/blueprint.js.map +1 -1
- package/dist/commands/make/da-migration.js +3 -3
- package/dist/commands/make/da-migration.js.map +1 -1
- package/dist/commands/make/mail.js +2 -2
- package/dist/commands/make/mail.js.map +1 -1
- package/dist/commands/make/notification.js +1 -1
- package/dist/commands/make/notification.js.map +1 -1
- package/dist/commands/make/queue.js +1 -1
- package/dist/commands/make/queue.js.map +1 -1
- package/dist/commands/make/resource.d.ts +2 -0
- package/dist/commands/make/resource.js +19 -0
- package/dist/commands/make/resource.js.map +1 -0
- package/dist/commands/make/{light-controller.js → skalfa-controller.js} +3 -3
- package/dist/commands/make/skalfa-controller.js.map +1 -0
- package/dist/commands/make/{light-model.js → skalfa-model.js} +4 -4
- package/dist/commands/make/skalfa-model.js.map +1 -0
- package/dist/commands/runner/barrels.js.map +1 -1
- package/dist/commands/runner/blueprint/controller-generation.js.map +1 -1
- package/dist/commands/runner/blueprint/documentation-generation.js.map +1 -1
- package/dist/commands/runner/blueprint/migration-generation.js +1 -1
- package/dist/commands/runner/blueprint/migration-generation.js.map +1 -1
- package/dist/commands/runner/blueprint/model-generation.js.map +1 -1
- package/dist/commands/runner/blueprint/runner.js +2 -2
- package/dist/commands/runner/blueprint/runner.js.map +1 -1
- package/dist/commands/runner/blueprint/seeder-generation.js +1 -1
- package/dist/commands/runner/blueprint/seeder-generation.js.map +1 -1
- package/dist/commands/runner/da-migration.js +1 -2
- package/dist/commands/runner/da-migration.js.map +1 -1
- package/dist/commands/runner/migration.js +1 -1
- package/dist/commands/runner/migration.js.map +1 -1
- package/dist/commands/runner/seeder.js +1 -1
- package/dist/commands/runner/seeder.js.map +1 -1
- package/dist/context/context.js +6 -0
- package/dist/context/context.js.map +1 -1
- package/dist/controller/controller.d.ts +17 -30
- package/dist/controller/controller.js +39 -121
- package/dist/controller/controller.js.map +1 -1
- package/dist/controller/response.d.ts +6 -0
- package/dist/controller/response.js +63 -0
- package/dist/controller/response.js.map +1 -0
- package/dist/controller/storage.d.ts +9 -0
- package/dist/controller/storage.js +56 -0
- package/dist/controller/storage.js.map +1 -0
- package/dist/conversion/conversion.d.ts +3 -0
- package/dist/conversion/conversion.js +28 -4
- package/dist/conversion/conversion.js.map +1 -1
- package/dist/conversion/date.d.ts +1 -0
- package/dist/conversion/date.js +77 -0
- package/dist/conversion/date.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/logger/logger.js +33 -0
- package/dist/logger/logger.js.map +1 -1
- package/dist/mail/mail.js +6 -6
- package/dist/mail/mail.js.map +1 -1
- package/dist/middleware/access-log.d.ts +31 -0
- package/dist/middleware/access-log.js +13 -0
- package/dist/middleware/access-log.js.map +1 -0
- package/dist/middleware/auth.d.ts +37 -0
- package/dist/middleware/auth.js +16 -0
- package/dist/middleware/auth.js.map +1 -0
- package/dist/middleware/body-parse.d.ts +35 -0
- package/dist/middleware/body-parse.js +87 -0
- package/dist/middleware/body-parse.js.map +1 -0
- package/dist/middleware/context.d.ts +29 -0
- package/dist/middleware/context.js +8 -0
- package/dist/middleware/context.js.map +1 -0
- package/dist/middleware/cors.d.ts +31 -0
- package/dist/middleware/cors.js +27 -0
- package/dist/middleware/cors.js.map +1 -0
- package/dist/middleware/error-handler.d.ts +33 -0
- package/dist/middleware/error-handler.js +17 -0
- package/dist/middleware/error-handler.js.map +1 -0
- package/dist/middleware/middleware.d.ts +31 -10
- package/dist/middleware/middleware.js +41 -209
- package/dist/middleware/middleware.js.map +1 -1
- package/dist/middleware/private.d.ts +29 -0
- package/dist/middleware/private.js +8 -0
- package/dist/middleware/private.js.map +1 -0
- package/dist/middleware/rate-limiter.d.ts +32 -0
- package/dist/middleware/rate-limiter.js +30 -0
- package/dist/middleware/rate-limiter.js.map +1 -0
- package/dist/notification/index.d.ts +1 -0
- package/dist/notification/index.js +2 -0
- package/dist/notification/index.js.map +1 -0
- package/dist/notification/notification.d.ts +16 -0
- package/dist/notification/notification.js +64 -0
- package/dist/notification/notification.js.map +1 -0
- package/dist/permission/permission.js +9 -0
- package/dist/permission/permission.js.map +1 -1
- package/dist/registry/registry.d.ts +0 -6
- package/dist/registry/registry.js +6 -6
- package/dist/registry/registry.js.map +1 -1
- package/dist/storage/storage.d.ts +3 -3
- package/dist/storage/storage.js.map +1 -1
- package/dist/validation/validation.js +43 -51
- package/dist/validation/validation.js.map +1 -1
- package/package.json +4 -4
- package/src/auth/auth.ts +21 -252
- package/src/auth/create-access-token.ts +29 -0
- package/src/auth/create-user-mail-token.ts +24 -0
- package/src/auth/helpers/generate-agent-id.ts +8 -0
- package/src/auth/helpers/get-request-ip.ts +3 -0
- package/src/auth/helpers/get-user-permissions.ts +15 -0
- package/src/auth/helpers/index.ts +3 -0
- package/src/auth/list-user-sessions.ts +11 -0
- package/src/auth/revalidate-user-permissions-by-role.ts +13 -0
- package/src/auth/revalidate-user-permissions.ts +26 -0
- package/src/auth/revoke-access-token.ts +5 -0
- package/src/auth/verify-access-token.ts +56 -0
- package/src/auth/verify-user-mail-token.ts +24 -0
- package/src/commands/cli.ts +5 -3
- package/src/commands/make/basic-controller.ts +3 -1
- package/src/commands/make/basic-migration.ts +5 -3
- package/src/commands/make/basic-model.ts +3 -1
- package/src/commands/make/basic-seeder.ts +3 -1
- package/src/commands/make/blueprint.ts +3 -1
- package/src/commands/make/da-migration.ts +6 -5
- package/src/commands/make/mail.ts +4 -2
- package/src/commands/make/notification.ts +3 -1
- package/src/commands/make/queue.ts +3 -1
- package/src/commands/make/resource.ts +21 -0
- package/src/commands/make/{light-controller.ts → skalfa-controller.ts} +4 -2
- package/src/commands/make/{light-model.ts → skalfa-model.ts} +5 -3
- package/src/commands/runner/barrels.ts +4 -0
- package/src/commands/runner/blueprint/controller-generation.ts +2 -0
- package/src/commands/runner/blueprint/documentation-generation.ts +2 -0
- package/src/commands/runner/blueprint/migration-generation.ts +3 -1
- package/src/commands/runner/blueprint/model-generation.ts +2 -0
- package/src/commands/runner/blueprint/runner.ts +10 -2
- package/src/commands/runner/blueprint/seeder-generation.ts +3 -1
- package/src/commands/runner/da-migration.ts +3 -2
- package/src/commands/runner/migration.ts +1 -1
- package/src/commands/runner/seeder.ts +1 -1
- package/src/context/context.ts +23 -17
- package/src/controller/controller.ts +124 -239
- package/src/controller/response.ts +78 -0
- package/src/controller/storage.ts +78 -0
- package/src/conversion/conversion.ts +90 -64
- package/src/conversion/date.ts +74 -0
- package/src/index.ts +2 -0
- package/src/logger/logger.ts +217 -176
- package/src/mail/mail.ts +85 -85
- package/src/middleware/access-log.ts +15 -0
- package/src/middleware/auth.ts +19 -0
- package/src/middleware/body-parse.ts +83 -0
- package/src/middleware/context.ts +11 -0
- package/src/middleware/cors.ts +31 -0
- package/src/middleware/error-handler.ts +20 -0
- package/src/middleware/middleware.ts +91 -288
- package/src/middleware/private.ts +8 -0
- package/src/middleware/rate-limiter.ts +41 -0
- package/src/notification/index.ts +1 -0
- package/src/notification/notification.ts +86 -0
- package/src/permission/permission.ts +140 -136
- package/src/registry/registry.ts +17 -15
- package/src/route/route.ts +11 -11
- package/src/storage/storage.ts +104 -106
- package/src/validation/validation.ts +322 -346
- package/dist/auth.util.d.ts +0 -19
- package/dist/auth.util.js +0 -183
- package/dist/auth.util.js.map +0 -1
- package/dist/commands/make/light-controller.js.map +0 -1
- package/dist/commands/make/light-model.js.map +0 -1
- package/dist/context.util.d.ts +0 -7
- package/dist/context.util.js +0 -11
- package/dist/context.util.js.map +0 -1
- package/dist/controller.util.d.ts +0 -118
- package/dist/controller.util.js +0 -144
- package/dist/controller.util.js.map +0 -1
- package/dist/conversion.util.d.ts +0 -8
- package/dist/conversion.util.js +0 -52
- package/dist/conversion.util.js.map +0 -1
- package/dist/db/db.d.ts +0 -84
- package/dist/db/db.js +0 -177
- package/dist/db/db.js.map +0 -1
- package/dist/db/index.d.ts +0 -1
- package/dist/db/index.js +0 -2
- package/dist/db/index.js.map +0 -1
- package/dist/db.util.d.ts +0 -84
- package/dist/db.util.js +0 -177
- package/dist/db.util.js.map +0 -1
- package/dist/logger.util.d.ts +0 -30
- package/dist/logger.util.js +0 -126
- package/dist/logger.util.js.map +0 -1
- package/dist/mail.util.d.ts +0 -21
- package/dist/mail.util.js +0 -53
- package/dist/mail.util.js.map +0 -1
- package/dist/middleware.util.d.ts +0 -263
- package/dist/middleware.util.js +0 -233
- package/dist/middleware.util.js.map +0 -1
- package/dist/model/index.d.ts +0 -3
- package/dist/model/index.js +0 -4
- package/dist/model/index.js.map +0 -1
- package/dist/model/model.d.ts +0 -204
- package/dist/model/model.js +0 -1495
- package/dist/model/model.js.map +0 -1
- package/dist/model.util.d.ts +0 -204
- package/dist/model.util.js +0 -1495
- package/dist/model.util.js.map +0 -1
- package/dist/permission.util.d.ts +0 -38
- package/dist/permission.util.js +0 -91
- package/dist/permission.util.js.map +0 -1
- package/dist/registry.util.d.ts +0 -28
- package/dist/registry.util.js +0 -19
- package/dist/registry.util.js.map +0 -1
- package/dist/route.util.d.ts +0 -1
- package/dist/route.util.js +0 -12
- package/dist/route.util.js.map +0 -1
- package/dist/storage.util.d.ts +0 -56
- package/dist/storage.util.js +0 -82
- package/dist/storage.util.js.map +0 -1
- package/dist/validation.util.d.ts +0 -7
- package/dist/validation.util.js +0 -237
- package/dist/validation.util.js.map +0 -1
- /package/dist/commands/make/{light-controller.d.ts → skalfa-controller.d.ts} +0 -0
- /package/dist/commands/make/{light-model.d.ts → skalfa-model.d.ts} +0 -0
|
@@ -1,346 +1,322 @@
|
|
|
1
|
-
import validator from "validator"
|
|
2
|
-
import { db } from "@skalfa/skalfa-orm"
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
// ==========================>
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
| "
|
|
11
|
-
| "
|
|
12
|
-
| "
|
|
13
|
-
| "
|
|
14
|
-
| "
|
|
15
|
-
| "
|
|
16
|
-
| "
|
|
17
|
-
| "
|
|
18
|
-
| "
|
|
19
|
-
|
|
|
20
|
-
| `min
|
|
21
|
-
| `
|
|
22
|
-
| `max
|
|
23
|
-
| `
|
|
24
|
-
| `between
|
|
25
|
-
| `
|
|
26
|
-
| `in
|
|
27
|
-
| `
|
|
28
|
-
| `not_in
|
|
29
|
-
| `
|
|
30
|
-
| `same
|
|
31
|
-
| `
|
|
32
|
-
| `different
|
|
33
|
-
| `
|
|
34
|
-
| `regex
|
|
35
|
-
| `
|
|
36
|
-
| `unique
|
|
37
|
-
| `
|
|
38
|
-
| `exists
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
if (!validator.
|
|
133
|
-
addError(errors, field, `${field} harus berupa
|
|
134
|
-
}
|
|
135
|
-
break
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
case "
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
case "
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
if (
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
break
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
const
|
|
236
|
-
if (
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
segments
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
function getNestedValue(obj: any, path: string): any {
|
|
324
|
-
if (!obj || typeof obj !== "object") return undefined
|
|
325
|
-
|
|
326
|
-
const normalizedPath = path
|
|
327
|
-
.replace(/\[(\w+)\]/g, '.$1')
|
|
328
|
-
.replace(/\['([^']+)'\]/g, '.$1')
|
|
329
|
-
.replace(/\["([^"]+)"\]/g, '.$1')
|
|
330
|
-
|
|
331
|
-
return normalizedPath.split('.').reduce((acc, key) => {
|
|
332
|
-
if (acc && Object.prototype.hasOwnProperty.call(acc, key)) {
|
|
333
|
-
return acc[key]
|
|
334
|
-
}
|
|
335
|
-
return undefined
|
|
336
|
-
}, obj)
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
function normalizeRules(rules: ValidationRule[] | string): ValidationRule[] {
|
|
340
|
-
if (Array.isArray(rules)) return rules
|
|
341
|
-
return rules.split("|") as ValidationRule[]
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
function addError(errors: Record<string, string[]>, field: string, message: string) {
|
|
345
|
-
errors[field] = [...(errors[field] || []), message]
|
|
346
|
-
}
|
|
1
|
+
import validator from "validator"
|
|
2
|
+
import { db } from "@skalfa/skalfa-orm";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
// ==========================>
|
|
7
|
+
// ## Validation: Rules of validation
|
|
8
|
+
// ==========================>
|
|
9
|
+
export type ValidationRule =
|
|
10
|
+
| "required"
|
|
11
|
+
| "string"
|
|
12
|
+
| "numeric"
|
|
13
|
+
| "number"
|
|
14
|
+
| "boolean"
|
|
15
|
+
| "email"
|
|
16
|
+
| "url"
|
|
17
|
+
| "date"
|
|
18
|
+
| "confirmed"
|
|
19
|
+
| "array"
|
|
20
|
+
| `min:`
|
|
21
|
+
| `min:${number}`
|
|
22
|
+
| `max:`
|
|
23
|
+
| `max:${number}`
|
|
24
|
+
| `between:`
|
|
25
|
+
| `between:${number},${number}`
|
|
26
|
+
| `in:`
|
|
27
|
+
| `in:${string}`
|
|
28
|
+
| `not_in:`
|
|
29
|
+
| `not_in:${string}`
|
|
30
|
+
| `same:`
|
|
31
|
+
| `same:${string}`
|
|
32
|
+
| `different:`
|
|
33
|
+
| `different:${string}`
|
|
34
|
+
| `regex:`
|
|
35
|
+
| `regex:${string}`
|
|
36
|
+
| `unique:`
|
|
37
|
+
| `unique:${string},${string}`
|
|
38
|
+
| `exists:`
|
|
39
|
+
| `exists:${string},${string}`
|
|
40
|
+
|
|
41
|
+
export type ValidationRules = Record<string, ValidationRule[] | string>
|
|
42
|
+
|
|
43
|
+
export interface ValidationResult {
|
|
44
|
+
valid : boolean
|
|
45
|
+
errors : Record<string, string[]>
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
// =====================================>
|
|
51
|
+
// ## Validation: validate request data
|
|
52
|
+
// =====================================>
|
|
53
|
+
export async function validate(
|
|
54
|
+
data : Record<string, any>,
|
|
55
|
+
rules : ValidationRules
|
|
56
|
+
): Promise<ValidationResult> {
|
|
57
|
+
const errors: Record<string, string[]> = {}
|
|
58
|
+
|
|
59
|
+
for (const field in rules) {
|
|
60
|
+
const fieldRules = normalizeRules(rules[field])
|
|
61
|
+
|
|
62
|
+
if (field.includes("*")) {
|
|
63
|
+
const segments = field.split(".")
|
|
64
|
+
|
|
65
|
+
await nestedValidation({ value: data, segments, rules: fieldRules, fieldPath: "", data, errors })
|
|
66
|
+
|
|
67
|
+
continue
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const value = getNestedValue(data, field) ?? ""
|
|
71
|
+
|
|
72
|
+
await checkRules({ field, value, rules: fieldRules, data, errors })
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
valid: Object.keys(errors).length === 0,
|
|
77
|
+
errors
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
async function checkRules({ field, value, rules, data, errors } : { field: string, value: any, rules: ValidationRule[], data: any, errors: Record<string, string[]> }) {
|
|
83
|
+
for (const rule of rules) {
|
|
84
|
+
const [name, param] = rule.split(":") as [string, string | undefined]
|
|
85
|
+
|
|
86
|
+
switch (name) {
|
|
87
|
+
// === BASIC ===
|
|
88
|
+
case "required":
|
|
89
|
+
if (validator.isEmpty(String(value).trim())) {
|
|
90
|
+
addError(errors, field, `${field} wajib diisi`)
|
|
91
|
+
}
|
|
92
|
+
break
|
|
93
|
+
|
|
94
|
+
case "string":
|
|
95
|
+
case "text":
|
|
96
|
+
if (!value) break
|
|
97
|
+
if (typeof value !== "string") {
|
|
98
|
+
addError(errors, field, `${field} harus berupa string`)
|
|
99
|
+
}
|
|
100
|
+
break
|
|
101
|
+
|
|
102
|
+
case "numeric":
|
|
103
|
+
case "number":
|
|
104
|
+
if (!value) break
|
|
105
|
+
if (!validator.isNumeric(String(value))) {
|
|
106
|
+
addError(errors, field, `${field} harus berupa angka`)
|
|
107
|
+
}
|
|
108
|
+
break
|
|
109
|
+
|
|
110
|
+
case "boolean":
|
|
111
|
+
if (!(value === true || value === false || value === "true" || value === "false" || value === 1 || value === 0)) {
|
|
112
|
+
addError(errors, field, `${field} harus berupa boolean`)
|
|
113
|
+
}
|
|
114
|
+
break
|
|
115
|
+
|
|
116
|
+
case "email":
|
|
117
|
+
if (!value) break
|
|
118
|
+
if (!validator.isEmail(String(value))) {
|
|
119
|
+
addError(errors, field, `${field} harus berupa email yang valid`)
|
|
120
|
+
}
|
|
121
|
+
break
|
|
122
|
+
|
|
123
|
+
case "url":
|
|
124
|
+
if (!value) break
|
|
125
|
+
if (!validator.isURL(String(value))) {
|
|
126
|
+
addError(errors, field, `${field} harus berupa URL yang valid`)
|
|
127
|
+
}
|
|
128
|
+
break
|
|
129
|
+
|
|
130
|
+
case "date":
|
|
131
|
+
if (!value) break
|
|
132
|
+
if (!validator.isDate(String(value))) {
|
|
133
|
+
addError(errors, field, `${field} harus berupa tanggal yang valid`)
|
|
134
|
+
}
|
|
135
|
+
break
|
|
136
|
+
|
|
137
|
+
// === LENGTH ===
|
|
138
|
+
case "min": {
|
|
139
|
+
if (!value) break
|
|
140
|
+
const min = parseInt(param!)
|
|
141
|
+
if (!validator.isLength(String(value), { min })) {
|
|
142
|
+
addError(errors, field, `${field} minimal ${min} karakter`)
|
|
143
|
+
}
|
|
144
|
+
break
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
case "max": {
|
|
148
|
+
if (!value) break
|
|
149
|
+
const max = parseInt(param!)
|
|
150
|
+
if (!validator.isLength(String(value), { max })) {
|
|
151
|
+
addError(errors, field, `${field} maksimal ${max} karakter`)
|
|
152
|
+
}
|
|
153
|
+
break
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
case "between": {
|
|
157
|
+
if (!value) break
|
|
158
|
+
const [minVal, maxVal] = param!.split(",").map(Number)
|
|
159
|
+
if (!validator.isLength(String(value), { min: minVal, max: maxVal })) {
|
|
160
|
+
addError(errors, field, `${field} harus antara ${minVal} - ${maxVal} karakter`)
|
|
161
|
+
}
|
|
162
|
+
break
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// === SET MEMBERSHIP ===
|
|
166
|
+
case "in": {
|
|
167
|
+
if (!value) break
|
|
168
|
+
const allowed = param!.split(",")
|
|
169
|
+
if (!allowed.includes(String(value))) {
|
|
170
|
+
addError(errors, field, `${field} harus salah satu dari: ${allowed.join(", ")}`)
|
|
171
|
+
}
|
|
172
|
+
break
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
case "not_in": {
|
|
176
|
+
if (!value) break
|
|
177
|
+
const notAllowed = param!.split(",")
|
|
178
|
+
if (notAllowed.includes(String(value))) {
|
|
179
|
+
addError(errors, field, `${field} tidak boleh salah satu dari: ${notAllowed.join(", ")}`)
|
|
180
|
+
}
|
|
181
|
+
break
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
case "array": {
|
|
185
|
+
if (!value) break
|
|
186
|
+
if (!Array.isArray(value)) {
|
|
187
|
+
addError(errors, field, `${field} harus berupa array`)
|
|
188
|
+
}
|
|
189
|
+
break
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// === RELATIONAL ===
|
|
193
|
+
case "confirmed":
|
|
194
|
+
if (value !== getNestedValue(data, `${field}_confirmation`)) {
|
|
195
|
+
addError(errors, field, `${field} tidak sama dengan konfirmasi`)
|
|
196
|
+
}
|
|
197
|
+
break
|
|
198
|
+
|
|
199
|
+
case "same":
|
|
200
|
+
if (value !== getNestedValue(data, param!)) {
|
|
201
|
+
addError(errors, field, `${field} harus sama dengan ${param}`)
|
|
202
|
+
}
|
|
203
|
+
break
|
|
204
|
+
|
|
205
|
+
case "different":
|
|
206
|
+
if (value === getNestedValue(data, param!)) {
|
|
207
|
+
addError(errors, field, `${field} harus berbeda dengan ${param}`)
|
|
208
|
+
}
|
|
209
|
+
break
|
|
210
|
+
|
|
211
|
+
// === REGEX ===
|
|
212
|
+
case "regex":
|
|
213
|
+
if (!value) break
|
|
214
|
+
try {
|
|
215
|
+
const pattern = new RegExp(param!)
|
|
216
|
+
if (!pattern.test(String(value))) {
|
|
217
|
+
addError(errors, field, `${field} tidak sesuai format`)
|
|
218
|
+
}
|
|
219
|
+
} catch {
|
|
220
|
+
addError(errors, field, `Regex rule untuk ${field} tidak valid`)
|
|
221
|
+
}
|
|
222
|
+
break
|
|
223
|
+
|
|
224
|
+
// === DATABASE VALIDATION ===
|
|
225
|
+
case "unique": {
|
|
226
|
+
if (!value) break
|
|
227
|
+
const [table, column, exceptId] = param!.split(",")
|
|
228
|
+
const query = db.table(table).where(column, value)
|
|
229
|
+
if (exceptId) query.whereNot("id", exceptId)
|
|
230
|
+
|
|
231
|
+
if (await db.schema.hasColumn(table, "deleted_at")) {
|
|
232
|
+
query.whereNull("deleted_at")
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const existing = await query.first()
|
|
236
|
+
if (existing) {
|
|
237
|
+
addError(errors, field, `${field} sudah digunakan`)
|
|
238
|
+
}
|
|
239
|
+
break
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
case "exists": {
|
|
243
|
+
if (!value) break
|
|
244
|
+
const [table, column] = param!.split(",")
|
|
245
|
+
const query = db.table(table).where(column, value)
|
|
246
|
+
|
|
247
|
+
if (await db.schema.hasColumn(table, "deleted_at")) {
|
|
248
|
+
query.whereNull("deleted_at")
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const existing = await query.first()
|
|
252
|
+
if (!existing) {
|
|
253
|
+
addError(errors, field, `${field} tidak ditemukan di ${table}`)
|
|
254
|
+
}
|
|
255
|
+
break
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
async function nestedValidation({ value, segments, rules, fieldPath, data, errors }: {
|
|
264
|
+
value : any
|
|
265
|
+
segments : string[]
|
|
266
|
+
rules : ValidationRule[]
|
|
267
|
+
fieldPath : string
|
|
268
|
+
data : any
|
|
269
|
+
errors : Record<string, string[]>
|
|
270
|
+
}) {
|
|
271
|
+
if (segments.length === 0) {
|
|
272
|
+
await checkRules({ field: fieldPath, value, rules, data, errors })
|
|
273
|
+
|
|
274
|
+
return
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const [segment, ...rest] = segments
|
|
278
|
+
|
|
279
|
+
if (segment === "*") {
|
|
280
|
+
if (!Array.isArray(value)) {
|
|
281
|
+
addError(errors, fieldPath, `${fieldPath} harus berupa array`)
|
|
282
|
+
|
|
283
|
+
return
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
for (let i = 0; i < value.length; i++) {
|
|
287
|
+
await nestedValidation({ value: value[i], segments: rest, rules, fieldPath: `${fieldPath}.${i}`, data, errors })
|
|
288
|
+
}
|
|
289
|
+
} else {
|
|
290
|
+
await nestedValidation({ value: value?.[segment], segments: rest, rules, fieldPath: fieldPath ? `${fieldPath}.${segment}` : segment, data, errors})
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
// ==================================>
|
|
297
|
+
// ## Validation helpers
|
|
298
|
+
// ==================================>
|
|
299
|
+
function getNestedValue(obj: any, path: string): any {
|
|
300
|
+
if (!obj || typeof obj !== "object") return undefined
|
|
301
|
+
|
|
302
|
+
const normalizedPath = path
|
|
303
|
+
.replace(/\[(\w+)\]/g, '.$1')
|
|
304
|
+
.replace(/\['([^']+)'\]/g, '.$1')
|
|
305
|
+
.replace(/\["([^"]+)"\]/g, '.$1')
|
|
306
|
+
|
|
307
|
+
return normalizedPath.split('.').reduce((acc, key) => {
|
|
308
|
+
if (acc && Object.prototype.hasOwnProperty.call(acc, key)) return acc[key]
|
|
309
|
+
|
|
310
|
+
return undefined
|
|
311
|
+
}, obj)
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function normalizeRules(rules: ValidationRule[] | string): ValidationRule[] {
|
|
315
|
+
if (Array.isArray(rules)) return rules
|
|
316
|
+
|
|
317
|
+
return rules.split("|") as ValidationRule[]
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function addError(errors: Record<string, string[]>, field: string, message: string) {
|
|
321
|
+
errors[field] = [...(errors[field] || []), message]
|
|
322
|
+
}
|