@excofy/utils 1.0.9 → 1.0.10

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;
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;
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;
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@excofy/utils",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
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;
@@ -248,6 +249,53 @@ export function createValidator<
248
249
 
249
250
  return validator;
250
251
  },
252
+ expirationDate: (message: string) => {
253
+ if (shouldSkipValidation()) {
254
+ return validator;
255
+ }
256
+
257
+ if (typeof current.value === 'string') {
258
+ const expirationDate = current.value;
259
+ const regex = /^(\d{2})\/(\d{2}|\d{4})$/;
260
+ const match = expirationDate.match(regex);
261
+ if (!match) {
262
+ current.pushError(message);
263
+
264
+ return validator;
265
+ }
266
+
267
+ const month = Number.parseInt(match[1], 10);
268
+ let year = Number.parseInt(match[2], 10);
269
+
270
+ if (month < 1 || month > 12) {
271
+ current.pushError(message);
272
+ return validator;
273
+ }
274
+
275
+ const now = new Date();
276
+ const currentYear = now.getFullYear();
277
+ const currentMonth = now.getMonth() + 1;
278
+
279
+ // Se vier YY, transforme para YYYY
280
+ if (year < 100) {
281
+ const century = Math.floor(currentYear / 100) * 100;
282
+ year += century;
283
+ if (year < currentYear) year += 100; // Evita voltar para o passado
284
+ }
285
+
286
+ if (
287
+ year < currentYear ||
288
+ (year === currentYear && month < currentMonth)
289
+ ) {
290
+ current.pushError(message);
291
+ return validator;
292
+ }
293
+ } else {
294
+ current.pushError(message);
295
+ }
296
+
297
+ return validator;
298
+ },
251
299
  fileMaxSize: (maxSize: number, message: string) => {
252
300
  if (shouldSkipValidation()) {
253
301
  return validator;