pwi-plata-type 0.4.142 → 0.4.143

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.
@@ -4,9 +4,13 @@ exports.ModelTemplateCombine = void 0;
4
4
  class ModelTemplateCombine {
5
5
  type;
6
6
  models;
7
+ validations;
7
8
  constructor(models) {
8
9
  this.models = models;
9
10
  }
11
+ addValidation(callback) {
12
+ this.validations.add(callback);
13
+ }
10
14
  async validate(obj) {
11
15
  let v = obj;
12
16
  for (let i = 0; i < this.models.length; i++) {
@@ -15,20 +19,34 @@ class ModelTemplateCombine {
15
19
  v = model.filter(v);
16
20
  }
17
21
  }
18
- const promises = [];
19
- for (let i = 0; i < this.models.length; i++) {
20
- const model = this.models[i];
21
- promises.push(Plata.FastPromise(() => model.validate(v, true)));
22
+ {
23
+ const promises = [];
24
+ for (let i = 0; i < this.models.length; i++) {
25
+ const model = this.models[i];
26
+ promises.push(Plata.FastPromise(() => model.validate(v, true)));
27
+ }
28
+ const results = await Promise.all(promises);
29
+ const errors = results
30
+ .filter((r) => r !== undefined)
31
+ .flatMap(r => r.errors);
32
+ if (errors.length !== 0) {
33
+ return {
34
+ value: undefined,
35
+ errors
36
+ };
37
+ }
22
38
  }
23
- const results = await Promise.all(promises);
24
- const errors = results
25
- .filter((r) => r !== undefined)
26
- .flatMap(r => r.errors);
27
- if (errors.length !== 0) {
28
- return {
29
- value: undefined,
30
- errors
31
- };
39
+ {
40
+ const promises = [];
41
+ this.validations.forEach(va => promises.push(Plata.FastPromise(() => va(v))));
42
+ const result = await Promise.all(promises);
43
+ const errors = result.filter(Boolean);
44
+ if (errors.length !== 0) {
45
+ return {
46
+ errors,
47
+ value: undefined
48
+ };
49
+ }
32
50
  }
33
51
  return {
34
52
  value: v,
@@ -1 +1 @@
1
- {"version":3,"file":"model-template-combine.js","sourceRoot":"","sources":["../../../src/libs/models/model-template-combine.ts"],"names":[],"mappings":";;;AAAA,MAAa,oBAAoB;IACpB,IA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MAAM,GAAG,OAAO;iBACjB,MAAM,CAAC,CAAC,CAAC,EAAiC,EAAE,CAAC,CAAC,KAAK,SAAS,CAAC;iBAC7D,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAC1B;YAED,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE;gBACrB,OAAO;oBACH,KAAK,EAAE,SAAS;oBAChB,MAAM;iBACT,CAAA;aACJ;SACJ;QAID;YACI,MAAM,QAAQ,GAAoC,EAAE,CAAA;YAEpD,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE,CAC1B,QAAQ,CAAC,IAAI,CACT,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CACjC,CACJ,CAAA;YAED,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;YAE1C,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;YAErC,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE;gBACrB,OAAQ;oBACJ,MAAM;oBACN,KAAK,EAAE,SAAS;iBACnB,CAAA;aACJ;SACJ;QAED,OAAO;YACH,KAAK,EAAE,CAAC;YACR,MAAM,EAAE,SAAS;SACpB,CAAA;IACL,CAAC;CACJ;AA3ED,oDA2EC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pwi-plata-type",
3
- "version": "0.4.142",
3
+ "version": "0.4.143",
4
4
  "main": "__BUILD__/index",
5
5
  "types": "src/@types/exported.d.ts",
6
6
  "bin": {
@@ -1,11 +1,16 @@
1
1
  export class ModelTemplateCombine<const T extends readonly Model.ModelClass[]> {
2
2
  readonly type: ArrayCombine<T>['type']
3
3
  readonly models: T
4
+ public readonly validations: Set<(value: typeof this.type) => Promise<Model.ValidateReturn>>
4
5
 
5
6
  constructor(models: T) {
6
7
  this.models = models
7
8
  }
8
9
 
10
+ public addValidation(callback: (value: typeof this.type) => Promise<Model.ValidateReturn>) {
11
+ this.validations.add(callback)
12
+ }
13
+
9
14
  public async validate(obj: any): Promise<Model.ModelValidateReturnHelper<typeof this.type, undefined>> {
10
15
  let v = obj
11
16
  for (let i = 0; i < this.models.length; i++) {
@@ -16,25 +21,50 @@ export class ModelTemplateCombine<const T extends readonly Model.ModelClass[]> {
16
21
  }
17
22
  }
18
23
 
19
- const promises: Promise<{ errors?: PlataError[] }>[] = []
24
+ { // Valida as models
25
+ const promises: Promise<{ errors?: PlataError[] }>[] = []
20
26
 
21
- for (let i = 0; i < this.models.length; i++) {
22
- const model = this.models[i];
23
-
24
- promises.push(Plata.FastPromise(() => model.validate(v, true)))
27
+ for (let i = 0; i < this.models.length; i++) {
28
+ const model = this.models[i];
29
+
30
+ promises.push(Plata.FastPromise(() => model.validate(v, true)))
31
+ }
32
+
33
+ const results = await Promise.all(promises)
34
+
35
+ const errors = results
36
+ .filter((r): r is { errors: PlataError[] } => r !== undefined)
37
+ .flatMap(r => r.errors)
38
+ ;
39
+
40
+ if (errors.length !== 0) {
41
+ return {
42
+ value: undefined,
43
+ errors
44
+ }
45
+ }
25
46
  }
26
47
 
27
- const results = await Promise.all(promises)
48
+
49
+
50
+ { // Validacoes adicionais
51
+ const promises: Promise<Model.ValidateReturn>[] = []
52
+
53
+ this.validations.forEach(va =>
54
+ promises.push(
55
+ Plata.FastPromise(() => va(v))
56
+ )
57
+ )
58
+
59
+ const result = await Promise.all(promises)
28
60
 
29
- const errors = results
30
- .filter((r): r is { errors: PlataError[] } => r !== undefined)
31
- .flatMap(r => r.errors)
32
- ;
61
+ const errors = result.filter(Boolean)
33
62
 
34
- if (errors.length !== 0) {
35
- return {
36
- value: undefined,
37
- errors
63
+ if (errors.length !== 0) {
64
+ return {
65
+ errors,
66
+ value: undefined
67
+ }
38
68
  }
39
69
  }
40
70