pwi-plata-type 0.4.142 → 0.4.144

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,8 +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;
10
+ this.validations = new Set();
11
+ }
12
+ addValidation(callback) {
13
+ this.validations.add(callback);
9
14
  }
10
15
  async validate(obj) {
11
16
  let v = obj;
@@ -15,20 +20,34 @@ class ModelTemplateCombine {
15
20
  v = model.filter(v);
16
21
  }
17
22
  }
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)));
23
+ {
24
+ const promises = [];
25
+ for (let i = 0; i < this.models.length; i++) {
26
+ const model = this.models[i];
27
+ promises.push(Plata.FastPromise(() => model.validate(v, true)));
28
+ }
29
+ const results = await Promise.all(promises);
30
+ const errors = results
31
+ .filter((r) => r !== undefined)
32
+ .flatMap(r => r.errors);
33
+ if (errors.length !== 0) {
34
+ return {
35
+ value: undefined,
36
+ errors
37
+ };
38
+ }
22
39
  }
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
- };
40
+ {
41
+ const promises = [];
42
+ this.validations.forEach(va => promises.push(Plata.FastPromise(() => va(v))));
43
+ const result = await Promise.all(promises);
44
+ const errors = result.filter(Boolean);
45
+ if (errors.length !== 0) {
46
+ return {
47
+ errors,
48
+ value: undefined
49
+ };
50
+ }
32
51
  }
33
52
  return {
34
53
  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,IAAI,CAAyB;IAC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,CAAC,QAAQ,CAAC,CAAA;YAE3C,MAAM,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;AA5ED,oDA4EC"}
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.144",
4
4
  "main": "__BUILD__/index",
5
5
  "types": "src/@types/exported.d.ts",
6
6
  "bin": {
@@ -1,9 +1,15 @@
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
8
+ this.validations = new Set()
9
+ }
10
+
11
+ public addValidation(callback: (value: typeof this.type) => Promise<Model.ValidateReturn>) {
12
+ this.validations.add(callback)
7
13
  }
8
14
 
9
15
  public async validate(obj: any): Promise<Model.ModelValidateReturnHelper<typeof this.type, undefined>> {
@@ -16,25 +22,50 @@ export class ModelTemplateCombine<const T extends readonly Model.ModelClass[]> {
16
22
  }
17
23
  }
18
24
 
19
- const promises: Promise<{ errors?: PlataError[] }>[] = []
25
+ { // Valida as models
26
+ const promises: Promise<{ errors?: PlataError[] }>[] = []
20
27
 
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)))
28
+ for (let i = 0; i < this.models.length; i++) {
29
+ const model = this.models[i];
30
+
31
+ promises.push(Plata.FastPromise(() => model.validate(v, true)))
32
+ }
33
+
34
+ const results = await Promise.all(promises)
35
+
36
+ const errors = results
37
+ .filter((r): r is { errors: PlataError[] } => r !== undefined)
38
+ .flatMap(r => r.errors)
39
+ ;
40
+
41
+ if (errors.length !== 0) {
42
+ return {
43
+ value: undefined,
44
+ errors
45
+ }
46
+ }
25
47
  }
26
48
 
27
- const results = await Promise.all(promises)
49
+
50
+
51
+ { // Validacoes adicionais
52
+ const promises: Promise<Model.ValidateReturn>[] = []
53
+
54
+ this.validations.forEach(va =>
55
+ promises.push(
56
+ Plata.FastPromise(() => va(v))
57
+ )
58
+ )
59
+
60
+ const result = await Promise.all(promises)
28
61
 
29
- const errors = results
30
- .filter((r): r is { errors: PlataError[] } => r !== undefined)
31
- .flatMap(r => r.errors)
32
- ;
62
+ const errors = result.filter(Boolean)
33
63
 
34
- if (errors.length !== 0) {
35
- return {
36
- value: undefined,
37
- errors
64
+ if (errors.length !== 0) {
65
+ return {
66
+ errors,
67
+ value: undefined
68
+ }
38
69
  }
39
70
  }
40
71