pols-validator 2.3.4 → 2.3.6

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 +1 @@
1
- {"version":3,"file":"rules.d.ts","sourceRoot":"","sources":["../src/rules.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAiB,MAAM,eAAe,CAAA;AA4C3D,qBAAa,MAAO,SAAQ,YAAY;IACvC,cAAc;IAWd,cAAc;IAQd,UAAU;IAcV,MAAM;IAMN,MAAM;IA4BN,KAAK,CAAC,OAAO,EAAE,MAAM;IAQrB,QAAQ;IAUR,SAAS;IAQT,SAAS;IAIT,eAAe;IAIf,WAAW;IAQX,SAAS,CAAC,KAAK,EAAE,MAAM;IAYvB,SAAS,CAAC,KAAK,EAAE,MAAM;IAYvB,cAAc,CAAC,KAAK,EAAE,MAAM;IAY5B,IAAI,CAAC,KAAK,EAAE,MAAM;IAMlB,OAAO,CAAC,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM;IA+BlD,IAAI,CAAC,GAAG,QAAQ,EAAE,OAAO,EAAE;IAY3B,OAAO,CAAC,GAAG,QAAQ,EAAE,OAAO,EAAE;IAM9B,WAAW;IAMX,EAAE,CAAC,KAAK,EAAE,MAAM;IAMhB,GAAG,CAAC,KAAK,EAAE,MAAM;IAMjB,EAAE,CAAC,KAAK,EAAE,MAAM;IAMhB,GAAG,CAAC,KAAK,EAAE,MAAM;IAMjB,iBAAiB;IAOjB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAMxC,SAAS;IA+CT,KAAK;IAQL,KAAK;IAQL,SAAS;IAQT,KAAK,CAAC,QAAQ,EAAE,MAAM;IAStB,iBAAiB;IAQjB,QAAQ;IASR,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,CAAC,CAAC,SAAS,EAAE,MAAM,EA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AAQ;IASR,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,CAAC,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,MAAM,CAAC;IAMlG,UAAU;IAQV,KAAK,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM;IAMhC,IAAI,CAAC,SAAS,EAAE,MAAM;IAMtB,KAAK;IAML,IAAI;IAMJ,QAAQ,CAAC,GAAG,WAAW,EAAE,MAAM,EAAE;CAKjC"}
package/dist/rules.js CHANGED
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PRules = void 0;
4
- const pols_utils_1 = require("pols-utils");
5
4
  const rulesEngine_1 = require("./rulesEngine");
6
5
  const pols_date_1 = require("pols-date");
7
6
  const isomorphic_dompurify_1 = require("isomorphic-dompurify");
7
+ const pols_utils_1 = require("pols-utils");
8
8
  const isObject = (context, wrapper, schema) => {
9
9
  const message = `'${wrapper.label}' debe ser un objeto`;
10
10
  if (typeof wrapper.value == 'string') {
@@ -14,13 +14,15 @@ const isObject = (context, wrapper, schema) => {
14
14
  catch {
15
15
  return message;
16
16
  }
17
- if (pols_utils_1.PUtils.getType(wrapper.value) != 'Object')
17
+ if (wrapper.value == null || typeof wrapper.value != 'object')
18
18
  return message;
19
19
  }
20
- else {
21
- if (pols_utils_1.PUtils.getType(wrapper.value) != 'Object') {
20
+ else if (wrapper.value != null) {
21
+ if (typeof wrapper.value != 'object')
22
22
  return message;
23
- }
23
+ }
24
+ else {
25
+ return message;
24
26
  }
25
27
  /* Realiza la validación de cada propiedad */
26
28
  const newWrapperValue = {};
@@ -30,7 +32,7 @@ const isObject = (context, wrapper, schema) => {
30
32
  const labelIndise = rulesInside.label ?? key;
31
33
  rulesInside.label = `${context.label ? `${context.label}${context.separator}` : ''}${labelIndise}`;
32
34
  newWrapperValue[key] = wrapper.value[key];
33
- const result2 = rulesInside.validate(newWrapperValue[key]);
35
+ const result2 = rulesInside.validate(newWrapperValue[key], false);
34
36
  if (result2.error == true) {
35
37
  errorMessages.push(...result2.messages);
36
38
  }
@@ -68,15 +70,10 @@ class PRules extends rulesEngine_1.PRulesEngine {
68
70
  isDateTime() {
69
71
  this.add(this.isDateTime.name, (wrapper) => {
70
72
  const message = `'${wrapper.label}' tiene un formato de fecha y hora no válido`;
71
- if (typeof wrapper.value == 'string' || typeof wrapper.value == 'number' || wrapper.value instanceof Date || wrapper.value instanceof pols_date_1.PDate) {
72
- const newDate = new pols_date_1.PDate(wrapper.value);
73
- if (newDate.isInvalidDate)
74
- return message;
75
- wrapper.value = newDate;
76
- }
77
- else {
73
+ const newDate = new pols_date_1.PDate(wrapper.value);
74
+ if (newDate.isInvalidDate)
78
75
  return message;
79
- }
76
+ wrapper.value = newDate;
80
77
  });
81
78
  return this;
82
79
  }
@@ -102,12 +99,12 @@ class PRules extends rulesEngine_1.PRulesEngine {
102
99
  if (hours >= 1 && hours <= 12) {
103
100
  if (middle === 'p')
104
101
  hours += 12;
105
- wrapper.value = `${pols_utils_1.PUtils.String.padLeft(hours, 2)}:${pols_utils_1.PUtils.String.padLeft(minutes, 2)}:${pols_utils_1.PUtils.String.padLeft(seconds, 2)}`;
102
+ wrapper.value = `${pols_utils_1.PUtilsString.padLeft(hours, 2)}:${pols_utils_1.PUtilsString.padLeft(minutes, 2)}:${pols_utils_1.PUtilsString.padLeft(seconds, 2)}`;
106
103
  return;
107
104
  }
108
105
  }
109
106
  else if (hours >= 0 && hours <= 23) {
110
- wrapper.value = `${pols_utils_1.PUtils.String.padLeft(hours, 2)}:${pols_utils_1.PUtils.String.padLeft(minutes, 2)}:${pols_utils_1.PUtils.String.padLeft(seconds, 2)}`;
107
+ wrapper.value = `${pols_utils_1.PUtilsString.padLeft(hours, 2)}:${pols_utils_1.PUtilsString.padLeft(minutes, 2)}:${pols_utils_1.PUtilsString.padLeft(seconds, 2)}`;
111
108
  return;
112
109
  }
113
110
  }
@@ -222,7 +219,7 @@ class PRules extends rulesEngine_1.PRulesEngine {
222
219
  for (const [i, element] of wrapper.value.entries()) {
223
220
  const rules = rulesGenerator(i);
224
221
  rules.label = `${this.label ? `${this.label}${rules.label ? this.separator : ''}` : ''}${rules.label ?? ''}`;
225
- const result = rules.validate(element);
222
+ const result = rules.validate(element, false);
226
223
  if (result.error == true) {
227
224
  messages.push(...result.messages);
228
225
  }
@@ -370,7 +367,7 @@ class PRules extends rulesEngine_1.PRulesEngine {
370
367
  round(decimals) {
371
368
  this.isNumber();
372
369
  this.add(this.round.name, (wrapper) => {
373
- wrapper.value = pols_utils_1.PUtils.Number.round(wrapper.value, decimals);
370
+ wrapper.value = pols_utils_1.PUtilsNumber.round(wrapper.value, decimals);
374
371
  });
375
372
  return this;
376
373
  }
@@ -398,7 +395,7 @@ class PRules extends rulesEngine_1.PRulesEngine {
398
395
  capitalize() {
399
396
  this.isAlphanumeric();
400
397
  this.add(this.capitalize.name, (wrapper) => {
401
- wrapper.value = pols_utils_1.PUtils.String.capitalize(wrapper.value);
398
+ wrapper.value = pols_utils_1.PUtilsString.capitalize(wrapper.value);
402
399
  });
403
400
  return this;
404
401
  }
@@ -30,6 +30,6 @@ export declare class PRulesEngine {
30
30
  private collectionNames;
31
31
  constructor(params?: PRulesParams);
32
32
  protected add(name: string, validationFunction: PRulesFunction): this;
33
- validate<T>(target: unknown): PRulesResponse<T>;
33
+ validate<T>(target: unknown, safe?: boolean): PRulesResponse<T>;
34
34
  }
35
35
  //# sourceMappingURL=rulesEngine.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"rulesEngine.d.ts","sourceRoot":"","sources":["../src/rulesEngine.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,YAAY,GAAG;IAC1B,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,SAAS,CAAC,EAAE,MAAM,CAAA;CAClB,GAAG,CAAC;IACJ,QAAQ,CAAC,EAAE,OAAO,CAAA;CA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,QAAQ,CAAC,EAAE,OAAO,CAAA;CAClB,GAAG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;CACjB,CAAC,CAAA;AAEF,MAAM,MAAM,cAAc,GAAG,CAAC,OAAO,EAAE,aAAa,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI,GAAG,IAAI,GAAG,SAAS,CAAA;AAExH,MAAM,MAAM,aAAa,CAAC,CAAC,GAAG,OAAO,IAAI;IACxC,KAAK,EAAE,CAAC,CAAA;IACR,KAAK,EAAE,MAAM,CAAA;CACb,CAAA;AAED,MAAM,MAAM,cAAc,CAAC,CAAC,IAAI;IAC/B,KAAK,EAAE,KAAK,CAAA;IACZ,OAAO,EAAE,IAAI,CAAA;IACb,MAAM,EAAE,CAAC,CAAA;CACT,GAAG;IACH,KAAK,EAAE,IAAI,CAAA;IACX,OAAO,EAAE,KAAK,CAAA;IACd,QAAQ,EAAE,MAAM,EAAE,CAAA;CAClB,CAAA;AAED,qBAAa,YAAY;IACxB,MAAM,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,MAAM,CAAA;IACb,SAAS,EAAE,MAAM,CAAA;IACjB,QAAQ,EAAE,OAAO,CAAQ;IACzB,OAAO,EAAE,OAAO,CAAO;IAEvB,OAAO,CAAC,mBAAmB,CAAuB;IAClD,OAAO,CAAC,eAAe,CAAe;gBAE1B,MAAM,CAAC,EAAE,YAAY;IAOjC,SAAS,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,kBAAkB,EAAE,cAAc;IAQ9D,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,UAAO,GAAG,cAAc,CAAC,CAAC,CAAC;CAqD5D"}
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PRulesEngine = void 0;
4
- const pols_utils_1 = require("pols-utils");
5
4
  class PRulesEngine {
6
5
  prefix;
7
6
  label;
@@ -23,7 +22,7 @@ class PRulesEngine {
23
22
  }
24
23
  return this;
25
24
  }
26
- validate(target) {
25
+ validate(target, safe = true) {
27
26
  const errorMessages = [];
28
27
  if (typeof target == 'string')
29
28
  target = target.trim();
@@ -44,7 +43,7 @@ class PRulesEngine {
44
43
  };
45
44
  }
46
45
  const wrapper = {
47
- value: pols_utils_1.PUtils.clone(target),
46
+ value: safe ? JSON.parse(JSON.stringify(target)) : target,
48
47
  label
49
48
  };
50
49
  for (const validationFunction of this.collectionFunctions) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pols-validator",
3
- "version": "2.3.4",
3
+ "version": "2.3.6",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -30,6 +30,6 @@
30
30
  "dependencies": {
31
31
  "isomorphic-dompurify": "^2.21.0",
32
32
  "pols-date": "^1.2.2",
33
- "pols-utils": "^4.1.2"
33
+ "pols-utils": "^5.0.1"
34
34
  }
35
35
  }
package/src/rules.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { PUtils } from "pols-utils"
2
1
  import { PRulesEngine, PRulesWrapper } from "./rulesEngine"
3
2
  import { PDate } from "pols-date"
4
3
  import { sanitize } from 'isomorphic-dompurify'
4
+ import { PUtilsNumber, PUtilsString } from "pols-utils"
5
5
 
6
6
  const isObject = (context: PRules, wrapper: PRulesWrapper, schema?: Record<string, PRules>) => {
7
7
  const message = `'${wrapper.label}' debe ser un objeto`
@@ -12,11 +12,11 @@ const isObject = (context: PRules, wrapper: PRulesWrapper, schema?: Record<strin
12
12
  } catch {
13
13
  return message
14
14
  }
15
- if (PUtils.getType(wrapper.value) != 'Object') return message
15
+ if (wrapper.value == null || typeof wrapper.value != 'object') return message
16
+ } else if (wrapper.value != null) {
17
+ if (typeof wrapper.value != 'object') return message
16
18
  } else {
17
- if (PUtils.getType(wrapper.value) != 'Object') {
18
- return message
19
- }
19
+ return message
20
20
  }
21
21
 
22
22
  /* Realiza la validación de cada propiedad */
@@ -29,7 +29,7 @@ const isObject = (context: PRules, wrapper: PRulesWrapper, schema?: Record<strin
29
29
 
30
30
  newWrapperValue[key] = wrapper.value[key]
31
31
 
32
- const result2 = rulesInside.validate(newWrapperValue[key])
32
+ const result2 = rulesInside.validate(newWrapperValue[key], false)
33
33
  if (result2.error == true) {
34
34
  errorMessages.push(...result2.messages)
35
35
  } else {
@@ -66,13 +66,9 @@ export class PRules extends PRulesEngine {
66
66
  isDateTime() {
67
67
  this.add(this.isDateTime.name, (wrapper: PRulesWrapper) => {
68
68
  const message = `'${wrapper.label}' tiene un formato de fecha y hora no válido`
69
- if (typeof wrapper.value == 'string' || typeof wrapper.value == 'number' || wrapper.value instanceof Date || wrapper.value instanceof PDate) {
70
- const newDate = new PDate(wrapper.value)
71
- if (newDate.isInvalidDate) return message
72
- wrapper.value = newDate
73
- } else {
74
- return message
75
- }
69
+ const newDate = new PDate(wrapper.value)
70
+ if (newDate.isInvalidDate) return message
71
+ wrapper.value = newDate
76
72
  })
77
73
  return this
78
74
  }
@@ -98,11 +94,11 @@ export class PRules extends PRulesEngine {
98
94
  if (middle) {
99
95
  if (hours >= 1 && hours <= 12) {
100
96
  if (middle === 'p') hours += 12
101
- wrapper.value = `${PUtils.String.padLeft(hours, 2)}:${PUtils.String.padLeft(minutes, 2)}:${PUtils.String.padLeft(seconds, 2)}`
97
+ wrapper.value = `${PUtilsString.padLeft(hours, 2)}:${PUtilsString.padLeft(minutes, 2)}:${PUtilsString.padLeft(seconds, 2)}`
102
98
  return
103
99
  }
104
100
  } else if (hours >= 0 && hours <= 23) {
105
- wrapper.value = `${PUtils.String.padLeft(hours, 2)}:${PUtils.String.padLeft(minutes, 2)}:${PUtils.String.padLeft(seconds, 2)}`
101
+ wrapper.value = `${PUtilsString.padLeft(hours, 2)}:${PUtilsString.padLeft(minutes, 2)}:${PUtilsString.padLeft(seconds, 2)}`
106
102
  return
107
103
  }
108
104
  }
@@ -215,7 +211,7 @@ export class PRules extends PRulesEngine {
215
211
  for (const [i, element] of wrapper.value.entries()) {
216
212
  const rules = rulesGenerator(i)
217
213
  rules.label = `${this.label ? `${this.label}${rules.label ? this.separator : ''}` : ''}${rules.label ?? ''}`
218
- const result = rules.validate(element)
214
+ const result = rules.validate(element, false)
219
215
  if (result.error == true) {
220
216
  messages.push(...result.messages)
221
217
  } else {
@@ -361,7 +357,7 @@ export class PRules extends PRulesEngine {
361
357
  round(decimals: number) {
362
358
  this.isNumber()
363
359
  this.add(this.round.name, (wrapper: PRulesWrapper) => {
364
- wrapper.value = PUtils.Number.round(wrapper.value as number, decimals)
360
+ wrapper.value = PUtilsNumber.round(wrapper.value as number, decimals)
365
361
  })
366
362
  return this
367
363
  }
@@ -393,7 +389,7 @@ export class PRules extends PRulesEngine {
393
389
  capitalize() {
394
390
  this.isAlphanumeric()
395
391
  this.add(this.capitalize.name, (wrapper: PRulesWrapper) => {
396
- wrapper.value = PUtils.String.capitalize(wrapper.value as string)
392
+ wrapper.value = PUtilsString.capitalize(wrapper.value as string)
397
393
  })
398
394
  return this
399
395
  }
@@ -1,5 +1,3 @@
1
- import { PUtils } from "pols-utils"
2
-
3
1
  export type PRulesParams = {
4
2
  label?: string
5
3
  separator?: string
@@ -51,7 +49,7 @@ export class PRulesEngine {
51
49
  return this
52
50
  }
53
51
 
54
- validate<T>(target: unknown): PRulesResponse<T> {
52
+ validate<T>(target: unknown, safe = true): PRulesResponse<T> {
55
53
  const errorMessages: string[] = []
56
54
 
57
55
  if (typeof target == 'string') target = target.trim()
@@ -76,7 +74,7 @@ export class PRulesEngine {
76
74
  }
77
75
 
78
76
  const wrapper: PRulesWrapper<T> = {
79
- value: PUtils.clone(target) as T,
77
+ value: safe ? JSON.parse(JSON.stringify(target)) : target,
80
78
  label
81
79
  }
82
80
  for (const validationFunction of this.collectionFunctions) {