pwi-plata-type 0.1.1 → 0.1.4

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.
@@ -1,4 +1,5 @@
1
1
  import { tools } from "./plata-create-tools.js";
2
+ import { PlataDirs } from "../../libs/tools.js"
2
3
  import * as path from 'node:path'
3
4
 
4
5
  export async function h() {
@@ -50,6 +51,17 @@ export async function h() {
50
51
  )))
51
52
 
52
53
  break;
54
+ case 'model':
55
+ const modelName = args.shift()
56
+ const plataName = PlataDirs.ProjectJson.plata_name
57
+
58
+ promises.push(tools.files.deployFileToProject(
59
+ 'models',
60
+ 'model',
61
+ `${modelName.toLowerCase()}.ts`,
62
+ { Name: `${modelName}`, PlataName: `${plataName}` }
63
+ ).finally(() => console.log(`Model ${modelName} criada`)))
64
+ break;
53
65
  }
54
66
 
55
67
  await Promise.all(promises)
package/libs/model.ts CHANGED
@@ -6,7 +6,7 @@ export interface ErroValidacao {
6
6
 
7
7
  export interface RetornoValidacaoPrimitivo {
8
8
  error?: ErroValidacao
9
- cotinua: boolean
9
+ continua: boolean
10
10
  }
11
11
 
12
12
  export interface RetornoValicadaoModel {
@@ -44,7 +44,7 @@ export async function _validaCampo(value: any, pipeline: ModelValicacaoPrimitivo
44
44
  return result.error
45
45
  }
46
46
 
47
- if (!result.cotinua) {
47
+ if (!result.continua) {
48
48
  break
49
49
  }
50
50
  }
@@ -196,7 +196,7 @@ export function DateTime(): ModelValicacaoPrimitivo {
196
196
  errorID: "BPMVDT001",
197
197
  msg: `A data no campo ${nome} tem que está no formato yyyy-mm-ddTHH:MM:SS`,
198
198
  },
199
- cotinua: false
199
+ continua: false
200
200
  }
201
201
  }
202
202
 
@@ -208,7 +208,7 @@ export function DateTime(): ModelValicacaoPrimitivo {
208
208
  errorID: "BPMVDT002",
209
209
  msg: `A data no campo ${nome} tem que está no formato yyyy-mm-ddTHH:MM:SS`
210
210
  },
211
- cotinua: false
211
+ continua: false
212
212
  }
213
213
  }
214
214
 
@@ -218,11 +218,11 @@ export function DateTime(): ModelValicacaoPrimitivo {
218
218
  errorID: "BCVDT003",
219
219
  msg: `A data no campo ${nome} tem que está no formato yyyy-mm-ddTHH:MM:SS`,
220
220
  },
221
- cotinua: false
221
+ continua: false
222
222
  }
223
223
  }
224
224
 
225
- return { cotinua: true }
225
+ return { continua: true }
226
226
  }
227
227
  }
228
228
 
@@ -234,7 +234,7 @@ export function Decimal(tamanhoInteiro: number, casasDecimais: number): ModelVal
234
234
  errorID: "BPMVDEC001",
235
235
  msg: `O campo ${nome} tem que ser um decimal`
236
236
  },
237
- cotinua: false,
237
+ continua: false,
238
238
  }
239
239
  }
240
240
 
@@ -246,7 +246,7 @@ export function Decimal(tamanhoInteiro: number, casasDecimais: number): ModelVal
246
246
  errorID: "BPMVDEC002",
247
247
  msg: `O campo ${nome} não pode ser maior que ${(tamanhoInteiro + casasDecimais + 1)}`
248
248
  },
249
- cotinua: false,
249
+ continua: false,
250
250
  }
251
251
  }
252
252
 
@@ -258,7 +258,7 @@ export function Decimal(tamanhoInteiro: number, casasDecimais: number): ModelVal
258
258
  errorID: "BPMVDEC003",
259
259
  msg: `A parte inteira do ${nome} não pode ter mais de ${tamanhoInteiro} digitos`
260
260
  },
261
- cotinua: false,
261
+ continua: false,
262
262
  }
263
263
  }
264
264
 
