pols-validator 1.0.1 → 2.0.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 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -73
- package/dist/rules.d.ts +7 -4
- package/dist/rules.d.ts.map +1 -1
- package/dist/rules.js +130 -135
- package/dist/rulesEngine.d.ts +21 -10
- package/dist/rulesEngine.d.ts.map +1 -1
- package/dist/rulesEngine.js +69 -13
- package/dist/validate.d.ts +12 -0
- package/dist/validate.d.ts.map +1 -0
- package/dist/validate.js +56 -0
- package/package.json +4 -3
- package/src/index.ts +3 -86
- package/src/rules.ts +143 -158
- package/src/rulesEngine.ts +87 -16
- package/test/validaciones.ts +7 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
export
|
|
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
|
-
export declare const validate: <T>(target: unknown, rules: RulesEngine) => EvaluateResponse<T>;
|
|
14
|
-
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;
|
|
15
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":"
|
|
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,77 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.rules =
|
|
4
|
-
const pols_utils_1 = require("pols-utils");
|
|
3
|
+
exports.rules = void 0;
|
|
5
4
|
const rules_1 = require("./rules");
|
|
6
|
-
|
|
7
|
-
Object.defineProperty(exports, "RulesEngine", { enumerable: true, get: function () { return rulesEngine_1.RulesEngine; } });
|
|
8
|
-
const validate = (target, rules) => {
|
|
9
|
-
const errorMessages = [];
|
|
10
|
-
if (typeof target == 'string')
|
|
11
|
-
target = target.trim();
|
|
12
|
-
const isEmpty = target == null || (typeof target == 'string' && !target);
|
|
13
|
-
const label = rules.label ?? 'Este valor';
|
|
14
|
-
if (rules.required && isEmpty) {
|
|
15
|
-
return {
|
|
16
|
-
error: true,
|
|
17
|
-
success: false,
|
|
18
|
-
messages: [`'${label}' es requerido`]
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
if (isEmpty) {
|
|
22
|
-
return {
|
|
23
|
-
error: false,
|
|
24
|
-
success: true,
|
|
25
|
-
result: rules.default
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
const wrapper = {
|
|
29
|
-
value: pols_utils_1.PUtils.clone(target),
|
|
30
|
-
label
|
|
31
|
-
};
|
|
32
|
-
for (const validationFunction of Object.values(rules.collection)) {
|
|
33
|
-
const result = validationFunction(wrapper);
|
|
34
|
-
if (!result)
|
|
35
|
-
continue;
|
|
36
|
-
if (typeof result == 'string') {
|
|
37
|
-
errorMessages.push(result);
|
|
38
|
-
break;
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
if (wrapper.value == null || typeof wrapper.value != 'object')
|
|
42
|
-
throw new Error(`El valor no es un objeto para ser validado contra un esquema: ${wrapper.value}`);
|
|
43
|
-
const newResult = {};
|
|
44
|
-
for (const key in result.schema) {
|
|
45
|
-
const rulesInside = result.schema[key];
|
|
46
|
-
const labelIndise = rulesInside.label ?? key;
|
|
47
|
-
rulesInside.label = `${result.prefix ? `${result.prefix} ` : ''}${labelIndise}`;
|
|
48
|
-
newResult[key] = wrapper.value[key];
|
|
49
|
-
const result2 = (0, exports.validate)(newResult[key], rulesInside);
|
|
50
|
-
if (result2.error == true) {
|
|
51
|
-
errorMessages.push(...result2.messages);
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
newResult[key] = result2.result;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
wrapper.value = newResult;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
if (errorMessages.length) {
|
|
61
|
-
return {
|
|
62
|
-
error: true,
|
|
63
|
-
success: false,
|
|
64
|
-
messages: errorMessages
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
return {
|
|
69
|
-
error: false,
|
|
70
|
-
success: true,
|
|
71
|
-
result: wrapper.value
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
exports.validate = validate;
|
|
76
|
-
const rules = (params) => new rules_1.Rules(params);
|
|
5
|
+
const rules = (params) => new rules_1.PRules(params);
|
|
77
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;
|
|
@@ -13,16 +13,19 @@ export declare class Rules extends RulesEngine {
|
|
|
13
13
|
onlyNumbers(): this;
|
|
14
14
|
maxLength(limit: number): this;
|
|
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, PRules>, prefix?: (index: number) => string): this;
|
|
18
20
|
isIn(...elements: unknown[]): this;
|
|
19
21
|
isNotIn(...elements: unknown[]): this;
|
|
22
|
+
hasElements(): this;
|
|
20
23
|
gt(limit: number): this;
|
|
21
24
|
gte(limit: number): this;
|
|
22
25
|
lt(limit: number): this;
|
|
23
26
|
lte(limit: number): this;
|
|
24
27
|
beforeOrSameAsNow(): this;
|
|
25
|
-
isObject(schema?: Record<string,
|
|
28
|
+
isObject(schema?: Record<string, PRules>): this;
|
|
26
29
|
isBoolean(): this;
|
|
27
30
|
upper(): this;
|
|
28
31
|
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;IAiBP,gBAAgB,CAAC,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM;IAoBvG,IAAI,CAAC,GAAG,QAAQ,EAAE,OAAO,EAAE;IAa3B,OAAO,CAAC,GAAG,QAAQ,EAAE,OAAO,EAAE;IAO9B,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,9 +1,49 @@
|
|
|
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
|
-
|
|
6
|
+
const isObject = (context, wrapper, schema) => {
|
|
7
|
+
const message = `'${wrapper.label}' debe ser un objeto`;
|
|
8
|
+
if (typeof wrapper.value == 'string') {
|
|
9
|
+
try {
|
|
10
|
+
wrapper.value = JSON.parse(wrapper.value);
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return message;
|
|
14
|
+
}
|
|
15
|
+
if (pols_utils_1.PUtils.getType(wrapper.value) != 'Object')
|
|
16
|
+
return message;
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
if (pols_utils_1.PUtils.getType(wrapper.value) != 'Object') {
|
|
20
|
+
return message;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/* Realiza la validación de cada propiedad */
|
|
24
|
+
const newWrapperValue = {};
|
|
25
|
+
const errorMessages = [];
|
|
26
|
+
for (const key in schema) {
|
|
27
|
+
const rulesInside = schema[key];
|
|
28
|
+
const labelIndise = rulesInside.label ?? key;
|
|
29
|
+
rulesInside.label = `${context.label ? `${context.label}${context.separator}` : ''}${labelIndise}`;
|
|
30
|
+
newWrapperValue[key] = wrapper.value[key];
|
|
31
|
+
const result2 = rulesInside.validate(newWrapperValue[key]);
|
|
32
|
+
if (result2.error == true) {
|
|
33
|
+
errorMessages.push(...result2.messages);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
newWrapperValue[key] = result2.result;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (errorMessages.length) {
|
|
40
|
+
return errorMessages;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
wrapper.value = newWrapperValue;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
class PRules extends rulesEngine_1.PRulesEngine {
|
|
7
47
|
isAlphanumeric() {
|
|
8
48
|
this.add(this.isAlphanumeric.name, (wrapper) => {
|
|
9
49
|
if (typeof wrapper.value == 'number') {
|
|
@@ -114,107 +154,90 @@ class Rules extends rulesEngine_1.RulesEngine {
|
|
|
114
154
|
return this;
|
|
115
155
|
}
|
|
116
156
|
maxLength(limit) {
|
|
117
|
-
this.
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
157
|
+
return this.add(this.maxLength.name, (wrapper) => {
|
|
158
|
+
if (wrapper.value instanceof Array) {
|
|
159
|
+
return wrapper.value.length > limit ? `'${wrapper.label}' debe contener '${limit} ${limit == 1 ? 'elementos' : 'elementos'}' como máximo` : null;
|
|
160
|
+
}
|
|
161
|
+
else if (typeof wrapper.value == 'string') {
|
|
162
|
+
return wrapper.value.length > limit ? `'${wrapper.label}' debe contener '${limit} ${limit == 1 ? 'caracter' : 'caracteres'}' como máximo` : null;
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
return `'${wrapper.label}' no es de un tipo válido`;
|
|
166
|
+
}
|
|
121
167
|
});
|
|
122
|
-
return this;
|
|
123
168
|
}
|
|
124
|
-
// maxCount(limit: number) {
|
|
125
|
-
// this.isArray()
|
|
126
|
-
// this.add(this.maxLength.name, (wrapper: Wrapper<unknown[]>) => {
|
|
127
|
-
// if (wrapper.value.length > limit) return `'${wrapper.label}' debe contener '${limit} ${limit == 1 ? 'elemento' : 'elementos'}' como máximo`
|
|
128
|
-
// })
|
|
129
|
-
// return this
|
|
130
|
-
// }
|
|
131
169
|
minLength(limit) {
|
|
132
|
-
this.
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
170
|
+
return this.add(this.minLength.name, (wrapper) => {
|
|
171
|
+
if (wrapper.value instanceof Array) {
|
|
172
|
+
return wrapper.value.length < limit ? `'${wrapper.label}' debe contener '${limit} ${limit == 1 ? 'elementos' : 'elementos'}' como mínimo` : null;
|
|
173
|
+
}
|
|
174
|
+
else if (typeof wrapper.value == 'string') {
|
|
175
|
+
return wrapper.value.length < limit ? `'${wrapper.label}' debe contener '${limit} ${limit == 1 ? 'caracter' : 'caracteres'}' como mínimo` : null;
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
return `'${wrapper.label}' no es de un tipo válido`;
|
|
179
|
+
}
|
|
136
180
|
});
|
|
137
|
-
return this;
|
|
138
181
|
}
|
|
139
|
-
|
|
140
|
-
this.
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
182
|
+
hasFixedLength(limit) {
|
|
183
|
+
return this.add(this.hasFixedLength.name, (wrapper) => {
|
|
184
|
+
if (wrapper.value instanceof Array) {
|
|
185
|
+
return wrapper.value.length < limit ? `'${wrapper.label}' debe contener sólo '${limit} ${limit == 1 ? 'elementos' : 'elementos'}'` : null;
|
|
186
|
+
}
|
|
187
|
+
else if (typeof wrapper.value == 'string') {
|
|
188
|
+
return wrapper.value.length < limit ? `'${wrapper.label}' debe contener sólo '${limit} ${limit == 1 ? 'caracter' : 'caracteres'}'` : null;
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
return `'${wrapper.label}' no es de un tipo válido`;
|
|
192
|
+
}
|
|
144
193
|
});
|
|
145
|
-
return this;
|
|
146
194
|
}
|
|
147
195
|
left(limit) {
|
|
148
|
-
this.isAlphanumeric()
|
|
149
|
-
this.add(this.length.name, (wrapper) => {
|
|
196
|
+
return this.isAlphanumeric().add(this.left.name, (wrapper) => {
|
|
150
197
|
wrapper.value = wrapper.value.substring(0, limit);
|
|
151
198
|
});
|
|
152
|
-
return this;
|
|
153
199
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
// }
|
|
196
|
-
// isArrayOfObjects(checkingElements?: (i: number) => FieldsStructure, prefixString?: (i: number) => string) {
|
|
197
|
-
// this.isArray()
|
|
198
|
-
// this.add(this.isArrayOfObjects.name, (wrapper: Wrapper<unknown[]>) => {
|
|
199
|
-
// const message = `'${wrapper.label}' debe ser una lista de objetos`
|
|
200
|
-
// const messages: string[] = []
|
|
201
|
-
// for (const [i, v] of wrapper.value.entries()) {
|
|
202
|
-
// const result = isObject(v)
|
|
203
|
-
// if (result.error == true) return message
|
|
204
|
-
// wrapper.value[i] = result.result
|
|
205
|
-
// if (checkingElements) {
|
|
206
|
-
// const v = wrapper.validator(wrapper.value[i], checkingElements(i), prefixString?.(i))
|
|
207
|
-
// if (v.error == true) {
|
|
208
|
-
// messages.push(...v.messages)
|
|
209
|
-
// } else {
|
|
210
|
-
// wrapper.value[i] = v.result
|
|
211
|
-
// }
|
|
212
|
-
// }
|
|
213
|
-
// }
|
|
214
|
-
// if (messages.length) return messages
|
|
215
|
-
// })
|
|
216
|
-
// return this
|
|
217
|
-
// }
|
|
200
|
+
isArray() {
|
|
201
|
+
return this.add(this.isArray.name, (wrapper) => {
|
|
202
|
+
const message = `'${wrapper.label}' debe ser una lista de elementos`;
|
|
203
|
+
if (typeof wrapper.value == 'string') {
|
|
204
|
+
try {
|
|
205
|
+
const value = JSON.parse(wrapper.value);
|
|
206
|
+
if (!(value instanceof Array))
|
|
207
|
+
return message;
|
|
208
|
+
wrapper.value = value;
|
|
209
|
+
}
|
|
210
|
+
catch (err) {
|
|
211
|
+
return message;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
if (!(wrapper.value instanceof Array))
|
|
216
|
+
return message;
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
isArrayOfObjects(schema, prefix) {
|
|
221
|
+
return this.isArray().add(this.isArrayOfObjects.name, (wrapper) => {
|
|
222
|
+
const messages = [];
|
|
223
|
+
for (const [i, value] of wrapper.value.entries()) {
|
|
224
|
+
const v = isObject(this, {
|
|
225
|
+
label: prefix?.(i) ?? `Item ${i}`,
|
|
226
|
+
value
|
|
227
|
+
}, schema?.(i));
|
|
228
|
+
if (v) {
|
|
229
|
+
if (v instanceof Array) {
|
|
230
|
+
messages.push(...v);
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
messages.push(v);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (messages.length)
|
|
238
|
+
return messages;
|
|
239
|
+
});
|
|
240
|
+
}
|
|
218
241
|
isIn(...elements) {
|
|
219
242
|
this.add(this.isIn.name, (wrapper) => {
|
|
220
243
|
if (wrapper.value instanceof Array) {
|
|
@@ -237,77 +260,50 @@ class Rules extends rulesEngine_1.RulesEngine {
|
|
|
237
260
|
});
|
|
238
261
|
return this;
|
|
239
262
|
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
// }
|
|
263
|
+
hasElements() {
|
|
264
|
+
return this.isArray().add(this.hasElements.name, (wrapper) => {
|
|
265
|
+
if (!wrapper.value.length)
|
|
266
|
+
return `'${wrapper.label}' debe contenedor al menos un elemento`;
|
|
267
|
+
});
|
|
268
|
+
}
|
|
247
269
|
gt(limit) {
|
|
248
|
-
this.isNumber()
|
|
249
|
-
this.add(this.gt.name, (wrapper) => {
|
|
270
|
+
return this.isNumber().add(this.gt.name, (wrapper) => {
|
|
250
271
|
if (wrapper.value <= limit)
|
|
251
272
|
return `'${wrapper.label}' debe ser mayor a '${limit}'`;
|
|
252
273
|
});
|
|
253
|
-
return this;
|
|
254
274
|
}
|
|
255
275
|
gte(limit) {
|
|
256
|
-
this.isNumber()
|
|
257
|
-
this.add(this.gte.name, (wrapper) => {
|
|
276
|
+
return this.isNumber().add(this.gte.name, (wrapper) => {
|
|
258
277
|
if (wrapper.value < limit)
|
|
259
278
|
return `'${wrapper.label}' debe ser mayor o igual a '${limit}'`;
|
|
260
279
|
});
|
|
261
|
-
return this;
|
|
262
280
|
}
|
|
263
281
|
lt(limit) {
|
|
264
|
-
this.isNumber()
|
|
265
|
-
this.add(this.lt.name, (wrapper) => {
|
|
282
|
+
return this.isNumber().add(this.lt.name, (wrapper) => {
|
|
266
283
|
if (wrapper.value >= limit)
|
|
267
284
|
return `'${wrapper.label}' debe ser menor a '${limit}'`;
|
|
268
285
|
});
|
|
269
|
-
return this;
|
|
270
286
|
}
|
|
271
287
|
lte(limit) {
|
|
272
|
-
return this
|
|
273
|
-
.isNumber()
|
|
274
|
-
.add(this.lte.name, (wrapper) => {
|
|
288
|
+
return this.isNumber().add(this.lte.name, (wrapper) => {
|
|
275
289
|
if (wrapper.value > limit)
|
|
276
290
|
return `'${wrapper.label}' debe ser menor o igual a '${limit}'`;
|
|
277
291
|
});
|
|
278
292
|
}
|
|
279
293
|
beforeOrSameAsNow() {
|
|
280
|
-
return this
|
|
281
|
-
.isDateTime()
|
|
282
|
-
.add(this.beforeOrSameAsNow.name, (wrapper) => {
|
|
294
|
+
return this.isDateTime().add(this.beforeOrSameAsNow.name, (wrapper) => {
|
|
283
295
|
const now = new pols_utils_1.PDate;
|
|
284
296
|
if (wrapper.value.time > now.time)
|
|
285
297
|
return `'${wrapper.label}' debe ser anterior o igual a 'ahora'`;
|
|
286
298
|
});
|
|
287
299
|
}
|
|
288
|
-
isObject(schema
|
|
300
|
+
isObject(schema) {
|
|
289
301
|
return this.add(this.isObject.name, (wrapper) => {
|
|
290
|
-
|
|
291
|
-
if (typeof wrapper.value == 'string') {
|
|
292
|
-
try {
|
|
293
|
-
wrapper.value = JSON.parse(wrapper.value);
|
|
294
|
-
}
|
|
295
|
-
catch {
|
|
296
|
-
return message;
|
|
297
|
-
}
|
|
298
|
-
if (pols_utils_1.PUtils.getType(wrapper.value) != 'Object')
|
|
299
|
-
return message;
|
|
300
|
-
}
|
|
301
|
-
else {
|
|
302
|
-
if (pols_utils_1.PUtils.getType(wrapper.value) != 'Object') {
|
|
303
|
-
return message;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
return { schema, prefix };
|
|
302
|
+
return isObject(this, wrapper, schema);
|
|
307
303
|
});
|
|
308
304
|
}
|
|
309
305
|
isBoolean() {
|
|
310
|
-
this.add(this.isBoolean.name, (wrapper) => {
|
|
306
|
+
return this.add(this.isBoolean.name, (wrapper) => {
|
|
311
307
|
const message = `'${wrapper.label}' debe ser de tipo booleano`;
|
|
312
308
|
if (typeof wrapper.value == 'string') {
|
|
313
309
|
const value = wrapper.value.trim().toUpperCase();
|
|
@@ -355,7 +351,6 @@ class Rules extends rulesEngine_1.RulesEngine {
|
|
|
355
351
|
return typeof wrapper.value == 'boolean' ? null : message;
|
|
356
352
|
}
|
|
357
353
|
});
|
|
358
|
-
return this;
|
|
359
354
|
}
|
|
360
355
|
upper() {
|
|
361
356
|
this.isAlphanumeric();
|
|
@@ -419,4 +414,4 @@ class Rules extends rulesEngine_1.RulesEngine {
|
|
|
419
414
|
});
|
|
420
415
|
}
|
|
421
416
|
}
|
|
422
|
-
exports.
|
|
417
|
+
exports.PRules = PRules;
|
package/dist/rulesEngine.d.ts
CHANGED
|
@@ -1,24 +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
|
-
|
|
10
|
-
prefix?: string;
|
|
11
|
-
};
|
|
12
|
-
export type Wrapper<T = unknown> = {
|
|
9
|
+
export type PRulesFunction = (wrapper: PRulesWrapper, ...args: unknown[]) => string | string[] | void | null | undefined;
|
|
10
|
+
export type PRulesWrapper<T = unknown> = {
|
|
13
11
|
value: T;
|
|
14
12
|
label: string;
|
|
15
13
|
};
|
|
16
|
-
export
|
|
17
|
-
|
|
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;
|
|
18
25
|
label: string;
|
|
26
|
+
separator: string;
|
|
19
27
|
required: boolean;
|
|
20
28
|
default: unknown;
|
|
21
|
-
|
|
22
|
-
|
|
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>;
|
|
23
34
|
}
|
|
24
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,22 +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
|
-
|
|
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
|
+
}
|
|
16
19
|
add(name, validationFunction) {
|
|
17
|
-
if (!this.
|
|
18
|
-
this.
|
|
20
|
+
if (!this.collectionNames.includes(name)) {
|
|
21
|
+
this.collectionNames.push(name);
|
|
22
|
+
this.collectionFunctions.push(validationFunction);
|
|
23
|
+
}
|
|
19
24
|
return this;
|
|
20
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
|
+
};
|
|
21
77
|
}
|
|
22
|
-
exports.
|
|
78
|
+
exports.PRulesEngine = PRulesEngine;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { RulesEngine } from "./rulesEngine";
|
|
2
|
+
export type EvaluateResponse<T> = {
|
|
3
|
+
error: false;
|
|
4
|
+
success: true;
|
|
5
|
+
result: T;
|
|
6
|
+
} | {
|
|
7
|
+
error: true;
|
|
8
|
+
success: false;
|
|
9
|
+
messages: string[];
|
|
10
|
+
};
|
|
11
|
+
export declare const validate: <T>(target: unknown, rules: RulesEngine) => EvaluateResponse<T>;
|
|
12
|
+
//# sourceMappingURL=validate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../src/validate.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAW,MAAM,eAAe,CAAA;AAEpD,MAAM,MAAM,gBAAgB,CAAC,CAAC,IAAI;IACjC,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,eAAO,MAAM,QAAQ,GAAI,CAAC,UAAU,OAAO,SAAS,WAAW,KAAG,gBAAgB,CAAC,CAAC,CAoDnF,CAAA"}
|