pwi-plata-type 0.4.166 → 0.4.167

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.
Files changed (101) hide show
  1. package/README.md +2 -2
  2. package/__BUILD__/libs/sql.js +4 -1
  3. package/__BUILD__/libs/sql.js.map +1 -1
  4. package/package.json +53 -53
  5. package/src/@types/build.d.ts +26 -26
  6. package/src/@types/cluster.actions.d.ts +27 -27
  7. package/src/@types/cluster.d.ts +55 -55
  8. package/src/@types/create-cli.d.ts +8 -8
  9. package/src/@types/exported.d.ts +31 -31
  10. package/src/@types/fs.d.ts +6 -6
  11. package/src/@types/global.d.ts +113 -113
  12. package/src/@types/knex.d.ts +13 -13
  13. package/src/@types/model.d.ts +143 -143
  14. package/src/@types/router.d.ts +107 -107
  15. package/src/@types/sql.d.ts +75 -75
  16. package/src/@types/swagger.d.ts +22 -22
  17. package/src/bin/extras/create-api-cli.ts +218 -218
  18. package/src/bin/extras/create-cluster-cli.ts +160 -160
  19. package/src/bin/extras/create-lib-cli.ts +84 -84
  20. package/src/bin/extras/plata-create-tools.ts +97 -97
  21. package/src/bin/plata-api.ts +189 -189
  22. package/src/bin/plata-build.ts +17 -17
  23. package/src/bin/plata-cluster.ts +42 -42
  24. package/src/bin/plata-cmd.ts +18 -18
  25. package/src/bin/plata-create.ts +49 -49
  26. package/src/bin/plata-runtime.ts +131 -131
  27. package/src/bin/plata-swagger.ts +99 -99
  28. package/src/bin/runtime/_setupRuntime.ts +48 -48
  29. package/src/bin/runtime/_types.d.ts +7 -7
  30. package/src/bin/runtime/class.ts +20 -20
  31. package/src/bin/runtime/dirs.ts +25 -25
  32. package/src/bin/runtime/effect.ts +60 -60
  33. package/src/bin/runtime/fs.ts +10 -10
  34. package/src/bin/runtime/promises.ts +18 -18
  35. package/src/bin/runtime/require.ts +113 -113
  36. package/src/bin/runtime/sql.ts +12 -12
  37. package/src/bin/runtime/switch.ts +60 -60
  38. package/src/index.ts +19 -19
  39. package/src/libs/axios.ts +51 -51
  40. package/src/libs/build.ts +162 -162
  41. package/src/libs/cluster/cluster-controller.ts +93 -93
  42. package/src/libs/cluster.ts +330 -330
  43. package/src/libs/custom.ts +15 -15
  44. package/src/libs/env.ts +10 -10
  45. package/src/libs/fs.ts +399 -399
  46. package/src/libs/hot-reload.ts +63 -63
  47. package/src/libs/models/index.ts +4 -4
  48. package/src/libs/models/model-template-combine.ts +93 -93
  49. package/src/libs/models/model-template-env.ts +58 -58
  50. package/src/libs/models/model-template-union.ts +83 -83
  51. package/src/libs/models/model-template.ts +107 -107
  52. package/src/libs/models/model-validations.ts +443 -443
  53. package/src/libs/models/tools.ts +194 -194
  54. package/src/libs/router.ts +185 -185
  55. package/src/libs/sql.ts +792 -788
  56. package/src/libs/swagger.ts +81 -81
  57. package/src/libs/tools.ts +240 -240
  58. package/src/scripts/post-install/api.ts +32 -32
  59. package/src/scripts/post-install/cluster.ts +28 -28
  60. package/src/scripts/post-install/lib.ts +25 -25
  61. package/src/scripts/post-install.ts +64 -64
  62. package/src/scripts/pre-test.ts +33 -33
  63. package/src/tests/assets/sql/example.sql +76 -76
  64. package/src/tests/config.test.ts +12 -12
  65. package/src/tests/effect.test.ts +62 -62
  66. package/src/tests/model.env.test.ts +66 -66
  67. package/src/tests/model.test.ts +141 -141
  68. package/src/tests/model.union.test.ts +160 -160
  69. package/src/tests/router.test.ts +44 -44
  70. package/src/tests/sql.test.ts +502 -502
  71. package/src/tests/switch.test.ts +76 -76
  72. package/templates/create-cli/class.ts +13 -13
  73. package/templates/create-cli/cluster.ts +14 -14
  74. package/templates/create-cli/cmd.ts +3 -3
  75. package/templates/create-cli/config.ts +2 -2
  76. package/templates/create-cli/lib.ts +3 -3
  77. package/templates/create-cli/model.ts +14 -14
  78. package/templates/create-cli/route.ts +8 -8
  79. package/templates/create-cli/task.ts +13 -13
  80. package/templates/postinstall/api/.vscode/launch.json +20 -20
  81. package/templates/postinstall/api/Dockerfile +24 -24
  82. package/templates/postinstall/api/configs/env.ts +2 -2
  83. package/templates/postinstall/api/envs/debug.env +15 -15
  84. package/templates/postinstall/api/envs/homolog.env +15 -15
  85. package/templates/postinstall/api/envs/prod.env +15 -15
  86. package/templates/postinstall/api/tsconfig.json +24 -24
  87. package/templates/postinstall/cluster/.vscode/launch.json +20 -20
  88. package/templates/postinstall/cluster/Dockerfile +24 -24
  89. package/templates/postinstall/cluster/configs/env.ts +2 -2
  90. package/templates/postinstall/cluster/envs/debug.env +10 -10
  91. package/templates/postinstall/cluster/envs/homolog.env +10 -10
  92. package/templates/postinstall/cluster/envs/prod.env +10 -10
  93. package/templates/postinstall/cluster/tsconfig.json +24 -24
  94. package/templates/postinstall/lib/postinstall-tools.mjs +111 -111
  95. package/templates/postinstall/lib/postinstall.js +33 -33
  96. package/templates/postinstall/lib/tsconfig.json +22 -22
  97. package/templates/swagger-cli/header.json +21 -21
  98. package/templates/swagger-cli/route/body.json +14 -14
  99. package/templates/swagger-cli/route/header.json +18 -18
  100. package/templates/swagger-cli/route/response.json +53 -53
  101. package/tsconfig.json +28 -28
