pols-validator 2.0.0 → 2.1.1

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 CHANGED
@@ -15,8 +15,7 @@ export declare class PRules extends PRulesEngine {
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
+ isArray(rulesGenerator?: (index: number) => PRules): this;
20
19
  isIn(...elements: unknown[]): this;
21
20
  isNotIn(...elements: unknown[]): this;
22
21
  hasElements(): this;
@@ -36,5 +35,7 @@ export declare class PRules extends PRulesEngine {
36
35
  replace(search: string | RegExp, replace: string | ((substring: string, ...args: any[]) => string)): this;
37
36
  capitalize(): this;
38
37
  split(separator: string | RegExp): this;
38
+ floor(): this;
39
+ ceil(): this;
39
40
  }
40
41
  //# sourceMappingURL=rules.d.ts.map
@@ -1 +1 @@
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,C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,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,KAAK;IAML,IAAI;CAKJ"}
package/dist/rules.js CHANGED
@@ -197,7 +197,7 @@ class PRules extends rulesEngine_1.PRulesEngine {
197
197
  wrapper.value = wrapper.value.substring(0, limit);
198
198
  });
199
199
  }
200
- isArray() {
200
+ isArray(rulesGenerator) {
201
201
  return this.add(this.isArray.name, (wrapper) => {
202
202
  const message = `'${wrapper.label}' debe ser una lista de elementos`;
203
203
  if (typeof wrapper.value == 'string') {
@@ -215,23 +215,17 @@ class PRules extends rulesEngine_1.PRulesEngine {
215
215
  if (!(wrapper.value instanceof Array))
216
216
  return message;
217
217
  }
218
- });
219
- }
220
- isArrayOfObjects(schema, prefix) {
221
- return this.isArray().add(this.isArrayOfObjects.name, (wrapper) => {
218
+ /* Si se ha definido un generador de reglas, se itera cada elemento */
222
219
  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
- }
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;
235
229
  }
236
230
  }
237
231
  if (messages.length)
@@ -239,7 +233,7 @@ class PRules extends rulesEngine_1.PRulesEngine {
239
233
  });
240
234
  }
