@excofy/utils 1.0.0 → 1.0.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/index.cjs CHANGED
@@ -341,6 +341,53 @@ function createValidator() {
341
341
  }
342
342
  return validator;
343
343
  }
344
+ },
345
+ asNumber(message) {
346
+ if (shouldSkipValidation()) return validator;
347
+ const value = current.value;
348
+ const num = typeof value === "number" ? value : Number(value);
349
+ if (Number.isNaN(num)) {
350
+ current.pushError(message);
351
+ } else {
352
+ current.value = num;
353
+ current.inputs[current.field] = num;
354
+ }
355
+ return validator;
356
+ },
357
+ asBoolean(message) {
358
+ if (shouldSkipValidation()) return validator;
359
+ const value = current.value;
360
+ let bool;
361
+ if (typeof value === "boolean") {
362
+ bool = value;
363
+ } else if (typeof value === "string") {
364
+ if (value.toLowerCase() === "true") bool = true;
365
+ else if (value.toLowerCase() === "false") bool = false;
366
+ }
367
+ if (typeof bool !== "boolean") {
368
+ current.pushError(message);
369
+ } else {
370
+ current.value = bool;
371
+ current.inputs[current.field] = bool;
372
+ }
373
+ return validator;
374
+ },
375
+ asStringArray(message) {
376
+ if (shouldSkipValidation()) return validator;
377
+ const value = current.value;
378
+ let arr;
379
+ if (Array.isArray(value)) {
380
+ arr = value.map((v) => String(v).trim());
381
+ } else if (typeof value === "string") {
382
+ arr = value.split(",").map((s) => s.trim()).filter(Boolean);
383
+ }
384
+ if (!arr || arr.some((item) => item === "")) {
385
+ current.pushError(message);
386
+ } else {
387
+ current.value = arr;
388
+ current.inputs[current.field] = arr;
389
+ }
390
+ return validator;
344
391
  }
345
392
  };
346
393
  return {
package/dist/index.d.cts CHANGED
@@ -60,6 +60,9 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
60
60
  videoUrl: {
61
61
  youtube(message: string): /*elided*/ any;
62
62
  };
63
+ asNumber(message: string): /*elided*/ any;
64
+ asBoolean(message: string): /*elided*/ any;
65
+ asStringArray(message: string): /*elided*/ any;
63
66
  };
64
67
  };
65
68
  isNotRequired(): {
@@ -80,6 +83,9 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
80
83
  videoUrl: {
81
84
  youtube(message: string): /*elided*/ any;
82
85
  };
86
+ asNumber(message: string): /*elided*/ any;
87
+ asBoolean(message: string): /*elided*/ any;
88
+ asStringArray(message: string): /*elided*/ any;
83
89
  };
84
90
  };
85
91
  };
package/dist/index.d.ts CHANGED
@@ -60,6 +60,9 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
60
60
  videoUrl: {
61
61
  youtube(message: string): /*elided*/ any;
62
62
  };
63
+ asNumber(message: string): /*elided*/ any;
64
+ asBoolean(message: string): /*elided*/ any;
65
+ asStringArray(message: string): /*elided*/ any;
63
66
  };
64
67
  };
65
68
  isNotRequired(): {
@@ -80,6 +83,9 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
80
83
  videoUrl: {
81
84
  youtube(message: string): /*elided*/ any;
82
85
  };
86
+ asNumber(message: string): /*elided*/ any;
87
+ asBoolean(message: string): /*elided*/ any;
88
+ asStringArray(message: string): /*elided*/ any;
83
89
  };
84
90
  };
85
91
  };
package/dist/index.js CHANGED
@@ -314,6 +314,53 @@ function createValidator() {
314
314
  }
315
315
  return validator;
316
316
  }