@@ -1,444 +1,444 @@
1
- export function Min(min: number): Model.PrimitiveValidationType<any> {
2
- return async (nome: string, valor: any) => {
3
- if (valor < min) return {
4
- error: {
5
- errorID: "BPMVMIN001",
6
- msg: `O valor de ${nome} tem que ser maior que ${min}`
7
- },
8
- continua: false,
9
- }
10
-
11
- return { continua: true }
12
- }
13
- }
14
-
15
- export function Max(max: number): Model.PrimitiveValidationType<any> {
16
- return async (nome: string, valor: any) => {
17
- if (valor > max) return {
18
- error: {
19
- errorID: "BPMVMIN001",
20
- msg: `O valor de ${nome} tem que ser maior que ${max}`
21
- },
22
- continua: false,
23
- }
24
-
25
- return { continua: true }
26
- }
27
- }
28
-
29
- export function Enum<const T>(valores: T[]): Model.PrimitiveValidationType<T> {
30
- return async (nome: string, valor: any) => {
31
- if (valores.findIndex(v => `${valor}` === `${v}`) === -1) return {
32
- error: {
33
- errorID: 'BPMVEN0001',
34
- msg: `O campo ${nome} so pode ter os valores: ${valores.join(', ')}`,
35
- error: `${valor}`
36
- },
37
- continua: false,
38
- }
39
-
40
- return { continua: true }
41
- }
42
- }
43
-
44
- export function StringDateTime(): Model.PrimitiveValidationType<string> {
45
- return async (nome: string, valor: any) => {
46
- const regex = /(\d{4})-([01]\d)-([0-3]\d)T([0-2]\d):([0-5]\d):([0-5]\d)/
47
-
48
- if (!regex.test(valor)) {
49
- return {
50
- error: {
51
- errorID: "BPVSDT001",
52
- msg: `A data no campo ${nome} tem que está no formato yyyy-mm-ddTHH:MM:SS`,
53
- },
54
- continua: false
55
- }
56
- }
57
-
58
- const [ _, ano, mes, dia, hora, minutos, segundos ] = valor.match(regex)
59
-
60
- if (isNaN(+ano) || isNaN(+mes) || isNaN(+dia) || isNaN(+hora) || isNaN(+minutos) || isNaN(+segundos) ){
61
- return {
62
- error: {
63
- errorID: "BPVSDT002",
64
- msg: `A data no campo ${nome} tem que está no formato yyyy-mm-ddTHH:MM:SS`
65
- },
66
- continua: false
67
- }
68
- }
69
-
70
- if ((+dia) > 31 || (+mes) > 12 || (+hora) > 24){
71
- return {
72
- error: {
73
- errorID: "BCVSDT003",
74
- msg: `A data no campo ${nome} tem que está no formato yyyy-mm-ddTHH:MM:SS`,
75
- },
76
- continua: false
77
- }
78
- }
79
-
80
- return { continua: true }
81
- }
82
- }
83
-
84
- export function DateTime(): Model.PrimitiveValidationType<Date> {
85
- return async (nome, value) => value instanceof Date ? { continua: true } : {
86
- error: {
87
- errorID: 'BPVDT001',
88
- msg: `O campo ${nome} não é do tipo Date`
89
- },
90
- continua: false
91
- }
92
- }
93
-
94
- export function Decimal(tamanhoInteiro: number, casasDecimais: number, min?: number, max?: number): Model.PrimitiveValidationType<number> {
95
- return async (nome: string, valor: any) => {
96
- if (isNaN(+valor)) {
97
- return {
98
- error: {
99
- errorID: "BPMVDEC001",
100
- msg: `O campo ${nome} tem que ser um decimal`
101
- },
102
- continua: false,
103
- }
104
- }
105
-
106
- const v = `${valor}`
107
-
108
- if (v.length > (tamanhoInteiro + casasDecimais + 1)) {
109
- return {
110
- error: {
111
- errorID: "BPMVDEC002",
112
- msg: `O campo ${nome} não pode ser maior que ${(tamanhoInteiro + casasDecimais + 1)}`
113
- },
114
- continua: false,
115
- }
116
- }
117
-
118
- const n = v.split('.')
119
-
120
- if (n[0].length > tamanhoInteiro) {
121
- return {
122
- error: {
123
- errorID: "BPMVDEC003",
124
- msg: `A parte inteira do ${nome} não pode ter mais de ${tamanhoInteiro} digitos`
125
- },
126
- continua: false,
127
- }
128
- }
129
-
130
- if (n[1] !== undefined) {
131
- if (n[1].length > casasDecimais) {
132
- return {
133
- error: {
134
- errorID: "BPMVDEC004",
135
- msg: `A parte decimal do ${nome} não pode ter mais de ${casasDecimais} digitos`
136
- },
137
- continua: false,
138
- }
139
- }
140
- }
141
-
142
- if (min !== undefined) {
143
- const result = await Min(min)(nome, valor)
144
-
145
- if (result.continua !== true)
146
- return result
147
- ;
148
- }
149
-
150
- if (max !== undefined) {
151
- const result = await Max(max)(nome, valor)
152
-
153
- if (result.continua !== true)
154
- return result
155
- ;
156
- }
157
-
158
- return { continua: true }
159
- }
160
- }
161
-
162
- export function Int(min?: number, max?: number): Model.PrimitiveValidationType<number> {
163
- return async (nome: string, valor: any) => {
164
- if (isNaN(+valor)) {
165
- return {
166
- error: {
167
- errorID: "BPMVINT001",
168
- msg: `O campo ${nome} tem que ser um número inteiro`
169
- },
170
- continua: false,
171
- }
172
- }
173
-
174
- const v = +valor
175
-
176
- if (Math.round(v) !== v) {
177
- return {
178
- error: {
179
- errorID: "BPMVINT002",
180
- msg: `O campo ${nome} tem que ser um número inteiro`
181
- },
182
- continua: false,
183
- }
184
- }
185
-
186
- if (min !== undefined) {
187
- const result = await Min(min)(nome, valor)
188
-
189
- if (result.continua !== true)
190
- return result
191
- ;
192
- }
193
-
194
- if (max !== undefined) {
195
- const result = await Max(max)(nome, valor)
196
-
197
- if (result.continua !== true)
198
- return result
199
- ;
200
- }
201
-
202
- return { continua: true }
203
- }
204
- }
205
-
206
- export function Optional(): Model.PrimitiveValidationType<any, null> {
207
- return async (_nome: string, valor: any) => {
208
- if (!valor) {
209
- return { continua: false }
210
- }
211
-
212
- return { continua: true }
213
- }
214
- }
215
-
216
- export function Required(): Model.PrimitiveValidationType {
217
- return async (nome: string, valor: any) => {
218
- if (valor === null) {
219
- return {
220
- error: {
221
- errorID: "BPCVREQ001",
222
- msg: `O campo ${nome} é obrigatório`
223
- },
224
- continua: false
225
- }
226
- }
227
-
228
- return { continua: true }
229
- }
230
- }
231
-
232
- export function SmallDateTime(): Model.PrimitiveValidationType<string> {
233
- return async (nome: string, valor: any) => {
234
- const regex = /(\d{4})-([01]\d)-([0-3]\d)/
235
-
236
- if (!regex.test(valor)) {
237
- return {
238
- error: {
239
- errorID: "BPCVDT001",
240
- msg: `A data no campo ${nome} tem que está no formato yyyy-mm-dd`
241
- },
242
- continua: false
243
- }
244
- }
245
-
246
- const [ _, ano, mes, dia ] = valor.match(regex)
247
-
248
- if (isNaN(+ano) || isNaN(+mes) || isNaN(+dia)){
249
- return {
250
- error: {
251
- errorID: "BPCVDT002",
252
- msg: `A data no campo ${nome} tem que está no formato yyyy-mm-dd`
253
- },
254
- continua: false
255
- }
256
- }
257
-
258
- if ((+dia) > 31 || (+mes) > 12 ){
259
- return {
260
- error: {
261
- errorID: "BPCVDT003",
262
- msg: `A data no campo ${nome} tem que está no formato yyyy-mm-dd`
263
- },
264
- continua: false
265
- }
266
- }
267
-
268
- //https://sqltutorialtips.blogspot.com/2016/11/smalldatetime-vs-datetime.html
269
- if (new Date(+ano, mes - 1, +dia) >= new Date(2079, 11, 31)) {
270
- return {
271
- error: {
272
- errorID: "BPCVDT004",
273
- msg: `A data no campo ${nome} não pode ser maior que 2079-12-31`
274
- },
275
- continua: false
276
- }
277
- }
278
-
279
- return { continua: true }
280
- }
281
- }
282
-
283
- export function Telefone(): Model.PrimitiveValidationType<string> {
284
- return async (nome: string, valor: any) => {
285
- const v = `${valor}`.split('-')
286
-
287
- if (v.length !== 2) {
288
- return {
289
- error: {
290
- errorID: "BPVTEL001",
291
- msg: `O campo ${nome} tem que ser um telefone em no formato 99-999999999`
292
- },
293
- continua: false
294
- }
295
- }
296
-
297
- if (v[0].length !== 2 || (v[1].length !== 8 && v[1].length !== 9)) {
298
- return {
299
- error: {
300
- errorID: "BPVTEL002",
301
- msg: `O campo ${nome} tem que ser um telefone em no formato 99-999999999`
302
- },
303
- continua: false
304
- }
305
- }
306
-
307
- if (!+v[0] || !+v[1]) {
308
- return {
309
- error: {
310
- errorID: "BPVTEL003",
311
- msg: `O campo ${nome} tem que ser um telefone em no formato 99-999999999`
312
- },
313
- continua: false
314
- }
315
- }
316
-
317
- return { continua: true }
318
- }
319
- }
320
-
321
- export function TelefoneDDI(): Model.PrimitiveValidationType<string> {
322
- return async (nome: string, valor: any) => {
323
- const [ ddi, t ] = `${valor}`.split(' ')
324
- const telefone = `${t}`.split('-')
325
-
326
- if (telefone.length !== 2) {
327
- return {
328
- error: {
329
- errorID: "BPVTELD001",
330
- msg: `O campo ${nome} tem que ser um telefone em no formato +000 99-999999999`
331
- },
332
- continua: false
333
- }
334
- }
335
-
336
- if (telefone[0].length !== 2 || (telefone[1].length !== 8 && telefone[1].length !== 9)) {
337
- return {
338
- error: {
339
- errorID: "BPVTELD002",
340
- msg: `O campo ${nome} tem que ser um telefone em no formato +000 99-999999999`
341
- },
342
- continua: false
343
- }
344
- }
345
-
346
- if (!+telefone[0] || !+telefone[1]) {
347
- return {
348
- error: {
349
- errorID: "BPVTELD003",
350
- msg: `O campo ${nome} tem que ser um telefone em no formato +000 99-999999999`
351
- },
352
- continua: false
353
- }
354
- }
355
-
356
- if (ddi.length !== 4) {
357
- return {
358
- error: {
359
- errorID: "BPVTELD004",
360
- msg: `O campo ${nome} tem que ser um telefone em no formato +000 99-999999999`
361
- },
362
- continua: false
363
- }
364
- }
365
-
366
- if (ddi[0] !== '+' || !+ddi) {
367
- return {
368
- error: {
369
- errorID: "BPVTELD005",
370
- msg: `O campo ${nome} tem que ser um telefone em no formato +000 99-999999999`
371
- },
372
- continua: false
373
- }
374
- }
375
-
376
- return { continua: true }
377
- }
378
- }
379
-
380
- export function VarChar(max: number, min?: number): Model.PrimitiveValidationType<string> {
381
- return async (nome: string, valor: any) => {
382
- if (`${valor}`.length > max) {
383
- return {
384
- error: {
385
- errorID: "BPVVARC001",
386
- msg: `O campo ${nome} tem que ter menos de ${max} caracteres`
387
- },
388
- continua: false
389
- }
390
- }
391
-
392
- if (min !== undefined) {
393
- if (`${valor}`.length < min) {
394
- return {
395
- error: {
396
- errorID: "BPVVARC002",
397
- msg: `O campo ${nome} tem que ter no mínimo ${min} caracteres`
398
- },
399
- continua: false
400
- }
401
- }
402
- }
403
-
404
- return { continua: true }
405
- }
406
- }
407
-
408
- export function useModel<const T extends Model.ModelClass>(model: T): Model.PrimitiveValidationType<T['type']> {
409
- const f: Model.PrimitiveValidationType<T['type']> = async (nome: string, valor: any) => {
410
- if (typeof valor !== 'object') {
411
- return {
412
- continua: false,
413
- error: {
414
- errorID: 'PLMVALUSML002',
415
- msg: `O valor de ${nome} precisa ser um objeto`,
416
- error: valor
417
- }
418
- }
419
- }
420
-
421
- const result = await model.validate(valor, true)
422
-
423
- if (result.errors !== undefined) {
424
- return {
425
- continua: false,
426
- error: {
427
- errorID: 'PLMVALUSML001',
428
- msg: `Exitem problemas dentro do objeto ${nome}`,
429
- error: result.errors
430
- }
431
- }
432
- }
433
-
434
- return {
435
- continua: true
436
- }
437
- }
438
-
439
- if (model.filter !== undefined) {
440
- f.filter = model.filter.bind(model)
441
- }
442
-
443
- return f
1
+ export function Min(min: number): Model.PrimitiveValidationType<any> {
2
+ return async (nome: string, valor: any) => {
3
+ if (valor < min) return {
4
+ error: {
5
+ errorID: "BPMVMIN001",
6
+ msg: `O valor de ${nome} tem que ser maior que ${min}`
7
+ },
8
+ continua: false,
9
+ }
10
+
11
+ return { continua: true }
12
+ }
13
+ }
14
+
15
+ export function Max(max: number): Model.PrimitiveValidationType<any> {
16
+ return async (nome: string, valor: any) => {
17
+ if (valor > max) return {
18
+ error: {
19
+ errorID: "BPMVMIN001",
20
+ msg: `O valor de ${nome} tem que ser maior que ${max}`
21
+ },
22
+ continua: false,
23
+ }
24
+
25
+ return { continua: true }
26
+ }
27
+ }
28
+
29
+ export function Enum<const T>(valores: T[]): Model.PrimitiveValidationType<T> {
30
+ return async (nome: string, valor: any) => {
31
+ if (valores.findIndex(v => `${valor}` === `${v}`) === -1) return {
32
+ error: {
33
+ errorID: 'BPMVEN0001',
34
+ msg: `O campo ${nome} so pode ter os valores: ${valores.join(', ')}`,
35
+ error: `${valor}`
36
+ },
37
+ continua: false,
38
+ }
39
+
40
+ return { continua: true }
41
+ }
42
+ }
43
+
44
+ export function StringDateTime(): Model.PrimitiveValidationType<string> {
45
+ return async (nome: string, valor: any) => {
46
+ const regex = /(\d{4})-([01]\d)-([0-3]\d)T([0-2]\d):([0-5]\d):([0-5]\d)/
47
+
48
+ if (!regex.test(valor)) {
49
+ return {
50
+ error: {
51
+ errorID: "BPVSDT001",
52
+ msg: `A data no campo ${nome} tem que está no formato yyyy-mm-ddTHH:MM:SS`,
53
+ },
54
+ continua: false
55
+ }
56
+ }
57
+
58
+ const [ _, ano, mes, dia, hora, minutos, segundos ] = valor.match(regex)
59
+
60
+ if (isNaN(+ano) || isNaN(+mes) || isNaN(+dia) || isNaN(+hora) || isNaN(+minutos) || isNaN(+segundos) ){
61
+ return {
62
+ error: {
63
+ errorID: "BPVSDT002",
64
+ msg: `A data no campo ${nome} tem que está no formato yyyy-mm-ddTHH:MM:SS`
65
+ },
66
+ continua: false
67
+ }
68
+ }
69
+
70
+ if ((+dia) > 31 || (+mes) > 12 || (+hora) > 24){
71
+ return {
72
+ error: {
73
+ errorID: "BCVSDT003",
74
+ msg: `A data no campo ${nome} tem que está no formato yyyy-mm-ddTHH:MM:SS`,
75
+ },
76
+ continua: false
77
+ }
78
+ }
79
+
80
+ return { continua: true }
81
+ }
82
+ }
83
+
84
+ export function DateTime(): Model.PrimitiveValidationType<Date> {
85
+ return async (nome, value) => value instanceof Date ? { continua: true } : {
86
+ error: {
87
+ errorID: 'BPVDT001',
88
+ msg: `O campo ${nome} não é do tipo Date`
89
+ },
90
+ continua: false
91
+ }
92
+ }
93
+
94
+ export function Decimal(tamanhoInteiro: number, casasDecimais: number, min?: number, max?: number): Model.PrimitiveValidationType<number> {
95
+ return async (nome: string, valor: any) => {
96
+ if (isNaN(+valor)) {
97
+ return {
98
+ error: {
99
+ errorID: "BPMVDEC001",
100
+ msg: `O campo ${nome} tem que ser um decimal`
101
+ },
102
+ continua: false,
103
+ }
104
+ }
105
+
106
+ const v = `${valor}`
107
+
108
+ if (v.length > (tamanhoInteiro + casasDecimais + 1)) {
109
+ return {
110
+ error: {
111
+ errorID: "BPMVDEC002",
112
+ msg: `O campo ${nome} não pode ser maior que ${(tamanhoInteiro + casasDecimais + 1)}`
113
+ },
114
+ continua: false,
115
+ }
116
+ }
117
+
118
+ const n = v.split('.')
119
+
120
+ if (n[0].length > tamanhoInteiro) {
121
+ return {
122
+ error: {
123
+ errorID: "BPMVDEC003",
124
+ msg: `A parte inteira do ${nome} não pode ter mais de ${tamanhoInteiro} digitos`
125
+ },
126
+ continua: false,
127
+ }
128
+ }
129
+
130
+ if (n[1] !== undefined) {
131
+ if (n[1].length > casasDecimais) {
132
+ return {
133
+ error: {
134
+ errorID: "BPMVDEC004",
135
+ msg: `A parte decimal do ${nome} não pode ter mais de ${casasDecimais} digitos`
136
+ },
137
+ continua: false,
138
+ }
139
+ }
140
+ }
141
+
142
+ if (min !== undefined) {
143
+ const result = await Min(min)(nome, valor)
144
+
145
+ if (result.continua !== true)
146
+ return result
147
+ ;
148
+ }
149
+
150
+ if (max !== undefined) {
151
+ const result = await Max(max)(nome, valor)
152
+
153
+ if (result.continua !== true)
154
+ return result
155
+ ;
156
+ }
157
+
158
+ return { continua: true }
159
+ }
160
+ }
161
+
162
+ export function Int(min?: number, max?: number): Model.PrimitiveValidationType<number> {
163
+ return async (nome: string, valor: any) => {
164
+ if (isNaN(+valor)) {
165
+ return {
166
+ error: {
167
+ errorID: "BPMVINT001",
168
+ msg: `O campo ${nome} tem que ser um número inteiro`
169
+ },
170
+ continua: false,
171
+ }
172
+ }
173
+
174
+ const v = +valor
175
+
176
+ if (Math.round(v) !== v) {
177
+ return {
178
+ error: {
179
+ errorID: "BPMVINT002",
180
+ msg: `O campo ${nome} tem que ser um número inteiro`
181
+ },
182
+ continua: false,
183
+ }
184
+ }
185
+
186
+ if (min !== undefined) {
187
+ const result = await Min(min)(nome, valor)
188
+
189
+ if (result.continua !== true)
190
+ return result
191
+ ;
192
+ }
193
+
194
+ if (max !== undefined) {
195
+ const result = await Max(max)(nome, valor)
196
+
197
+ if (result.continua !== true)
198
+ return result
199
+ ;
200
+ }
201
+
202
+ return { continua: true }
203
+ }
204
+ }
205
+
206
+ export function Optional(): Model.PrimitiveValidationType<any, null> {
207
+ return async (_nome: string, valor: any) => {
208
+ if (!valor) {
209
+ return { continua: false }
210
+ }
211
+
212
+ return { continua: true }
213
+ }
214
+ }
215
+
216
+ export function Required(): Model.PrimitiveValidationType {
217
+ return async (nome: string, valor: any) => {
218
+ if (valor === null) {
219
+ return {
220
+ error: {
221
+ errorID: "BPCVREQ001",
222
+ msg: `O campo ${nome} é obrigatório`
223
+ },
224
+ continua: false
225
+ }
226
+ }
227
+
228
+ return { continua: true }
229
+ }
230
+ }
231
+
232
+ export function SmallDateTime(): Model.PrimitiveValidationType<string> {
233
+ return async (nome: string, valor: any) => {
234
+ const regex = /(\d{4})-([01]\d)-([0-3]\d)/
235
+
236
+ if (!regex.test(valor)) {
237
+ return {
238
+ error: {
239
+ errorID: "BPCVDT001",
240
+ msg: `A data no campo ${nome} tem que está no formato yyyy-mm-dd`
241
+ },
242
+ continua: false
243
+ }
244
+ }
245
+
246
+ const [ _, ano, mes, dia ] = valor.match(regex)
247
+
248
+ if (isNaN(+ano) || isNaN(+mes) || isNaN(+dia)){
249
+ return {
250
+ error: {
251
+ errorID: "BPCVDT002",
252
+ msg: `A data no campo ${nome} tem que está no formato yyyy-mm-dd`
253
+ },
254
+ continua: false
255
+ }
256
+ }
257
+
258
+ if ((+dia) > 31 || (+mes) > 12 ){
259
+ return {
260
+ error: {
261
+ errorID: "BPCVDT003",
262
+ msg: `A data no campo ${nome} tem que está no formato yyyy-mm-dd`
263
+ },
264
+ continua: false
265
+ }
266
+ }
267
+
268
+ //https://sqltutorialtips.blogspot.com/2016/11/smalldatetime-vs-datetime.html
269
+ if (new Date(+ano, mes - 1, +dia) >= new Date(2079, 11, 31)) {
270
+ return {
271
+ error: {
272
+ errorID: "BPCVDT004",
273
+ msg: `A data no campo ${nome} não pode ser maior que 2079-12-31`
274
+ },
275
+ continua: false
276
+ }
277
+ }
278
+
279
+ return { continua: true }
280
+ }
281
+ }
282
+
283
+ export function Telefone(): Model.PrimitiveValidationType<string> {
284
+ return async (nome: string, valor: any) => {
285
+ const v = `${valor}`.split('-')
286
+
287
+ if (v.length !== 2) {
288
+ return {
289
+ error: {
290
+ errorID: "BPVTEL001",
291
+ msg: `O campo ${nome} tem que ser um telefone em no formato 99-999999999`
292
+ },
293
+ continua: false
294
+ }
295
+ }
296
+
297
+ if (v[0].length !== 2 || (v[1].length !== 8 && v[1].length !== 9)) {
298
+ return {
299
+ error: {
300
+ errorID: "BPVTEL002",
301
+ msg: `O campo ${nome} tem que ser um telefone em no formato 99-999999999`
302
+ },
303
+ continua: false
304
+ }
305
+ }
306
+
307
+ if (!+v[0] || !+v[1]) {
308
+ return {
309
+ error: {
310
+ errorID: "BPVTEL003",
311
+ msg: `O campo ${nome} tem que ser um telefone em no formato 99-999999999`
312
+ },
313
+ continua: false
314
+ }
315
+ }
316
+
317
+ return { continua: true }
318
+ }
319
+ }
320
+
321
+ export function TelefoneDDI(): Model.PrimitiveValidationType<string> {
322
+ return async (nome: string, valor: any) => {
323
+ const [ ddi, t ] = `${valor}`.split(' ')
324
+ const telefone = `${t}`.split('-')
325
+
326
+ if (telefone.length !== 2) {
327
+ return {
328
+ error: {
329
+ errorID: "BPVTELD001",
330
+ msg: `O campo ${nome} tem que ser um telefone em no formato +000 99-999999999`
331
+ },
332
+ continua: false
333
+ }
334
+ }
335
+
336
+ if (telefone[0].length !== 2 || (telefone[1].length !== 8 && telefone[1].length !== 9)) {
337
+ return {
338
+ error: {
339
+ errorID: "BPVTELD002",
340
+ msg: `O campo ${nome} tem que ser um telefone em no formato +000 99-999999999`
341
+ },
342
+ continua: false
343
+ }
344
+ }
345
+
346
+ if (!+telefone[0] || !+telefone[1]) {
347
+ return {
348
+ error: {
349
+ errorID: "BPVTELD003",
350
+ msg: `O campo ${nome} tem que ser um telefone em no formato +000 99-999999999`
351
+ },
352
+ continua: false
353
+ }
354
+ }
355
+
356
+ if (ddi.length !== 4) {
357
+ return {
358
+ error: {
359
+ errorID: "BPVTELD004",
360
+ msg: `O campo ${nome} tem que ser um telefone em no formato +000 99-999999999`
361
+ },
362
+ continua: false
363
+ }
364
+ }
365
+
366
+ if (ddi[0] !== '+' || !+ddi) {
367
+ return {
368
+ error: {
369
+ errorID: "BPVTELD005",
370
+ msg: `O campo ${nome} tem que ser um telefone em no formato +000 99-999999999`
371
+ },
372
+ continua: false
373
+ }
374
+ }
375
+
376
+ return { continua: true }
377
+ }
378
+ }
379
+
380
+ export function VarChar(max: number, min?: number): Model.PrimitiveValidationType<string> {
381
+ return async (nome: string, valor: any) => {
382
+ if (`${valor}`.length > max) {
383
+ return {
384
+ error: {
385
+ errorID: "BPVVARC001",
386
+ msg: `O campo ${nome} tem que ter menos de ${max} caracteres`
387
+ },
388
+ continua: false
389
+ }
390
+ }
391
+
392
+ if (min !== undefined) {
393
+ if (`${valor}`.length < min) {
394
+ return {
395
+ error: {
396
+ errorID: "BPVVARC002",
397
+ msg: `O campo ${nome} tem que ter no mínimo ${min} caracteres`
398
+ },
399
+ continua: false
400
+ }
401
+ }
402
+ }
403
+
404
+ return { continua: true }
405
+ }
406
+ }
407
+
408
+ export function useModel<const T extends Model.ModelClass>(model: T): Model.PrimitiveValidationType<T['type']> {
409
+ const f: Model.PrimitiveValidationType<T['type']> = async (nome: string, valor: any) => {
410
+ if (typeof valor !== 'object') {
411
+ return {
412
+ continua: false,
413
+ error: {
414
+ errorID: 'PLMVALUSML002',
415
+ msg: `O valor de ${nome} precisa ser um objeto`,
416
+ error: valor
417
+ }
418
+ }
419
+ }
420
+
421
+ const result = await model.validate(valor, true)
422
+
423
+ if (result.errors !== undefined) {
424
+ return {
425
+ continua: false,
426
+ error: {
427
+ errorID: 'PLMVALUSML001',
428
+ msg: `Exitem problemas dentro do objeto ${nome}`,
429
+ error: result.errors
430
+ }
431
+ }
432
+ }
433
+
434
+ return {
435
+ continua: true
436
+ }
437
+ }
438
+
439
+ if (model.filter !== undefined) {
440
+ f.filter = model.filter.bind(model)
441
+ }
442
+
443
+ return f
444
444
  }