241
235
  isIn(...elements) {
242
- this.add(this.isIn.name, (wrapper) => {
236
+ return this.add(this.isIn.name, (wrapper) => {
243
237
  if (wrapper.value instanceof Array) {
244
238
  for (const v of wrapper.value) {
245
239
  if (!elements.includes(v))
@@ -251,14 +245,12 @@ class PRules extends rulesEngine_1.PRulesEngine {
251
245
  return `'${wrapper.label}' no tiene un valor válido`;
252
246
  }
253
247
  });
254
- return this;
255
248
  }
256
249
  isNotIn(...elements) {
257
- this.add(this.isNotIn.name, (wrapper) => {
250
+ return this.add(this.isNotIn.name, (wrapper) => {
258
251
  if (elements.includes(wrapper.value))
259
252
  return `'${wrapper.label}' no tiene un valor válido`;
260
253
  });
261
- return this;
262
254
  }
263
255
  hasElements() {
264
256
  return this.isArray().add(this.hasElements.name, (wrapper) => {
@@ -413,5 +405,15 @@ class PRules extends rulesEngine_1.PRulesEngine {
413
405
  wrapper.value = wrapper.value.split(separator);
414
406
  });
415
407
  }
408
+ floor() {
409
+ return this.isNumber().add(this.floor.name, (wrapper) => {
410
+ wrapper.value = Math.floor(wrapper.value);
411
+ });
412
+ }
413
+ ceil() {
414
+ return this.isNumber().add(this.ceil.name, (wrapper) => {
415
+ wrapper.value = Math.ceil(wrapper.value);
416
+ });
417
+ }
416
418
  }
417
419
  exports.PRules = PRules;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pols-validator",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/rules.ts CHANGED
@@ -193,8 +193,8 @@ export class PRules extends PRulesEngine {
193
193
  })
194
194
  }
195
195
 
196
- isArray() {
197
- return this.add(this.isArray.name, (wrapper: PRulesWrapper) => {
196
+ isArray(rulesGenerator?: (index: number) => PRules) {
197
+ return this.add(this.isArray.name, (wrapper: PRulesWrapper<unknown[]>) => {
198
198
  const message = `'${wrapper.label}' debe ser una lista de elementos`
199
199
  if (typeof wrapper.value == 'string') {
200
200
  try {
@@ -207,23 +207,17 @@ export class PRules extends PRulesEngine {
207
207
  } else {
208
208
  if (!(wrapper.value instanceof Array)) return message
209
209
  }
210
- })
211
- }
212
210
 
213
- isArrayOfObjects(schema?: (index: number) => Record<string, PRules>, prefix?: (index: number) => string) {
214
- return this.isArray().add(this.isArrayOfObjects.name, (wrapper: PRulesWrapper<unknown[]>) => {
211
+ /* Si se ha definido un generador de reglas, se itera cada elemento */
215
212
  const messages: string[] = []
216
- for (const [i, value] of wrapper.value.entries()) {
217
- const v = isObject(this, {
218
- label: prefix?.(i) ?? `Item ${i}`,
219
- value
220
- }, schema?.(i))
221
- if (v) {
222
- if (v instanceof Array) {
223
- messages.push(...v)
224
- } else {
225
- messages.push(v)
226
- }
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
227
221
  }
228
222
  }
229
223
  if (messages.length) return messages
@@ -231,7 +225,7 @@ export class PRules extends PRulesEngine {
231
225
  }
232
226
 
233
227
  isIn(...elements: unknown[]) {
234
- this.add(this.isIn.name, (wrapper: PRulesWrapper) => {
228
+ return this.add(this.isIn.name, (wrapper: PRulesWrapper) => {
235
229
  if (wrapper.value instanceof Array) {
236
230
  for (const v of wrapper.value) {
237
231
  if (!elements.includes(v)) return `'${wrapper.label}' no contiene valores válidos`
@@ -240,14 +234,12 @@ export class PRules extends PRulesEngine {
240
234
  if (!elements.includes(wrapper.value)) return `'${wrapper.label}' no tiene un valor válido`
241
235
  }
242
236
  })
243
- return this
244
237
  }
245
238
 
246
239
  isNotIn(...elements: unknown[]) {
247
- this.add(this.isNotIn.name, (wrapper: PRulesWrapper) => {
240
+ return this.add(this.isNotIn.name, (wrapper: PRulesWrapper) => {
248
241
  if (elements.includes(wrapper.value)) return `'${wrapper.label}' no tiene un valor válido`
249
242
  })
250
- return this
251
243
  }
252
244
 
253
245
  hasElements() {
@@ -405,8 +397,20 @@ export class PRules extends PRulesEngine {
405
397
  }
406
398
 
407
399
  split(separator: string | RegExp) {
408
- return this.isAlphanumeric().add(this.split.name, (wrapper: PRulesWrapper) => {
409
- wrapper.value = (wrapper.value as any).split(separator)
400
+ return this.isAlphanumeric().add(this.split.name, (wrapper: PRulesWrapper<string>) => {
401
+ wrapper.value = wrapper.value.split(separator) as any
402
+ })
403
+ }
404
+
405
+ floor() {
406
+ return this.isNumber().add(this.floor.name, (wrapper: PRulesWrapper<number>) => {
407
+ wrapper.value = Math.floor(wrapper.value)
408
+ })
409
+ }
410
+
411
+ ceil() {
412
+ return this.isNumber().add(this.ceil.name, (wrapper: PRulesWrapper<number>) => {
413
+ wrapper.value = Math.ceil(wrapper.value)
410
414
  })
411
415
  }
412
416
  }
@@ -7,7 +7,10 @@ const original = {
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
16
  const otro = 56
@@ -18,7 +21,10 @@ const resultados = rules({ label: 'Body' }).isObject({
18
21
  tres: rules({ required: true }).isNaturalNoZero(),
19
22
  cuatro: rules({ label: 'Cuatro', default: {} }).isObject({
20
23
  aaa: rules().isDateTime()
21
- })
22
- }).validate(original)
24
+ }),
25
+ cinco: rules().isArray(i => rules({ label: `Elem ${i + 1}` }).isObject({
26
+ ccc: rules().isNumber()
27
+ }))
28
+ }).validate<typeof original>(original)
23
29
 
24
30
  console.log(original, resultados)