317
+ },
318
+ asNumber(message) {
319
+ if (shouldSkipValidation()) return validator;
320
+ const value = current.value;
321
+ const num = typeof value === "number" ? value : Number(value);
322
+ if (Number.isNaN(num)) {
323
+ current.pushError(message);
324
+ } else {
325
+ current.value = num;
326
+ current.inputs[current.field] = num;
327
+ }
328
+ return validator;
329
+ },
330
+ asBoolean(message) {
331
+ if (shouldSkipValidation()) return validator;
332
+ const value = current.value;
333
+ let bool;
334
+ if (typeof value === "boolean") {
335
+ bool = value;
336
+ } else if (typeof value === "string") {
337
+ if (value.toLowerCase() === "true") bool = true;
338
+ else if (value.toLowerCase() === "false") bool = false;
339
+ }
340
+ if (typeof bool !== "boolean") {
341
+ current.pushError(message);
342
+ } else {
343
+ current.value = bool;
344
+ current.inputs[current.field] = bool;
345
+ }
346
+ return validator;
347
+ },
348
+ asStringArray(message) {
349
+ if (shouldSkipValidation()) return validator;
350
+ const value = current.value;
351
+ let arr;
352
+ if (Array.isArray(value)) {
353
+ arr = value.map((v) => String(v).trim());
354
+ } else if (typeof value === "string") {
355
+ arr = value.split(",").map((s) => s.trim()).filter(Boolean);
356
+ }
357
+ if (!arr || arr.some((item) => item === "")) {
358
+ current.pushError(message);
359
+ } else {
360
+ current.value = arr;
361
+ current.inputs[current.field] = arr;
362
+ }
363
+ return validator;
317
364
  }
318
365
  };
319
366
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@excofy/utils",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Biblioteca de utilitários para o Excofy",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -253,7 +253,6 @@ export function createValidator<
253
253
  current.inputs[current.field] = transformed as TRaw[keyof TRaw];
254
254
  return validator;
255
255
  },
256
-
257
256
  slug(message: string) {
258
257
  if (shouldSkipValidation()) {
259
258
  return validator;
@@ -335,6 +334,71 @@ export function createValidator<
335
334
  return validator;
336
335
  },
337
336
  },
337
+
338
+ asNumber(message: string) {
339
+ if (shouldSkipValidation()) return validator;
340
+
341
+ const value = current.value;
342
+ const num = typeof value === 'number' ? value : Number(value);
343
+
344
+ if (Number.isNaN(num)) {
345
+ current.pushError(message);
346
+ } else {
347
+ current.value = num;
348
+ current.inputs[current.field] = num as TRaw[keyof TRaw];
349
+ }
350
+
351
+ return validator;
352
+ },
353
+
354
+ asBoolean(message: string) {
355
+ if (shouldSkipValidation()) return validator;
356
+
357
+ const value = current.value;
358
+
359
+ let bool: boolean | undefined;
360
+ if (typeof value === 'boolean') {
361
+ bool = value;
362
+ } else if (typeof value === 'string') {
363
+ if (value.toLowerCase() === 'true') bool = true;
364
+ else if (value.toLowerCase() === 'false') bool = false;
365
+ }
366
+
367
+ if (typeof bool !== 'boolean') {
368
+ current.pushError(message);
369
+ } else {
370
+ current.value = bool;
371
+ current.inputs[current.field] = bool as TRaw[keyof TRaw];
372
+ }
373
+
374
+ return validator;
375
+ },
376
+
377
+ asStringArray(message: string) {
378
+ if (shouldSkipValidation()) return validator;
379
+
380
+ const value = current.value;
381
+
382
+ let arr: string[] | undefined;
383
+
384
+ if (Array.isArray(value)) {
385
+ arr = value.map((v) => String(v).trim());
386
+ } else if (typeof value === 'string') {
387
+ arr = value
388
+ .split(',')
389
+ .map((s) => s.trim())
390
+ .filter(Boolean);
391
+ }
392
+
393
+ if (!arr || arr.some((item) => item === '')) {
394
+ current.pushError(message);
395
+ } else {
396
+ current.value = arr;
397
+ current.inputs[current.field] = arr as TRaw[keyof TRaw];
398
+ }
399
+
400
+ return validator;
401
+ },
338
402
  };
339
403
 
340
404
  return {