pols-validator 1.1.0 → 2.1.0
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/index.d.ts +3 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -6
- package/dist/rules.d.ts +4 -5
- package/dist/rules.d.ts.map +1 -1
- package/dist/rules.js +21 -30
- package/dist/rulesEngine.d.ts +21 -7
- package/dist/rulesEngine.d.ts.map +1 -1
- package/dist/rulesEngine.js +70 -13
- package/package.json +4 -3
- package/src/index.ts +3 -7
- package/src/rules.ts +50 -59
- package/src/rulesEngine.ts +87 -13
- package/test/validaciones.ts +15 -7
- package/src/validate.ts +0 -66
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
export
|
|
4
|
-
export { EvaluateResponse, validate } from './validate';
|
|
5
|
-
export declare const rules: (params?: RulesParams) => Rules;
|
|
1
|
+
import { PRules } from "./rules";
|
|
2
|
+
import { PRulesParams } from "./rulesEngine";
|
|
3
|
+
export declare const rules: (params?: PRulesParams) => PRules;
|
|
6
4
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAChC,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAE5C,eAAO,MAAM,KAAK,YAAa,YAAY,WAAuB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.rules =
|
|
3
|
+
exports.rules = void 0;
|
|
4
4
|
const rules_1 = require("./rules");
|
|
5
|
-
|
|
6
|
-
Object.defineProperty(exports, "RulesEngine", { enumerable: true, get: function () { return rulesEngine_1.RulesEngine; } });
|
|
7
|
-
var validate_1 = require("./validate");
|
|
8
|
-
Object.defineProperty(exports, "validate", { enumerable: true, get: function () { return validate_1.validate; } });
|
|
9
|
-
const rules = (params) => new rules_1.Rules(params);
|
|
5
|
+
const rules = (params) => new rules_1.PRules(params);
|
|
10
6
|
exports.rules = rules;
|
package/dist/rules.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare class
|
|
1
|
+
import { PRulesEngine } from "./rulesEngine";
|
|
2
|
+
export declare class PRules extends PRulesEngine {
|
|
3
3
|
isAlphanumeric(): this;
|
|
4
4
|
isEmailAddress(): this;
|
|
5
5
|
isDateTime(): this;
|
|
@@ -15,8 +15,7 @@ export declare class Rules extends RulesEngine {
|
|
|
15
15
|
minLength(limit: number): this;
|
|
16
16
|
hasFixedLength(limit: number): this;
|
|
17
17
|
left(limit: number): this;
|
|
18
|
-
isArray(): this;
|
|
19
|
-
isArrayOfObjects(schema?: (index: number) => Record<string, RulesEngine>, prefix?: (index: number) => string): this;
|
|
18
|
+
isArray(rulesGenerator?: (index: number) => PRules): this;
|
|
20
19
|
isIn(...elements: unknown[]): this;
|
|
21
20
|
isNotIn(...elements: unknown[]): this;
|
|
22
21
|
hasElements(): this;
|
|
@@ -25,7 +24,7 @@ export declare class Rules extends RulesEngine {
|
|
|
25
24
|
lt(limit: number): this;
|
|
26
25
|
lte(limit: number): this;
|
|
27
26
|
beforeOrSameAsNow(): this;
|
|
28
|
-
isObject(schema?: Record<string,
|
|
27
|
+
isObject(schema?: Record<string, PRules>): this;
|
|
29
28
|
isBoolean(): this;
|
|
30
29
|
upper(): this;
|
|
31
30
|
lower(): this;
|
package/dist/rules.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rules.d.ts","sourceRoot":"","sources":["../src/rules.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"rules.d.ts","sourceRoot":"","sources":["../src/rules.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAiB,MAAM,eAAe,CAAA;AA0C3D,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,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,MAAM,CAAC;IAMlG,UAAU;IAQV,KAAK,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM;CAKhC"}
|
package/dist/rules.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.PRules = void 0;
|
|
4
4
|
const pols_utils_1 = require("pols-utils");
|
|
5
5
|
const rulesEngine_1 = require("./rulesEngine");
|
|
6
|
-
const
|
|
7
|
-
const isObject = (wrapper, schema, prefix) => {
|
|
6
|
+
const isObject = (context, wrapper, schema) => {
|
|
8
7
|
const message = `'${wrapper.label}' debe ser un objeto`;
|
|
9
8
|
if (typeof wrapper.value == 'string') {
|
|
10
9
|
try {
|
|
@@ -27,9 +26,9 @@ const isObject = (wrapper, schema, prefix) => {
|
|
|
27
26
|
for (const key in schema) {
|
|
28
27
|
const rulesInside = schema[key];
|
|
29
28
|
const labelIndise = rulesInside.label ?? key;
|
|
30
|
-
rulesInside.label = `${
|
|
29
|
+
rulesInside.label = `${context.label ? `${context.label}${context.separator}` : ''}${labelIndise}`;
|
|
31
30
|
newWrapperValue[key] = wrapper.value[key];
|
|
32
|
-
const result2 =
|
|
31
|
+
const result2 = rulesInside.validate(newWrapperValue[key]);
|
|
33
32
|
if (result2.error == true) {
|
|
34
33
|
errorMessages.push(...result2.messages);
|
|
35
34
|
}
|
|
@@ -44,7 +43,7 @@ const isObject = (wrapper, schema, prefix) => {
|
|
|
44
43
|
wrapper.value = newWrapperValue;
|
|
45
44
|
}
|
|
46
45
|
};
|
|
47
|
-
class
|
|
46
|
+
class PRules extends rulesEngine_1.PRulesEngine {
|
|
48
47
|
isAlphanumeric() {
|
|
49
48
|
this.add(this.isAlphanumeric.name, (wrapper) => {
|
|
50
49
|
if (typeof wrapper.value == 'number') {
|
|
@@ -198,7 +197,7 @@ class Rules extends rulesEngine_1.RulesEngine {
|
|
|
198
197
|
wrapper.value = wrapper.value.substring(0, limit);
|
|
199
198
|
});
|
|
200
199
|
}
|
|
201
|
-
isArray() {
|
|
200
|
+
isArray(rulesGenerator) {
|
|
202
201
|
return this.add(this.isArray.name, (wrapper) => {
|
|
203
202
|
const message = `'${wrapper.label}' debe ser una lista de elementos`;
|
|
204
203
|
if (typeof wrapper.value == 'string') {
|
|
@@ -216,23 +215,17 @@ class Rules extends rulesEngine_1.RulesEngine {
|
|
|
216
215
|
if (!(wrapper.value instanceof Array))
|
|
217
216
|
return message;
|
|
218
217
|
}
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
isArrayOfObjects(schema, prefix) {
|
|
222
|
-
return this.isArray().add(this.isArrayOfObjects.name, (wrapper) => {
|
|
218
|
+
/* Si se ha definido un generador de reglas, se itera cada elemento */
|
|
223
219
|
const messages = [];
|
|
224
|
-
for (const [i,
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
else {
|
|
234
|
-
messages.push(v);
|
|
235
|
-
}
|
|
220
|
+
for (const [i, element] of wrapper.value.entries()) {
|
|
221
|
+
const rules = rulesGenerator(i);
|
|
222
|
+
rules.label = `${this.label ? `${this.label}${rules.label ? this.separator : ''}` : ''}${rules.label ?? ''}`;
|
|
223
|
+
const result = rules.validate(element);
|
|
224
|
+
if (result.error == true) {
|
|
225
|
+
messages.push(...result.messages);
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
wrapper.value[i] = result.result;
|
|
236
229
|
}
|
|
237
230
|
}
|
|
238
231
|
if (messages.length)
|
|
@@ -240,7 +233,7 @@ class Rules extends rulesEngine_1.RulesEngine {
|
|
|
240
233
|
});
|
|
241
234
|
}
|
|
242
235
|
isIn(...elements) {
|
|
243
|
-
this.add(this.isIn.name, (wrapper) => {
|
|
236
|
+
return this.add(this.isIn.name, (wrapper) => {
|
|
244
237
|
if (wrapper.value instanceof Array) {
|
|
245
238
|
for (const v of wrapper.value) {
|
|
246
239
|
if (!elements.includes(v))
|
|
@@ -252,14 +245,12 @@ class Rules extends rulesEngine_1.RulesEngine {
|
|
|
252
245
|
return `'${wrapper.label}' no tiene un valor válido`;
|
|
253
246
|
}
|
|
254
247
|
});
|
|
255
|
-
return this;
|
|
256
248
|
}
|
|
257
249
|
isNotIn(...elements) {
|
|
258
|
-
this.add(this.isNotIn.name, (wrapper) => {
|
|
250
|
+
return this.add(this.isNotIn.name, (wrapper) => {
|
|
259
251
|
if (elements.includes(wrapper.value))
|
|
260
252
|
return `'${wrapper.label}' no tiene un valor válido`;
|
|
261
253
|
});
|
|
262
|
-
return this;
|
|
263
254
|
}
|
|
264
255
|
hasElements() {
|
|
265
256
|
return this.isArray().add(this.hasElements.name, (wrapper) => {
|
|
@@ -298,9 +289,9 @@ class Rules extends rulesEngine_1.RulesEngine {
|
|
|
298
289
|
return `'${wrapper.label}' debe ser anterior o igual a 'ahora'`;
|
|
299
290
|
});
|
|
300
291
|
}
|
|
301
|
-
isObject(schema
|
|
292
|
+
isObject(schema) {
|
|
302
293
|
return this.add(this.isObject.name, (wrapper) => {
|
|
303
|
-
return isObject(wrapper, schema
|
|
294
|
+
return isObject(this, wrapper, schema);
|
|
304
295
|
});
|
|
305
296
|
}
|
|
306
297
|
isBoolean() {
|
|
@@ -415,4 +406,4 @@ class Rules extends rulesEngine_1.RulesEngine {
|
|
|
415
406
|
});
|
|
416
407
|
}
|
|
417
408
|
}
|
|
418
|
-
exports.
|
|
409
|
+
exports.PRules = PRules;
|
package/dist/rulesEngine.d.ts
CHANGED
|
@@ -1,21 +1,35 @@
|
|
|
1
|
-
export type
|
|
1
|
+
export type PRulesParams = {
|
|
2
2
|
label?: string;
|
|
3
|
+
separator?: string;
|
|
3
4
|
} & ({
|
|
4
5
|
required?: boolean;
|
|
5
6
|
} | {
|
|
6
7
|
default?: unknown;
|
|
7
8
|
});
|
|
8
|
-
export type
|
|
9
|
-
export type
|
|
9
|
+
export type PRulesFunction = (wrapper: PRulesWrapper, ...args: unknown[]) => string | string[] | void | null | undefined;
|
|
10
|
+
export type PRulesWrapper<T = unknown> = {
|
|
10
11
|
value: T;
|
|
11
12
|
label: string;
|
|
12
13
|
};
|
|
13
|
-
export
|
|
14
|
-
|
|
14
|
+
export type PRulesResponse<T> = {
|
|
15
|
+
error: false;
|
|
16
|
+
success: true;
|
|
17
|
+
result: T;
|
|
18
|
+
} | {
|
|
19
|
+
error: true;
|
|
20
|
+
success: false;
|
|
21
|
+
messages: string[];
|
|
22
|
+
};
|
|
23
|
+
export declare class PRulesEngine {
|
|
24
|
+
prefix: string;
|
|
15
25
|
label: string;
|
|
26
|
+
separator: string;
|
|
16
27
|
required: boolean;
|
|
17
28
|
default: unknown;
|
|
18
|
-
|
|
19
|
-
|
|
29
|
+
private collectionFunctions;
|
|
30
|
+
private collectionNames;
|
|
31
|
+
constructor(params?: PRulesParams);
|
|
32
|
+
protected add(name: string, validationFunction: PRulesFunction): this;
|
|
33
|
+
validate: <T>(target: unknown) => PRulesResponse<T>;
|
|
20
34
|
}
|
|
21
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":"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;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,GAAI,CAAC,UAAU,OAAO,KAAG,cAAc,CAAC,CAAC,CAAC,CAoDjD;CACD"}
|
package/dist/rulesEngine.js
CHANGED
|
@@ -1,21 +1,78 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
return;
|
|
8
|
-
this.label = params.label;
|
|
9
|
-
this.required = 'required' in params ? params.required : false;
|
|
10
|
-
this.default = 'default' in params ? params.default : null;
|
|
11
|
-
}
|
|
3
|
+
exports.PRulesEngine = void 0;
|
|
4
|
+
const pols_utils_1 = require("pols-utils");
|
|
5
|
+
class PRulesEngine {
|
|
6
|
+
prefix;
|
|
12
7
|
label;
|
|
8
|
+
separator;
|
|
13
9
|
required = false;
|
|
14
10
|
default = null;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
collectionFunctions = [];
|
|
12
|
+
collectionNames = [];
|
|
13
|
+
constructor(params) {
|
|
14
|
+
this.label = params?.label;
|
|
15
|
+
this.separator = params?.separator ?? ' > ';
|
|
16
|
+
this.required = params && 'required' in params ? params?.required : false;
|
|
17
|
+
this.default = params && 'default' in params ? params?.default : null;
|
|
18
|
+
}
|
|
19
|
+
add(name, validationFunction) {
|
|
20
|
+
if (!this.collectionNames.includes(name)) {
|
|
21
|
+
this.collectionNames.push(name);
|
|
22
|
+
this.collectionFunctions.push(validationFunction);
|
|
23
|
+
}
|
|
18
24
|
return this;
|
|
19
25
|
}
|
|
26
|
+
validate = (target) => {
|
|
27
|
+
const errorMessages = [];
|
|
28
|
+
if (typeof target == 'string')
|
|
29
|
+
target = target.trim();
|
|
30
|
+
const isEmpty = target == null || (typeof target == 'string' && !target);
|
|
31
|
+
const label = this.label;
|
|
32
|
+
if (this.required && isEmpty) {
|
|
33
|
+
return {
|
|
34
|
+
error: true,
|
|
35
|
+
success: false,
|
|
36
|
+
messages: [`'${label}' es requerido`]
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
if (isEmpty) {
|
|
40
|
+
return {
|
|
41
|
+
error: false,
|
|
42
|
+
success: true,
|
|
43
|
+
result: this.default
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const wrapper = {
|
|
47
|
+
value: pols_utils_1.PUtils.clone(target),
|
|
48
|
+
label
|
|
49
|
+
};
|
|
50
|
+
for (const validationFunction of this.collectionFunctions) {
|
|
51
|
+
const result = validationFunction(wrapper);
|
|
52
|
+
if (!result)
|
|
53
|
+
continue;
|
|
54
|
+
if (typeof result == 'string') {
|
|
55
|
+
errorMessages.push(result);
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
errorMessages.push(...result);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (errorMessages.length) {
|
|
63
|
+
return {
|
|
64
|
+
error: true,
|
|
65
|
+
success: false,
|
|
66
|
+
messages: errorMessages
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
return {
|
|
71
|
+
error: false,
|
|
72
|
+
success: true,
|
|
73
|
+
result: wrapper.value
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
};
|
|
20
77
|
}
|
|
21
|
-
exports.
|
|
78
|
+
exports.PRulesEngine = PRulesEngine;
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pols-validator",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "npx tsc",
|
|
9
|
-
"test": "npx ts-node-dev -r tsconfig-paths/register --project tsconfig.json"
|
|
9
|
+
"test": "npx ts-node-dev -r tsconfig-paths/register --project tsconfig.json",
|
|
10
|
+
"export": "npm run build && npm publish"
|
|
10
11
|
},
|
|
11
12
|
"author": "Jean Paul Sánchez mendoza",
|
|
12
13
|
"license": "ISC",
|
|
@@ -21,6 +22,6 @@
|
|
|
21
22
|
"typescript-eslint": "^8.11.0"
|
|
22
23
|
},
|
|
23
24
|
"dependencies": {
|
|
24
|
-
"pols-utils": "^1.
|
|
25
|
+
"pols-utils": "^1.3.5"
|
|
25
26
|
}
|
|
26
27
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { PRules } from "./rules"
|
|
2
|
+
import { PRulesParams } from "./rulesEngine"
|
|
3
3
|
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
export { EvaluateResponse, validate } from './validate'
|
|
7
|
-
|
|
8
|
-
export const rules = (params?: RulesParams) => new Rules(params)
|
|
4
|
+
export const rules = (params?: PRulesParams) => new PRules(params)
|
package/src/rules.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { PDate, PUtils } from "pols-utils"
|
|
2
|
-
import {
|
|
3
|
-
import { validate } from "./validate"
|
|
2
|
+
import { PRulesEngine, PRulesWrapper } from "./rulesEngine"
|
|
4
3
|
|
|
5
|
-
const isObject = (wrapper:
|
|
4
|
+
const isObject = (context: PRules, wrapper: PRulesWrapper, schema?: Record<string, PRules>) => {
|
|
6
5
|
const message = `'${wrapper.label}' debe ser un objeto`
|
|
7
6
|
|
|
8
7
|
if (typeof wrapper.value == 'string') {
|
|
@@ -24,11 +23,11 @@ const isObject = (wrapper: Wrapper, schema?: Record<string, RulesEngine>, prefix
|
|
|
24
23
|
for (const key in schema) {
|
|
25
24
|
const rulesInside = schema[key]
|
|
26
25
|
const labelIndise = rulesInside.label ?? key
|
|
27
|
-
rulesInside.label = `${
|
|
26
|
+
rulesInside.label = `${context.label ? `${context.label}${context.separator}` : ''}${labelIndise}`
|
|
28
27
|
|
|
29
28
|
newWrapperValue[key] = wrapper.value[key]
|
|
30
29
|
|
|
31
|
-
const result2 = validate(newWrapperValue[key]
|
|
30
|
+
const result2 = rulesInside.validate(newWrapperValue[key])
|
|
32
31
|
if (result2.error == true) {
|
|
33
32
|
errorMessages.push(...result2.messages)
|
|
34
33
|
} else {
|
|
@@ -42,9 +41,9 @@ const isObject = (wrapper: Wrapper, schema?: Record<string, RulesEngine>, prefix
|
|
|
42
41
|
}
|
|
43
42
|
}
|
|
44
43
|
|
|
45
|
-
export class
|
|
44
|
+
export class PRules extends PRulesEngine {
|
|
46
45
|
isAlphanumeric() {
|
|
47
|
-
this.add(this.isAlphanumeric.name, (wrapper:
|
|
46
|
+
this.add(this.isAlphanumeric.name, (wrapper: PRulesWrapper) => {
|
|
48
47
|
if (typeof wrapper.value == 'number') {
|
|
49
48
|
wrapper.value = wrapper.value.toString()
|
|
50
49
|
} else if (typeof wrapper.value != 'string') {
|
|
@@ -56,14 +55,14 @@ export class Rules extends RulesEngine {
|
|
|
56
55
|
|
|
57
56
|
isEmailAddress() {
|
|
58
57
|
this.isAlphanumeric()
|
|
59
|
-
this.add(this.isEmailAddress.name, (wrapper:
|
|
58
|
+
this.add(this.isEmailAddress.name, (wrapper: PRulesWrapper) => {
|
|
60
59
|
if (!(wrapper.value as string).match(/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/)) return `'${wrapper.label}' debe ser una dirección de correo`
|
|
61
60
|
})
|
|
62
61
|
return this
|
|
63
62
|
}
|
|
64
63
|
|
|
65
64
|
isDateTime() {
|
|
66
|
-
this.add(this.isDateTime.name, (wrapper:
|
|
65
|
+
this.add(this.isDateTime.name, (wrapper: PRulesWrapper) => {
|
|
67
66
|
const message = `'${wrapper.label}' tiene un formato de fecha y hora no válido`
|
|
68
67
|
if (typeof wrapper.value == 'string' || typeof wrapper.value == 'number' || wrapper.value instanceof Date || wrapper.value instanceof PDate) {
|
|
69
68
|
const newDate = new PDate(wrapper.value)
|
|
@@ -77,13 +76,13 @@ export class Rules extends RulesEngine {
|
|
|
77
76
|
}
|
|
78
77
|
|
|
79
78
|
isDate() {
|
|
80
|
-
return this.isDateTime().add(this.isDate.name, (wrapper:
|
|
79
|
+
return this.isDateTime().add(this.isDate.name, (wrapper: PRulesWrapper<PDate>) => {
|
|
81
80
|
wrapper.value.clearTime()
|
|
82
81
|
})
|
|
83
82
|
}
|
|
84
83
|
|
|
85
84
|
isTime() {
|
|
86
|
-
this.add(this.isTime.name, (wrapper:
|
|
85
|
+
this.add(this.isTime.name, (wrapper: PRulesWrapper) => {
|
|
87
86
|
const message = `'${wrapper.label}' contiene un formato de hora no válido`
|
|
88
87
|
const value = wrapper.value.toString()
|
|
89
88
|
const parts = value.replace(/[.,]/g, ':').replace('m', '').match(/^([0-2]?[0-9])(:?)([0-5]?[0-9]?)(:?)([0-5]?[0-9]?)([ap]?)\.?m?\.?$/)
|
|
@@ -112,14 +111,14 @@ export class Rules extends RulesEngine {
|
|
|
112
111
|
|
|
113
112
|
match(pattern: RegExp) {
|
|
114
113
|
this.isAlphanumeric()
|
|
115
|
-
this.add(this.match.name, (wrapper:
|
|
114
|
+
this.add(this.match.name, (wrapper: PRulesWrapper) => {
|
|
116
115
|
if (!(wrapper.value as string).match(pattern)) return `'${wrapper.label}' no cumple con el formato de texto deseado`
|
|
117
116
|
})
|
|
118
117
|
return this
|
|
119
118
|
}
|
|
120
119
|
|
|
121
120
|
isNumber() {
|
|
122
|
-
this.add(this.isNumber.name, (wrapper:
|
|
121
|
+
this.add(this.isNumber.name, (wrapper: PRulesWrapper) => {
|
|
123
122
|
const message = `'${wrapper.label}' debe ser un número`
|
|
124
123
|
const value = Number(wrapper.value)
|
|
125
124
|
if (isNaN(value) || value == Infinity) return message
|
|
@@ -130,7 +129,7 @@ export class Rules extends RulesEngine {
|
|
|
130
129
|
|
|
131
130
|
isInteger() {
|
|
132
131
|
this.isNumber()
|
|
133
|
-
this.add(this.isInteger.name, (wrapper:
|
|
132
|
+
this.add(this.isInteger.name, (wrapper: PRulesWrapper) => {
|
|
134
133
|
if (wrapper.value != Math.floor(wrapper.value as number)) return `'${wrapper.label}' debe ser un número entero`
|
|
135
134
|
})
|
|
136
135
|
return this
|
|
@@ -146,14 +145,14 @@ export class Rules extends RulesEngine {
|
|
|
146
145
|
|
|
147
146
|
onlyNumbers() {
|
|
148
147
|
this.isAlphanumeric()
|
|
149
|
-
this.add(this.onlyNumbers.name, (wrapper:
|
|
148
|
+
this.add(this.onlyNumbers.name, (wrapper: PRulesWrapper) => {
|
|
150
149
|
if (!(wrapper.value as string).match(/^[0-9]+$/)) return `'${wrapper.label}' debe contener sólo números`
|
|
151
150
|
})
|
|
152
151
|
return this
|
|
153
152
|
}
|
|
154
153
|
|
|
155
154
|
maxLength(limit: number) {
|
|
156
|
-
return this.add(this.maxLength.name, (wrapper:
|
|
155
|
+
return this.add(this.maxLength.name, (wrapper: PRulesWrapper) => {
|
|
157
156
|
if (wrapper.value instanceof Array) {
|
|
158
157
|
return wrapper.value.length > limit ? `'${wrapper.label}' debe contener '${limit} ${limit == 1 ? 'elementos' : 'elementos'}' como máximo` : null
|
|
159
158
|
} else if (typeof wrapper.value == 'string') {
|
|
@@ -165,7 +164,7 @@ export class Rules extends RulesEngine {
|
|
|
165
164
|
}
|
|
166
165
|
|
|
167
166
|
minLength(limit: number) {
|
|
168
|
-
return this.add(this.minLength.name, (wrapper:
|
|
167
|
+
return this.add(this.minLength.name, (wrapper: PRulesWrapper) => {
|
|
169
168
|
if (wrapper.value instanceof Array) {
|
|
170
169
|
return wrapper.value.length < limit ? `'${wrapper.label}' debe contener '${limit} ${limit == 1 ? 'elementos' : 'elementos'}' como mínimo` : null
|
|
171
170
|
} else if (typeof wrapper.value == 'string') {
|
|
@@ -177,7 +176,7 @@ export class Rules extends RulesEngine {
|
|
|
177
176
|
}
|
|
178
177
|
|
|
179
178
|
hasFixedLength(limit: number) {
|
|
180
|
-
return this.add(this.hasFixedLength.name, (wrapper:
|
|
179
|
+
return this.add(this.hasFixedLength.name, (wrapper: PRulesWrapper) => {
|
|
181
180
|
if (wrapper.value instanceof Array) {
|
|
182
181
|
return wrapper.value.length < limit ? `'${wrapper.label}' debe contener sólo '${limit} ${limit == 1 ? 'elementos' : 'elementos'}'` : null
|
|
183
182
|
} else if (typeof wrapper.value == 'string') {
|
|
@@ -189,13 +188,13 @@ export class Rules extends RulesEngine {
|
|
|
189
188
|
}
|
|
190
189
|
|
|
191
190
|
left(limit: number) {
|
|
192
|
-
return this.isAlphanumeric().add(this.left.name, (wrapper:
|
|
191
|
+
return this.isAlphanumeric().add(this.left.name, (wrapper: PRulesWrapper) => {
|
|
193
192
|
(wrapper.value as string) = (wrapper.value as string).substring(0, limit)
|
|
194
193
|
})
|
|
195
194
|
}
|
|
196
195
|
|
|
197
|
-
isArray() {
|
|
198
|
-
return this.add(this.isArray.name, (wrapper:
|
|
196
|
+
isArray(rulesGenerator?: (index: number) => PRules) {
|
|
197
|
+
return this.add(this.isArray.name, (wrapper: PRulesWrapper<unknown[]>) => {
|
|
199
198
|
const message = `'${wrapper.label}' debe ser una lista de elementos`
|
|
200
199
|
if (typeof wrapper.value == 'string') {
|
|
201
200
|
try {
|
|
@@ -208,23 +207,17 @@ export class Rules extends RulesEngine {
|
|
|
208
207
|
} else {
|
|
209
208
|
if (!(wrapper.value instanceof Array)) return message
|
|
210
209
|
}
|
|
211
|
-
})
|
|
212
|
-
}
|
|
213
210
|
|
|
214
|
-
|
|
215
|
-
return this.isArray().add(this.isArrayOfObjects.name, (wrapper: Wrapper<unknown[]>) => {
|
|
211
|
+
/* Si se ha definido un generador de reglas, se itera cada elemento */
|
|
216
212
|
const messages: string[] = []
|
|
217
|
-
for (const [i,
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
} else {
|
|
226
|
-
messages.push(v)
|
|
227
|
-
}
|
|
213
|
+
for (const [i, element] of wrapper.value.entries()) {
|
|
214
|
+
const rules = rulesGenerator(i)
|
|
215
|
+
rules.label = `${this.label ? `${this.label}${rules.label ? this.separator : ''}` : ''}${rules.label ?? ''}`
|
|
216
|
+
const result = rules.validate(element)
|
|
217
|
+
if (result.error == true) {
|
|
218
|
+
messages.push(...result.messages)
|
|
219
|
+
} else {
|
|
220
|
+
wrapper.value[i] = result.result
|
|
228
221
|
}
|
|
229
222
|
}
|
|
230
223
|
if (messages.length) return messages
|
|
@@ -232,7 +225,7 @@ export class Rules extends RulesEngine {
|
|
|
232
225
|
}
|
|
233
226
|
|
|
234
227
|
isIn(...elements: unknown[]) {
|
|
235
|
-
this.add(this.isIn.name, (wrapper:
|
|
228
|
+
return this.add(this.isIn.name, (wrapper: PRulesWrapper) => {
|
|
236
229
|
if (wrapper.value instanceof Array) {
|
|
237
230
|
for (const v of wrapper.value) {
|
|
238
231
|
if (!elements.includes(v)) return `'${wrapper.label}' no contiene valores válidos`
|
|
@@ -241,61 +234,59 @@ export class Rules extends RulesEngine {
|
|
|
241
234
|
if (!elements.includes(wrapper.value)) return `'${wrapper.label}' no tiene un valor válido`
|
|
242
235
|
}
|
|
243
236
|
})
|
|
244
|
-
return this
|
|
245
237
|
}
|
|
246
238
|
|
|
247
239
|
isNotIn(...elements: unknown[]) {
|
|
248
|
-
this.add(this.isNotIn.name, (wrapper:
|
|
240
|
+
return this.add(this.isNotIn.name, (wrapper: PRulesWrapper) => {
|
|
249
241
|
if (elements.includes(wrapper.value)) return `'${wrapper.label}' no tiene un valor válido`
|
|
250
242
|
})
|
|
251
|
-
return this
|
|
252
243
|
}
|
|
253
244
|
|
|
254
245
|
hasElements() {
|
|
255
|
-
return this.isArray().add(this.hasElements.name, (wrapper:
|
|
246
|
+
return this.isArray().add(this.hasElements.name, (wrapper: PRulesWrapper<unknown[]>) => {
|
|
256
247
|
if (!wrapper.value.length) return `'${wrapper.label}' debe contenedor al menos un elemento`
|
|
257
248
|
})
|
|
258
249
|
}
|
|
259
250
|
|
|
260
251
|
gt(limit: number) {
|
|
261
|
-
return this.isNumber().add(this.gt.name, (wrapper:
|
|
252
|
+
return this.isNumber().add(this.gt.name, (wrapper: PRulesWrapper) => {
|
|
262
253
|
if (wrapper.value as number <= limit) return `'${wrapper.label}' debe ser mayor a '${limit}'`
|
|
263
254
|
})
|
|
264
255
|
}
|
|
265
256
|
|
|
266
257
|
gte(limit: number) {
|
|
267
|
-
return this.isNumber().add(this.gte.name, (wrapper:
|
|
258
|
+
return this.isNumber().add(this.gte.name, (wrapper: PRulesWrapper) => {
|
|
268
259
|
if (wrapper.value as number < limit) return `'${wrapper.label}' debe ser mayor o igual a '${limit}'`
|
|
269
260
|
})
|
|
270
261
|
}
|
|
271
262
|
|
|
272
263
|
lt(limit: number) {
|
|
273
|
-
return this.isNumber().add(this.lt.name, (wrapper:
|
|
264
|
+
return this.isNumber().add(this.lt.name, (wrapper: PRulesWrapper) => {
|
|
274
265
|
if (wrapper.value as number >= limit) return `'${wrapper.label}' debe ser menor a '${limit}'`
|
|
275
266
|
})
|
|
276
267
|
}
|
|
277
268
|
|
|
278
269
|
lte(limit: number) {
|
|
279
|
-
return this.isNumber().add(this.lte.name, (wrapper:
|
|
270
|
+
return this.isNumber().add(this.lte.name, (wrapper: PRulesWrapper) => {
|
|
280
271
|
if (wrapper.value as number > limit) return `'${wrapper.label}' debe ser menor o igual a '${limit}'`
|
|
281
272
|
})
|
|
282
273
|
}
|
|
283
274
|
|
|
284
275
|
beforeOrSameAsNow() {
|
|
285
|
-
return this.isDateTime().add(this.beforeOrSameAsNow.name, (wrapper:
|
|
276
|
+
return this.isDateTime().add(this.beforeOrSameAsNow.name, (wrapper: PRulesWrapper<PDate>) => {
|
|
286
277
|
const now = new PDate
|
|
287
278
|
if (wrapper.value.time > now.time) return `'${wrapper.label}' debe ser anterior o igual a 'ahora'`
|
|
288
279
|
})
|
|
289
280
|
}
|
|
290
281
|
|
|
291
|
-
isObject(schema?: Record<string,
|
|
292
|
-
return this.add(this.isObject.name, (wrapper:
|
|
293
|
-
return isObject(wrapper, schema
|
|
282
|
+
isObject(schema?: Record<string, PRules>) {
|
|
283
|
+
return this.add(this.isObject.name, (wrapper: PRulesWrapper) => {
|
|
284
|
+
return isObject(this, wrapper, schema)
|
|
294
285
|
})
|
|
295
286
|
}
|
|
296
287
|
|
|
297
288
|
isBoolean() {
|
|
298
|
-
return this.add(this.isBoolean.name, (wrapper:
|
|
289
|
+
return this.add(this.isBoolean.name, (wrapper: PRulesWrapper) => {
|
|
299
290
|
const message = `'${wrapper.label}' debe ser de tipo booleano`
|
|
300
291
|
if (typeof wrapper.value == 'string') {
|
|
301
292
|
const value = wrapper.value.trim().toUpperCase()
|
|
@@ -343,7 +334,7 @@ export class Rules extends RulesEngine {
|
|
|
343
334
|
|
|
344
335
|
upper() {
|
|
345
336
|
this.isAlphanumeric()
|
|
346
|
-
this.add(this.upper.name, (wrapper:
|
|
337
|
+
this.add(this.upper.name, (wrapper: PRulesWrapper) => {
|
|
347
338
|
wrapper.value = (wrapper.value as string).toUpperCase()
|
|
348
339
|
})
|
|
349
340
|
return this
|
|
@@ -351,7 +342,7 @@ export class Rules extends RulesEngine {
|
|
|
351
342
|
|
|
352
343
|
lower() {
|
|
353
344
|
this.isAlphanumeric()
|
|
354
|
-
this.add(this.lower.name, (wrapper:
|
|
345
|
+
this.add(this.lower.name, (wrapper: PRulesWrapper) => {
|
|
355
346
|
wrapper.value = (wrapper.value as string).toLowerCase()
|
|
356
347
|
})
|
|
357
348
|
return this
|
|
@@ -359,7 +350,7 @@ export class Rules extends RulesEngine {
|
|
|
359
350
|
|
|
360
351
|
decodeURI() {
|
|
361
352
|
this.isAlphanumeric()
|
|
362
|
-
this.add(this.decodeURI.name, (wrapper:
|
|
353
|
+
this.add(this.decodeURI.name, (wrapper: PRulesWrapper) => {
|
|
363
354
|
wrapper.value = decodeURI(wrapper.value as string)
|
|
364
355
|
})
|
|
365
356
|
return this
|
|
@@ -367,7 +358,7 @@ export class Rules extends RulesEngine {
|
|
|
367
358
|
|
|
368
359
|
round(decimals: number) {
|
|
369
360
|
this.isNumber()
|
|
370
|
-
this.add(this.round.name, (wrapper:
|
|
361
|
+
this.add(this.round.name, (wrapper: PRulesWrapper) => {
|
|
371
362
|
wrapper.value = PUtils.Number.round(wrapper.value as number, decimals)
|
|
372
363
|
})
|
|
373
364
|
return this
|
|
@@ -376,7 +367,7 @@ export class Rules extends RulesEngine {
|
|
|
376
367
|
/* Reemplaza todos los dobles (o más) espacios juntos por uno simple */
|
|
377
368
|
cleanDoubleSpaces() {
|
|
378
369
|
this.isAlphanumeric()
|
|
379
|
-
this.add(this.cleanDoubleSpaces.name, (wrapper:
|
|
370
|
+
this.add(this.cleanDoubleSpaces.name, (wrapper: PRulesWrapper) => {
|
|
380
371
|
wrapper.value = (wrapper.value as string).replace(/\s{2,}/g, ' ')
|
|
381
372
|
})
|
|
382
373
|
return this
|
|
@@ -384,7 +375,7 @@ export class Rules extends RulesEngine {
|
|
|
384
375
|
|
|
385
376
|
noSpaces() {
|
|
386
377
|
this.isAlphanumeric()
|
|
387
|
-
this.add(this.noSpaces.name, (wrapper:
|
|
378
|
+
this.add(this.noSpaces.name, (wrapper: PRulesWrapper) => {
|
|
388
379
|
if ((wrapper.value as string).match(/\s/)) return `'${wrapper.label}' no debe contener 'espacios'`
|
|
389
380
|
})
|
|
390
381
|
return this
|
|
@@ -392,21 +383,21 @@ export class Rules extends RulesEngine {
|
|
|
392
383
|
|
|
393
384
|
|
|
394
385
|
replace(search: string | RegExp, replace: string | ((substring: string, ...args: any[]) => string)) {
|
|
395
|
-
return this.isAlphanumeric().add(this.replace.name, (wrapper:
|
|
386
|
+
return this.isAlphanumeric().add(this.replace.name, (wrapper: PRulesWrapper) => {
|
|
396
387
|
wrapper.value = (wrapper.value as string).replace(search, replace as any)
|
|
397
388
|
})
|
|
398
389
|
}
|
|
399
390
|
|
|
400
391
|
capitalize() {
|
|
401
392
|
this.isAlphanumeric()
|
|
402
|
-
this.add(this.capitalize.name, (wrapper:
|
|
393
|
+
this.add(this.capitalize.name, (wrapper: PRulesWrapper) => {
|
|
403
394
|
wrapper.value = PUtils.String.capitalize(wrapper.value as string)
|
|
404
395
|
})
|
|
405
396
|
return this
|
|
406
397
|
}
|
|
407
398
|
|
|
408
399
|
split(separator: string | RegExp) {
|
|
409
|
-
return this.isAlphanumeric().add(this.split.name, (wrapper:
|
|
400
|
+
return this.isAlphanumeric().add(this.split.name, (wrapper: PRulesWrapper) => {
|
|
410
401
|
wrapper.value = (wrapper.value as any).split(separator)
|
|
411
402
|
})
|
|
412
403
|
}
|
package/src/rulesEngine.ts
CHANGED
|
@@ -1,33 +1,107 @@
|
|
|
1
|
-
|
|
1
|
+
import { PUtils } from "pols-utils"
|
|
2
|
+
|
|
3
|
+
export type PRulesParams = {
|
|
2
4
|
label?: string
|
|
5
|
+
separator?: string
|
|
3
6
|
} & ({
|
|
4
7
|
required?: boolean
|
|
5
8
|
} | {
|
|
6
9
|
default?: unknown
|
|
7
10
|
})
|
|
8
11
|
|
|
9
|
-
export type
|
|
12
|
+
export type PRulesFunction = (wrapper: PRulesWrapper, ...args: unknown[]) => string | string[] | void | null | undefined
|
|
10
13
|
|
|
11
|
-
export type
|
|
14
|
+
export type PRulesWrapper<T = unknown> = {
|
|
12
15
|
value: T
|
|
13
16
|
label: string
|
|
14
17
|
}
|
|
15
18
|
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
export type PRulesResponse<T> = {
|
|
20
|
+
error: false
|
|
21
|
+
success: true
|
|
22
|
+
result: T
|
|
23
|
+
} | {
|
|
24
|
+
error: true
|
|
25
|
+
success: false
|
|
26
|
+
messages: string[]
|
|
27
|
+
}
|
|
23
28
|
|
|
29
|
+
export class PRulesEngine {
|
|
30
|
+
prefix: string
|
|
24
31
|
label: string
|
|
32
|
+
separator: string
|
|
25
33
|
required: boolean = false
|
|
26
34
|
default: unknown = null
|
|
27
|
-
collection: ValidationFunction[] = []
|
|
28
35
|
|
|
29
|
-
|
|
30
|
-
|
|
36
|
+
private collectionFunctions: PRulesFunction[] = []
|
|
37
|
+
private collectionNames: string[] = []
|
|
38
|
+
|
|
39
|
+
constructor(params?: PRulesParams) {
|
|
40
|
+
this.label = params?.label
|
|
41
|
+
this.separator = params?.separator ?? ' > '
|
|
42
|
+
this.required = params && 'required' in params ? params?.required : false
|
|
43
|
+
this.default = params && 'default' in params ? params?.default : null
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
protected add(name: string, validationFunction: PRulesFunction) {
|
|
47
|
+
if (!this.collectionNames.includes(name)) {
|
|
48
|
+
this.collectionNames.push(name)
|
|
49
|
+
this.collectionFunctions.push(validationFunction)
|
|
50
|
+
}
|
|
31
51
|
return this
|
|
32
52
|
}
|
|
53
|
+
|
|
54
|
+
validate = <T>(target: unknown): PRulesResponse<T> => {
|
|
55
|
+
const errorMessages: string[] = []
|
|
56
|
+
|
|
57
|
+
if (typeof target == 'string') target = target.trim()
|
|
58
|
+
|
|
59
|
+
const isEmpty = target == null || (typeof target == 'string' && !target)
|
|
60
|
+
const label = this.label
|
|
61
|
+
|
|
62
|
+
if (this.required && isEmpty) {
|
|
63
|
+
return {
|
|
64
|
+
error: true,
|
|
65
|
+
success: false,
|
|
66
|
+
messages: [`'${label}' es requerido`]
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (isEmpty) {
|
|
71
|
+
return {
|
|
72
|
+
error: false,
|
|
73
|
+
success: true,
|
|
74
|
+
result: this.default as T
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const wrapper: PRulesWrapper<T> = {
|
|
79
|
+
value: PUtils.clone(target) as T,
|
|
80
|
+
label
|
|
81
|
+
}
|
|
82
|
+
for (const validationFunction of this.collectionFunctions) {
|
|
83
|
+
const result = validationFunction(wrapper)
|
|
84
|
+
if (!result) continue
|
|
85
|
+
if (typeof result == 'string') {
|
|
86
|
+
errorMessages.push(result)
|
|
87
|
+
break
|
|
88
|
+
} else {
|
|
89
|
+
errorMessages.push(...result)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (errorMessages.length) {
|
|
94
|
+
return {
|
|
95
|
+
error: true,
|
|
96
|
+
success: false,
|
|
97
|
+
messages: errorMessages
|
|
98
|
+
}
|
|
99
|
+
} else {
|
|
100
|
+
return {
|
|
101
|
+
error: false,
|
|
102
|
+
success: true,
|
|
103
|
+
result: wrapper.value
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
33
107
|
}
|
package/test/validaciones.ts
CHANGED
|
@@ -1,22 +1,30 @@
|
|
|
1
|
-
import { rules
|
|
1
|
+
import { rules } from '../src/index'
|
|
2
2
|
|
|
3
3
|
const original = {
|
|
4
4
|
uno: 'hola',
|
|
5
|
-
dos:
|
|
5
|
+
dos: 'er',
|
|
6
6
|
tres: '67',
|
|
7
7
|
cuatro: {
|
|
8
8
|
aaa: '2024-10-28 23:56:00',
|
|
9
9
|
bbb: 'fgh'
|
|
10
|
-
}
|
|
10
|
+
},
|
|
11
|
+
cinco: [23, {
|
|
12
|
+
ccc: 'asd'
|
|
13
|
+
}]
|
|
11
14
|
}
|
|
12
15
|
|
|
13
|
-
const
|
|
14
|
-
|
|
16
|
+
const otro = 56
|
|
17
|
+
|
|
18
|
+
const resultados = rules({ label: 'Body' }).isObject({
|
|
19
|
+
uno: rules({ label: 'Uno', required: true }).isNatural(),
|
|
15
20
|
dos: rules().isBoolean(),
|
|
16
21
|
tres: rules({ required: true }).isNaturalNoZero(),
|
|
17
22
|
cuatro: rules({ label: 'Cuatro', default: {} }).isObject({
|
|
18
23
|
aaa: rules().isDateTime()
|
|
19
|
-
})
|
|
20
|
-
|
|
24
|
+
}),
|
|
25
|
+
cinco: rules().isArray(i => rules({ label: `Elem ${i + 1}` }).isObject({
|
|
26
|
+
ccc: rules().isNumber()
|
|
27
|
+
}))
|
|
28
|
+
}).validate<typeof original>(original)
|
|
21
29
|
|
|
22
30
|
console.log(original, resultados)
|
package/src/validate.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { PUtils } from "pols-utils"
|
|
2
|
-
import { RulesEngine, Wrapper } from "./rulesEngine"
|
|
3
|
-
|
|
4
|
-
export type EvaluateResponse<T> = {
|
|
5
|
-
error: false
|
|
6
|
-
success: true
|
|
7
|
-
result: T
|
|
8
|
-
} | {
|
|
9
|
-
error: true
|
|
10
|
-
success: false
|
|
11
|
-
messages: string[]
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export const validate = <T>(target: unknown, rules: RulesEngine): EvaluateResponse<T> => {
|
|
15
|
-
const errorMessages: string[] = []
|
|
16
|
-
|
|
17
|
-
if (typeof target == 'string') target = target.trim()
|
|
18
|
-
|
|
19
|
-
const isEmpty = target == null || (typeof target == 'string' && !target)
|
|
20
|
-
const label = rules.label ?? 'Este valor'
|
|
21
|
-
|
|
22
|
-
if (rules.required && isEmpty) {
|
|
23
|
-
return {
|
|
24
|
-
error: true,
|
|
25
|
-
success: false,
|
|
26
|
-
messages: [`'${label}' es requerido`]
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (isEmpty) {
|
|
31
|
-
return {
|
|
32
|
-
error: false,
|
|
33
|
-
success: true,
|
|
34
|
-
result: rules.default as T
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const wrapper: Wrapper<T> = {
|
|
39
|
-
value: PUtils.clone(target) as T,
|
|
40
|
-
label
|
|
41
|
-
}
|
|
42
|
-
for (const validationFunction of rules.collection) {
|
|
43
|
-
const result = validationFunction(wrapper)
|
|
44
|
-
if (!result) continue
|
|
45
|
-
if (typeof result == 'string') {
|
|
46
|
-
errorMessages.push(result)
|
|
47
|
-
break
|
|
48
|
-
} else {
|
|
49
|
-
errorMessages.push(...result)
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (errorMessages.length) {
|
|
54
|
-
return {
|
|
55
|
-
error: true,
|
|
56
|
-
success: false,
|
|
57
|
-
messages: errorMessages
|
|
58
|
-
}
|
|
59
|
-
} else {
|
|
60
|
-
return {
|
|
61
|
-
error: false,
|
|
62
|
-
success: true,
|
|
63
|
-
result: wrapper.value
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|