pols-validator 2.3.5 → 2.3.7
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.
- package/dist/rules.d.ts.map +1 -1
- package/dist/rules.js +13 -11
- package/dist/rulesEngine.d.ts +1 -1
- package/dist/rulesEngine.d.ts.map +1 -1
- package/dist/rulesEngine.js +13 -8
- package/package.json +2 -2
- package/src/rules.ts +11 -11
- package/src/rulesEngine.ts +12 -8
package/dist/rules.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rules.d.ts","sourceRoot":"","sources":["../src/rules.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"rules.d.ts","sourceRoot":"","sources":["../src/rules.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAiB,MAAM,eAAe,CAAA;AA6C3D,qBAAa,MAAO,SAAQ,YAAY;IACvC,cAAc;IAWd,cAAc;IAQd,UAAU;IAUV,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,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 (
|
|
17
|
+
if (wrapper.value == null || typeof wrapper.value != 'object')
|
|
18
18
|
return message;
|
|
19
19
|
}
|
|
20
|
-
else {
|
|
21
|
-
if (
|
|
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
|
}
|
|
@@ -97,12 +99,12 @@ class PRules extends rulesEngine_1.PRulesEngine {
|
|
|
97
99
|
if (hours >= 1 && hours <= 12) {
|
|
98
100
|
if (middle === 'p')
|
|
99
101
|
hours += 12;
|
|
100
|
-
wrapper.value = `${pols_utils_1.
|
|
102
|
+
wrapper.value = `${pols_utils_1.PUtilsString.padLeft(hours, 2)}:${pols_utils_1.PUtilsString.padLeft(minutes, 2)}:${pols_utils_1.PUtilsString.padLeft(seconds, 2)}`;
|
|
101
103
|
return;
|
|
102
104
|
}
|
|
103
105
|
}
|
|
104
106
|
else if (hours >= 0 && hours <= 23) {
|
|
105
|
-
wrapper.value = `${pols_utils_1.
|
|
107
|
+
wrapper.value = `${pols_utils_1.PUtilsString.padLeft(hours, 2)}:${pols_utils_1.PUtilsString.padLeft(minutes, 2)}:${pols_utils_1.PUtilsString.padLeft(seconds, 2)}`;
|
|
106
108
|
return;
|
|
107
109
|
}
|
|
108
110
|
}
|
|
@@ -217,7 +219,7 @@ class PRules extends rulesEngine_1.PRulesEngine {
|
|
|
217
219
|
for (const [i, element] of wrapper.value.entries()) {
|
|
218
220
|
const rules = rulesGenerator(i);
|
|
219
221
|
rules.label = `${this.label ? `${this.label}${rules.label ? this.separator : ''}` : ''}${rules.label ?? ''}`;
|
|
220
|
-
const result = rules.validate(element);
|
|
222
|
+
const result = rules.validate(element, false);
|
|
221
223
|
if (result.error == true) {
|
|
222
224
|
messages.push(...result.messages);
|
|
223
225
|
}
|
|
@@ -365,7 +367,7 @@ class PRules extends rulesEngine_1.PRulesEngine {
|
|
|
365
367
|
round(decimals) {
|
|
366
368
|
this.isNumber();
|
|
367
369
|
this.add(this.round.name, (wrapper) => {
|
|
368
|
-
wrapper.value = pols_utils_1.
|
|
370
|
+
wrapper.value = pols_utils_1.PUtilsNumber.round(wrapper.value, decimals);
|
|
369
371
|
});
|
|
370
372
|
return this;
|
|
371
373
|
}
|
|
@@ -393,7 +395,7 @@ class PRules extends rulesEngine_1.PRulesEngine {
|
|
|
393
395
|
capitalize() {
|
|
394
396
|
this.isAlphanumeric();
|
|
395
397
|
this.add(this.capitalize.name, (wrapper) => {
|
|
396
|
-
wrapper.value = pols_utils_1.
|
|
398
|
+
wrapper.value = pols_utils_1.PUtilsString.capitalize(wrapper.value);
|
|
397
399
|
});
|
|
398
400
|
return this;
|
|
399
401
|
}
|
package/dist/rulesEngine.d.ts
CHANGED
|
@@ -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":"
|
|
1
|
+
{"version":3,"file":"rulesEngine.d.ts","sourceRoot":"","sources":["../src/rulesEngine.ts"],"names":[],"mappings":"AAAA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
@@ -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,10 +22,11 @@ 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();
|
|
29
|
+
const defaultIsEmpty = this.default == null || (typeof this.default == 'string' && !this.default);
|
|
30
30
|
const isEmpty = target == null || (typeof target == 'string' && !target);
|
|
31
31
|
const label = this.label;
|
|
32
32
|
if (this.required && isEmpty) {
|
|
@@ -37,14 +37,19 @@ class PRulesEngine {
|
|
|
37
37
|
};
|
|
38
38
|
}
|
|
39
39
|
if (isEmpty) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
if (!defaultIsEmpty) {
|
|
41
|
+
target = this.default;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
return {
|
|
45
|
+
error: false,
|
|
46
|
+
success: true,
|
|
47
|
+
result: this.default
|
|
48
|
+
};
|
|
49
|
+
}
|
|
45
50
|
}
|
|
46
51
|
const wrapper = {
|
|
47
|
-
value:
|
|
52
|
+
value: safe ? JSON.parse(JSON.stringify(target)) : target,
|
|
48
53
|
label
|
|
49
54
|
};
|
|
50
55
|
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.
|
|
3
|
+
"version": "2.3.7",
|
|
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": "^
|
|
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 (
|
|
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
|
-
|
|
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 {
|
|
@@ -94,11 +94,11 @@ export class PRules extends PRulesEngine {
|
|
|
94
94
|
if (middle) {
|
|
95
95
|
if (hours >= 1 && hours <= 12) {
|
|
96
96
|
if (middle === 'p') hours += 12
|
|
97
|
-
wrapper.value = `${
|
|
97
|
+
wrapper.value = `${PUtilsString.padLeft(hours, 2)}:${PUtilsString.padLeft(minutes, 2)}:${PUtilsString.padLeft(seconds, 2)}`
|
|
98
98
|
return
|
|
99
99
|
}
|
|
100
100
|
} else if (hours >= 0 && hours <= 23) {
|
|
101
|
-
wrapper.value = `${
|
|
101
|
+
wrapper.value = `${PUtilsString.padLeft(hours, 2)}:${PUtilsString.padLeft(minutes, 2)}:${PUtilsString.padLeft(seconds, 2)}`
|
|
102
102
|
return
|
|
103
103
|
}
|
|
104
104
|
}
|
|
@@ -211,7 +211,7 @@ export class PRules extends PRulesEngine {
|
|
|
211
211
|
for (const [i, element] of wrapper.value.entries()) {
|
|
212
212
|
const rules = rulesGenerator(i)
|
|
213
213
|
rules.label = `${this.label ? `${this.label}${rules.label ? this.separator : ''}` : ''}${rules.label ?? ''}`
|
|
214
|
-
const result = rules.validate(element)
|
|
214
|
+
const result = rules.validate(element, false)
|
|
215
215
|
if (result.error == true) {
|
|
216
216
|
messages.push(...result.messages)
|
|
217
217
|
} else {
|
|
@@ -357,7 +357,7 @@ export class PRules extends PRulesEngine {
|
|
|
357
357
|
round(decimals: number) {
|
|
358
358
|
this.isNumber()
|
|
359
359
|
this.add(this.round.name, (wrapper: PRulesWrapper) => {
|
|
360
|
-
wrapper.value =
|
|
360
|
+
wrapper.value = PUtilsNumber.round(wrapper.value as number, decimals)
|
|
361
361
|
})
|
|
362
362
|
return this
|
|
363
363
|
}
|
|
@@ -389,7 +389,7 @@ export class PRules extends PRulesEngine {
|
|
|
389
389
|
capitalize() {
|
|
390
390
|
this.isAlphanumeric()
|
|
391
391
|
this.add(this.capitalize.name, (wrapper: PRulesWrapper) => {
|
|
392
|
-
wrapper.value =
|
|
392
|
+
wrapper.value = PUtilsString.capitalize(wrapper.value as string)
|
|
393
393
|
})
|
|
394
394
|
return this
|
|
395
395
|
}
|
package/src/rulesEngine.ts
CHANGED
|
@@ -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,12 +49,14 @@ 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()
|
|
58
56
|
|
|
57
|
+
const defaultIsEmpty = this.default == null || (typeof this.default == 'string' && !this.default)
|
|
59
58
|
const isEmpty = target == null || (typeof target == 'string' && !target)
|
|
59
|
+
|
|
60
60
|
const label = this.label
|
|
61
61
|
|
|
62
62
|
if (this.required && isEmpty) {
|
|
@@ -68,15 +68,19 @@ export class PRulesEngine {
|
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
if (isEmpty) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
if (!defaultIsEmpty) {
|
|
72
|
+
target = this.default
|
|
73
|
+
} else {
|
|
74
|
+
return {
|
|
75
|
+
error: false,
|
|
76
|
+
success: true,
|
|
77
|
+
result: this.default as T
|
|
78
|
+
}
|
|
75
79
|
}
|
|
76
80
|
}
|
|
77
81
|
|
|
78
82
|
const wrapper: PRulesWrapper<T> = {
|
|
79
|
-
value:
|
|
83
|
+
value: safe ? JSON.parse(JSON.stringify(target)) : target,
|
|
80
84
|
label
|
|
81
85
|
}
|
|
82
86
|
for (const validationFunction of this.collectionFunctions) {
|