@@ -269,12 +269,12 @@ export function Decimal(tamanhoInteiro: number, casasDecimais: number): ModelVal
269
269
  errorID: "BPMVDEC004",
270
270
  msg: `A parte decimal do ${nome} não pode ter mais de ${casasDecimais} digitos`
271
271
  },
272
- cotinua: false,
272
+ continua: false,
273
273
  }
274
274
  }
275
275
  }
276
276
 
277
- return { cotinua: true }
277
+ return { continua: true }
278
278
  }
279
279
  }
280
280
 
@@ -286,7 +286,7 @@ export function Int(): ModelValicacaoPrimitivo {
286
286
  errorID: "BPMVINT001",
287
287
  msg: `O campo ${nome} tem que ser um número inteiro`
288
288
  },
289
- cotinua: false,
289
+ continua: false,
290
290
  }
291
291
  }
292
292
 
@@ -296,10 +296,179 @@ export function Int(): ModelValicacaoPrimitivo {
296
296
  errorID: "BPMVINT002",
297
297
  msg: `O campo ${nome} tem que ser um número inteiro`
298
298
  },
299
- cotinua: false,
299
+ continua: false,
300
300
  }
301
301
  }
302
302
 
303
- return { cotinua: true }
303
+ return { continua: true }
304
+ }
305
+ }
306
+
307
+ export function isArray(tamanhoMaximo?: number): ModelValicacaoPrimitivo {
308
+ return async (nome: string, valor: any) => {
309
+ if (valor === null) {
310
+ return {
311
+ error: {
312
+ errorID: "BPCVARR001",
313
+ msg: `O campo ${nome} tem ser um array`
314
+ },
315
+ continua: false
316
+ }
317
+ }
318
+
319
+ if (valor.length === undefined) {
320
+ return {
321
+ error: {
322
+ errorID: "BPCVARR002",
323
+ msg: `O campo ${nome} tem ser um array`
324
+ },
325
+ continua: false
326
+ }
327
+ }
328
+
329
+ if (tamanhoMaximo !== undefined && tamanhoMaximo !== Infinity) {
330
+ if (valor.length > tamanhoMaximo) {
331
+ return {
332
+ error: {
333
+ errorID: "BPCVARR003",
334
+ msg: `O campo ${nome} não pode ter mais de ${tamanhoMaximo} itens`
335
+ },
336
+ continua: false
337
+ }
338
+ }
339
+ }
340
+
341
+ return { continua: true }
342
+ }
343
+ }
344
+
345
+ export function Optional(): ModelValicacaoPrimitivo {
346
+ return async (nome: string, valor: any) => {
347
+ if (!valor) {
348
+ return { continua: false }
349
+ }
350
+
351
+ return { continua: true }
352
+ }
353
+ }
354
+
355
+ export function Required(): ModelValicacaoPrimitivo {
356
+ return async (nome: string, valor: any) => {
357
+ if (valor === null) {
358
+ return {
359
+ error: {
360
+ errorID: "BPCVREQ001",
361
+ msg: `O campo ${nome} é obrigatório`
362
+ },
363
+ continua: false
364
+ }
365
+ }
366
+
367
+ return { continua: true }
368
+ }
369
+ }
370
+
371
+ export function SmallDateTime(): ModelValicacaoPrimitivo {
372
+ return async (nome: string, valor: any) => {
373
+ const regex = /(\d{4})-([01]\d)-([0-3]\d)/
374
+
375
+ if (!regex.test(valor)) {
376
+ return {
377
+ error: {
378
+ errorID: "BPCVDT001",
379
+ msg: `A data no campo ${nome} tem que está no formato yyyy-mm-dd`
380
+ },
381
+ continua: false
382
+ }
383
+ }
384
+
385
+ const [ _, ano, mes, dia ] = valor.match(regex)
386
+
387
+ if (isNaN(+ano) || isNaN(+mes) || isNaN(+dia)){
388
+ return {
389
+ error: {
390
+ errorID: "BPCVDT002",
391
+ msg: `A data no campo ${nome} tem que está no formato yyyy-mm-dd`
392
+ },
393
+ continua: false
394
+ }
395
+ }
396
+
397
+ if ((+dia) > 31 || (+mes) > 12 ){
398
+ return {
399
+ error: {
400
+ errorID: "BPCVDT003",
401
+ msg: `A data no campo ${nome} tem que está no formato yyyy-mm-dd`
402
+ },
403
+ continua: false
404
+ }
405
+ }
406
+
407
+ //https://sqltutorialtips.blogspot.com/2016/11/smalldatetime-vs-datetime.html
408
+ if (new Date(+ano, mes - 1, +dia) >= new Date(2079, 11, 31)) {
409
+ return {
410
+ error: {
411
+ errorID: "BPCVDT004",
412
+ msg: `A data no campo ${nome} não pode ser maior que 2079-12-31`
413
+ },
414
+ continua: false
415
+ }
416
+ }
417
+
418
+ return { continua: true }
419
+ }
420
+ }
421
+
422
+ export function Telefone(): ModelValicacaoPrimitivo {
423
+ return async (nome: string, valor: any) => {
424
+ const v = `${valor}`.split('-')
425
+
426
+ if (v.length !== 2) {
427
+ return {
428
+ error: {
429
+ errorID: "BPVTEL001",
430
+ msg: `O campo ${nome} tem que ser um telefone em no formato 99-999999999`
431
+ },
432
+ continua: false
433
+ }
434
+ }
435
+
436
+ if (v[0].length !== 2 || v[1].length > 9) {
437
+ return {
438
+ error: {
439
+ errorID: "BPVTEL002",
440
+ msg: `O campo ${nome} tem que ser um telefone em no formato 99-999999999`
441
+ },
442
+ continua: false
443
+ }
444
+ }
445
+
446
+ if (!+v[0] || !+v[1]) {
447
+ return {
448
+ error: {
449
+ errorID: "BPVTEL003",
450
+ msg: `O campo ${nome} tem que ser um telefone em no formato 99-999999999`
451
+ },
452
+ continua: false
453
+ }
454
+ }
455
+
456
+ return { continua: true }
457
+ }
458
+ }
459
+
460
+ export function VarChar(size: number): ModelValicacaoPrimitivo {
461
+ return async (nome: string, valor: any) => {
462
+ if (valor.length > size) {
463
+ return {
464
+ error: {
465
+ errorID: "BPVVARC001",
466
+ msg: `O campo ${nome} tem que menos de ${nome} caracteres`
467
+ },
468
+ continua: false
469
+ }
470
+ }
471
+
472
+ return { continua: true }
304
473
  }
305
474
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pwi-plata-type",
3
- "version": "0.1.1",
3
+ "version": "0.1.4",
4
4
  "type": "module",
5
5
  "main": "index.ts",
6
6
  "bin": {
@@ -0,0 +1,26 @@
1
+ //@ts-nocheck
2
+ import { PlataModels } from "ç__PlataName__ç";
3
+
4
+ const ç__Name__ç = new PlataModels.Model({
5
+ campo: [ PlataModels.Required(), PlataModels.Int() ],
6
+ objeto: {
7
+ campo: [ PlataModels.Optional(), PlataModels.DateTime() ]
8
+ },
9
+ arrayItens: [{
10
+ campo: [ PlataModels.Required(), PlataModels.Telefone() ]
11
+ }]
12
+ })
13
+
14
+ ç__Name__ç.addValidacao('Validação Customizada', async (objeto) => {
15
+ if (objeto.campo > 30) {
16
+ return {
17
+ errorID: 'BM0001',
18
+ msg: 'O campo não pode ser maior que 30',
19
+ error: `valor do campo: ${objeto.campo}`
20
+ }
21
+ }
22
+
23
+ return null
24
+ })
25
+
26
+ export const ç__Name__çModel = ç__Name__ç
@@ -7,6 +7,7 @@ export async function install({ dirs, projectPackageJson }) {
7
7
  promises.push(t.createFolderProject('clusters'))
8
8
  promises.push(t.createFolderProject('routes'))
9
9
  promises.push(t.createFolderProject('libs'))
10
+ promises.push(t.createFolderProject('models'))
10
11
 
11
12
  promises.push(t.copyFolderToProject('configs'))
12
13
  promises.push(t.copyFolderToProject('envs'))