@excofy/utils 1.0.9 → 1.0.11

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
@@ -311,6 +311,41 @@ function createValidator() {
311
311
  }
312
312
  return validator;
313
313
  },
314
+ expirationDate: (message) => {
315
+ if (shouldSkipValidation()) {
316
+ return validator;
317
+ }
318
+ if (typeof current.value === "string") {
319
+ const expirationDate = current.value;
320
+ const regex = /^(\d{2})\/(\d{2}|\d{4})$/;
321
+ const match = expirationDate.match(regex);
322
+ if (!match) {
323
+ current.pushError(message);
324
+ return validator;
325
+ }
326
+ const month = Number.parseInt(match[1], 10);
327
+ let year = Number.parseInt(match[2], 10);
328
+ if (month < 1 || month > 12) {
329
+ current.pushError(message);
330
+ return validator;
331
+ }
332
+ const now = /* @__PURE__ */ new Date();
333
+ const currentYear = now.getFullYear();
334
+ const currentMonth = now.getMonth() + 1;
335
+ if (year < 100) {
336
+ const century = Math.floor(currentYear / 100) * 100;
337
+ year += century;
338
+ if (year < currentYear) year += 100;
339
+ }
340
+ if (year < currentYear || year === currentYear && month < currentMonth) {
341
+ current.pushError(message);
342
+ return validator;
343
+ }
344
+ } else {
345
+ current.pushError(message);
346
+ }
347
+ return validator;
348
+ },
314
349
  fileMaxSize: (maxSize, message) => {
315
350
  if (shouldSkipValidation()) {
316
351
  return validator;
@@ -400,6 +435,15 @@ function createValidator() {
400
435
  }
401
436
  return validator;
402
437
  },
438
+ sanitizeDigits() {
439
+ if (shouldSkipValidation()) return validator;
440
+ if (typeof current.value === "string") {
441
+ const digitsOnly = current.value.replace(/\D+/g, "");
442
+ current.value = digitsOnly;
443
+ current.inputs[current.field] = digitsOnly;
444
+ }
445
+ return validator;
446
+ },
403
447
  sanitizePreservingNewlines() {
404
448
  if (shouldSkipValidation()) {
405
449
  return validator;
package/dist/index.d.cts CHANGED
@@ -22,6 +22,7 @@ interface ValidatorField {
22
22
  cpf(message: string): ValidatorField;
23
23
  each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): ValidatorField;
24
24
  email(message: string): ValidatorField;
25
+ expirationDate(message: string): ValidatorField;
25
26
  fileMaxSize(maxSize: number, message: string): ValidatorField;
26
27
  fileType(validTypes: string[], message: string): ValidatorField;
27
28
  length(length: number, message: string): ValidatorField;
@@ -30,6 +31,7 @@ interface ValidatorField {
30
31
  transform<U>(fn: (value: TInputValue) => U): ValidatorField;
31
32
  slug(message: string): ValidatorField;
32
33
  sanitize(): ValidatorField;
34
+ sanitizeDigits(): ValidatorField;
33
35
  sanitizePreservingNewlines(): ValidatorField;
34
36
  sanitizeHTML(tags?: AllowedTag[]): ValidatorField;
35
37
  url(message: string): ValidatorField;
package/dist/index.d.ts CHANGED
@@ -22,6 +22,7 @@ interface ValidatorField {
22
22
  cpf(message: string): ValidatorField;
23
23
  each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): ValidatorField;
24
24
  email(message: string): ValidatorField;
25
+ expirationDate(message: string): ValidatorField;
25
26
  fileMaxSize(maxSize: number, message: string): ValidatorField;
26
27
  fileType(validTypes: string[], message: string): ValidatorField;
27
28
  length(length: number, message: string): ValidatorField;
@@ -30,6 +31,7 @@ interface ValidatorField {
30
31
  transform<U>(fn: (value: TInputValue) => U): ValidatorField;
31
32
  slug(message: string): ValidatorField;
32
33
  sanitize(): ValidatorField;
34
+ sanitizeDigits(): ValidatorField;
33
35
  sanitizePreservingNewlines(): ValidatorField;
34
36
  sanitizeHTML(tags?: AllowedTag[]): ValidatorField;
35
37
  url(message: string): ValidatorField;
package/dist/index.js CHANGED
@@ -277,6 +277,41 @@ function createValidator() {
277
277
  }
278
278
  return validator;
279
279
  },
280
+ expirationDate: (message) => {
281
+ if (shouldSkipValidation()) {
282
+ return validator;
283
+ }
284
+ if (typeof current.value === "string") {
285
+ const expirationDate = current.value;
286
+ const regex = /^(\d{2})\/(\d{2}|\d{4})$/;
287
+ const match = expirationDate.match(regex);
288
+ if (!match) {
289
+ current.pushError(message);
290
+ return validator;
291
+ }
292
+ const month = Number.parseInt(match[1], 10);
293
+ let year = Number.parseInt(match[2], 10);
294
+ if (month < 1 || month > 12) {
295
+ current.pushError(message);
296
+ return validator;
297
+ }
298
+ const now = /* @__PURE__ */ new Date();
299
+ const currentYear = now.getFullYear();
300
+ const currentMonth = now.getMonth() + 1;
301
+ if (year < 100) {
302
+ const century = Math.floor(currentYear / 100) * 100;
303
+ year += century;
304
+ if (year < currentYear) year += 100;
305
+ }
306
+ if (year < currentYear || year === currentYear && month < currentMonth) {
307
+ current.pushError(message);
308
+ return validator;
309
+ }
310
+ } else {
311
+ current.pushError(message);
312
+ }
313
+ return validator;
314
+ },
280
315
  fileMaxSize: (maxSize, message) => {
281
316
  if (shouldSkipValidation()) {
282
317
  return validator;
@@ -366,6 +401,15 @@ function createValidator() {
366
401
  }
367
402
  return validator;
368
403
  },
404
+ sanitizeDigits() {
405
+ if (shouldSkipValidation()) return validator;
406
+ if (typeof current.value === "string") {
407
+ const digitsOnly = current.value.replace(/\D+/g, "");
408
+ current.value = digitsOnly;
409
+ current.inputs[current.field] = digitsOnly;
410
+ }
411
+ return validator;
412
+ },
369
413
  sanitizePreservingNewlines() {
370
414
  if (shouldSkipValidation()) {
371
415
  return validator;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@excofy/utils",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "description": "Biblioteca de utilitários para o Excofy",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -29,6 +29,7 @@ interface ValidatorField {
29
29
  ) => void
30
30
  ): ValidatorField;
31
31
  email(message: string): ValidatorField;
32
+ expirationDate(message: string): ValidatorField;
32
33
  fileMaxSize(maxSize: number, message: string): ValidatorField;
33
34
  fileType(validTypes: string[], message: string): ValidatorField;
34
35
  length(length: number, message: string): ValidatorField;
@@ -37,6 +38,7 @@ interface ValidatorField {
37
38
  transform<U>(fn: (value: TInputValue) => U): ValidatorField;
38
39
  slug(message: string): ValidatorField;
39
40
  sanitize(): ValidatorField;
41
+ sanitizeDigits(): ValidatorField;
40
42
  sanitizePreservingNewlines(): ValidatorField;
41
43
  sanitizeHTML(tags?: AllowedTag[]): ValidatorField;
42
44
  url(message: string): ValidatorField;
@@ -248,6 +250,53 @@ export function createValidator<
248
250
 
249
251
  return validator;
250
252
  },
253
+ expirationDate: (message: string) => {
254
+ if (shouldSkipValidation()) {
255
+ return validator;
256
+ }
257
+
258
+ if (typeof current.value === 'string') {
259
+ const expirationDate = current.value;
260
+ const regex = /^(\d{2})\/(\d{2}|\d{4})$/;
261
+ const match = expirationDate.match(regex);
262
+ if (!match) {
263
+ current.pushError(message);
264
+
265
+ return validator;
266
+ }
267
+
268
+ const month = Number.parseInt(match[1], 10);
269
+ let year = Number.parseInt(match[2], 10);
270
+
271
+ if (month < 1 || month > 12) {
272
+ current.pushError(message);
273
+ return validator;
274
+ }
275
+
276
+ const now = new Date();
277
+ const currentYear = now.getFullYear();
278
+ const currentMonth = now.getMonth() + 1;
279
+
280
+ // Se vier YY, transforme para YYYY
281
+ if (year < 100) {
282
+ const century = Math.floor(currentYear / 100) * 100;
283
+ year += century;
284
+ if (year < currentYear) year += 100; // Evita voltar para o passado
285
+ }
286
+
287
+ if (
288
+ year < currentYear ||
289
+ (year === currentYear && month < currentMonth)
290
+ ) {
291
+ current.pushError(message);
292
+ return validator;
293
+ }
294
+ } else {
295
+ current.pushError(message);
296
+ }
297
+
298
+ return validator;
299
+ },
251
300
  fileMaxSize: (maxSize: number, message: string) => {
252
301
  if (shouldSkipValidation()) {
253
302
  return validator;
@@ -359,6 +408,18 @@ export function createValidator<
359
408
  return validator;
360
409
  },
361
410
 
411
+ sanitizeDigits() {
412
+ if (shouldSkipValidation()) return validator;
413
+
414
+ if (typeof current.value === 'string') {
415
+ const digitsOnly = current.value.replace(/\D+/g, '');
416
+ current.value = digitsOnly;
417
+ current.inputs[current.field] = digitsOnly as TRaw[keyof TRaw];
418
+ }
419
+
420
+ return validator;
421
+ },
422
+
362
423
  sanitizePreservingNewlines() {
363
424
  if (shouldSkipValidation()) {
364
425
  return validator;