@excofy/utils 2.2.0 → 2.3.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
@@ -96,30 +96,6 @@ function isValidCNPJ(cnpj) {
96
96
  return digit1 === digits[1];
97
97
  }
98
98
 
99
- // src/helpers/file.ts
100
- var fileUtils = {
101
- base64ToFile: (base64, filename) => {
102
- if (!base64 || base64 === "") {
103
- return null;
104
- }
105
- const arr = base64.split(",");
106
- if (arr.length !== 2) {
107
- return null;
108
- }
109
- const mime = arr[0].match(/:(.*?);/)?.[1];
110
- if (!mime) {
111
- return null;
112
- }
113
- const bstr = atob(arr[1]);
114
- let n = bstr.length;
115
- const u8arr = new Uint8Array(n);
116
- while (n--) {
117
- u8arr[n] = bstr.charCodeAt(n);
118
- }
119
- return new File([u8arr], filename, { type: mime });
120
- }
121
- };
122
-
123
99
  // src/helpers/sanitize.ts
124
100
  var import_xss = require("xss");
125
101
  var allTags = [
@@ -258,8 +234,6 @@ function createValidator() {
258
234
  isValidType = typeof value === "object" && value !== null && !Array.isArray(value);
259
235
  } else if (type === "file") {
260
236
  isValidType = value instanceof File;
261
- } else if (type === "base64") {
262
- isValidType = typeof value === "string" && !!value.match(/^data:([A-Za-z-+\/]+);base64,([A-Za-z0-9+/=]+)$/);
263
237
  } else if (type === "date") {
264
238
  isValidType = typeof value === "string" && !Number.isNaN(new Date(value).getTime());
265
239
  } else {
@@ -276,6 +250,44 @@ function createValidator() {
276
250
  return !current.required && (current.value === void 0 || current.value === null || current.value === "");
277
251
  };
278
252
  const validator = {
253
+ base64Type(validTypes, message) {
254
+ if (shouldSkipValidation()) return validator;
255
+ const value = current.value;
256
+ if (typeof value !== "string") {
257
+ current.pushError(message);
258
+ return validator;
259
+ }
260
+ const match = value.match(
261
+ /^data:([A-Za-z-+\/]+);(?:charset=utf-8;)?base64,/
262
+ );
263
+ if (!match) {
264
+ current.pushError(message);
265
+ return validator;
266
+ }
267
+ const mimeType = match[1];
268
+ if (!validTypes.includes(mimeType)) {
269
+ current.pushError(message);
270
+ }
271
+ return validator;
272
+ },
273
+ base64MaxSize(maxSize, message) {
274
+ if (shouldSkipValidation()) return validator;
275
+ const value = current.value;
276
+ if (typeof value !== "string") {
277
+ current.pushError(message);
278
+ return validator;
279
+ }
280
+ const base64Content = value.split(",")[1];
281
+ if (!base64Content) {
282
+ current.pushError(message);
283
+ return validator;
284
+ }
285
+ const sizeInBytes = Math.ceil(base64Content.length * 3 / 4);
286
+ if (sizeInBytes > maxSize) {
287
+ current.pushError(message);
288
+ }
289
+ return validator;
290
+ },
279
291
  cnpj: (message) => {
280
292
  if (shouldSkipValidation()) {
281
293
  return validator;
@@ -618,22 +630,6 @@ function createValidator() {
618
630
  current.inputs[current.field] = arr;
619
631
  }
620
632
  return validator;
621
- },
622
- asFileFromBase64(message, filename) {
623
- if (shouldSkipValidation()) return validator;
624
- const value = current.value;
625
- if (typeof value !== "string") {
626
- current.pushError(message);
627
- return validator;
628
- }
629
- if (!value.match(/^data:([A-Za-z-+\/]+);base64,([A-Za-z0-9+/=]+)$/)) {
630
- current.pushError(message);
631
- return validator;
632
- }
633
- const file = fileUtils.base64ToFile(value, filename);
634
- current.value = file;
635
- current.inputs[current.field] = file;
636
- return validator;
637
633
  }
638
634
  };
639
635
  return {
package/dist/index.d.cts CHANGED
@@ -13,11 +13,13 @@ type TMessage = {
13
13
  };
14
14
  type TValue = string | number | boolean | File | object | null | undefined;
15
15
  type TInputValue = TValue;
16
- type TTypes = 'string' | 'number' | 'boolean' | 'object' | 'array' | 'file' | 'base64' | 'date';
16
+ type TTypes = 'string' | 'number' | 'boolean' | 'object' | 'array' | 'file' | 'date';
17
17
  interface IInputErrors {
18
18
  [key: string]: TMessage[] | IInputErrors[];
19
19
  }
20
20
  interface ValidatorField {
21
+ base64Type(validTypes: string[], message: string): ValidatorField;
22
+ base64MaxSize(maxSize: number, message: string): ValidatorField;
21
23
  cnpj(message: string): ValidatorField;
22
24
  cpf(message: string): ValidatorField;
23
25
  each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): ValidatorField;
@@ -48,7 +50,6 @@ interface ValidatorField {
48
50
  asNumber(message: string): ValidatorField;
49
51
  asBoolean(message: string): ValidatorField;
50
52
  asStringArray(message: string): ValidatorField;
51
- asFileFromBase64(message: string, filename: string): ValidatorField;
52
53
  }
53
54
  /**
54
55
  * Cria um validador de inputs com suporte a validações encadeadas, transformação e sanitização.
package/dist/index.d.ts CHANGED
@@ -13,11 +13,13 @@ type TMessage = {
13
13
  };
14
14
  type TValue = string | number | boolean | File | object | null | undefined;
15
15
  type TInputValue = TValue;
16
- type TTypes = 'string' | 'number' | 'boolean' | 'object' | 'array' | 'file' | 'base64' | 'date';
16
+ type TTypes = 'string' | 'number' | 'boolean' | 'object' | 'array' | 'file' | 'date';
17
17
  interface IInputErrors {
18
18
  [key: string]: TMessage[] | IInputErrors[];
19
19
  }
20
20
  interface ValidatorField {
21
+ base64Type(validTypes: string[], message: string): ValidatorField;
22
+ base64MaxSize(maxSize: number, message: string): ValidatorField;
21
23
  cnpj(message: string): ValidatorField;
22
24
  cpf(message: string): ValidatorField;
23
25
  each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): ValidatorField;
@@ -48,7 +50,6 @@ interface ValidatorField {
48
50
  asNumber(message: string): ValidatorField;
49
51
  asBoolean(message: string): ValidatorField;
50
52
  asStringArray(message: string): ValidatorField;
51
- asFileFromBase64(message: string, filename: string): ValidatorField;
52
53
  }
53
54
  /**
54
55
  * Cria um validador de inputs com suporte a validações encadeadas, transformação e sanitização.
package/dist/index.js CHANGED
@@ -58,30 +58,6 @@ function isValidCNPJ(cnpj) {
58
58
  return digit1 === digits[1];
59
59
  }
60
60
 
61
- // src/helpers/file.ts
62
- var fileUtils = {
63
- base64ToFile: (base64, filename) => {
64
- if (!base64 || base64 === "") {
65
- return null;
66
- }
67
- const arr = base64.split(",");
68
- if (arr.length !== 2) {
69
- return null;
70
- }
71
- const mime = arr[0].match(/:(.*?);/)?.[1];
72
- if (!mime) {
73
- return null;
74
- }
75
- const bstr = atob(arr[1]);
76
- let n = bstr.length;
77
- const u8arr = new Uint8Array(n);
78
- while (n--) {
79
- u8arr[n] = bstr.charCodeAt(n);
80
- }
81
- return new File([u8arr], filename, { type: mime });
82
- }
83
- };
84
-
85
61
  // src/helpers/sanitize.ts
86
62
  import { FilterXSS } from "xss";
87
63
  var allTags = [
@@ -220,8 +196,6 @@ function createValidator() {
220
196
  isValidType = typeof value === "object" && value !== null && !Array.isArray(value);
221
197
  } else if (type === "file") {
222
198
  isValidType = value instanceof File;
223
- } else if (type === "base64") {
224
- isValidType = typeof value === "string" && !!value.match(/^data:([A-Za-z-+\/]+);base64,([A-Za-z0-9+/=]+)$/);
225
199
  } else if (type === "date") {
226
200
  isValidType = typeof value === "string" && !Number.isNaN(new Date(value).getTime());
227
201
  } else {
@@ -238,6 +212,44 @@ function createValidator() {
238
212
  return !current.required && (current.value === void 0 || current.value === null || current.value === "");
239
213
  };
240
214
  const validator = {
215
+ base64Type(validTypes, message) {
216
+ if (shouldSkipValidation()) return validator;
217
+ const value = current.value;
218
+ if (typeof value !== "string") {
219
+ current.pushError(message);
220
+ return validator;
221
+ }
222
+ const match = value.match(
223
+ /^data:([A-Za-z-+\/]+);(?:charset=utf-8;)?base64,/
224
+ );
225
+ if (!match) {
226
+ current.pushError(message);
227
+ return validator;
228
+ }
229
+ const mimeType = match[1];
230
+ if (!validTypes.includes(mimeType)) {
231
+ current.pushError(message);
232
+ }
233
+ return validator;
234
+ },
235
+ base64MaxSize(maxSize, message) {
236
+ if (shouldSkipValidation()) return validator;
237
+ const value = current.value;
238
+ if (typeof value !== "string") {
239
+ current.pushError(message);
240
+ return validator;
241
+ }
242
+ const base64Content = value.split(",")[1];
243
+ if (!base64Content) {
244
+ current.pushError(message);
245
+ return validator;
246
+ }
247
+ const sizeInBytes = Math.ceil(base64Content.length * 3 / 4);
248
+ if (sizeInBytes > maxSize) {
249
+ current.pushError(message);
250
+ }
251
+ return validator;
252
+ },
241
253
  cnpj: (message) => {
242
254
  if (shouldSkipValidation()) {
243
255
  return validator;
@@ -580,22 +592,6 @@ function createValidator() {
580
592
  current.inputs[current.field] = arr;
581
593
  }
582
594
  return validator;
583
- },
584
- asFileFromBase64(message, filename) {
585
- if (shouldSkipValidation()) return validator;
586
- const value = current.value;
587
- if (typeof value !== "string") {
588
- current.pushError(message);
589
- return validator;
590
- }
591
- if (!value.match(/^data:([A-Za-z-+\/]+);base64,([A-Za-z0-9+/=]+)$/)) {
592
- current.pushError(message);
593
- return validator;
594
- }
595
- const file = fileUtils.base64ToFile(value, filename);
596
- current.value = file;
597
- current.inputs[current.field] = file;
598
- return validator;
599
595
  }
600
596
  };
601
597
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@excofy/utils",
3
- "version": "2.2.0",
3
+ "version": "2.3.1",
4
4
  "description": "Biblioteca de utilitários para o Excofy",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -1,5 +1,4 @@
1
1
  import { isValidCNPJ, isValidCPF } from './document';
2
- import { fileUtils } from './file';
3
2
  import { sanitizeValue, type AllowedTag } from './sanitize';
4
3
  import { video } from './video';
5
4
 
@@ -13,7 +12,6 @@ type TTypes =
13
12
  | 'object'
14
13
  | 'array'
15
14
  | 'file'
16
- | 'base64'
17
15
  | 'date';
18
16
 
19
17
  interface IInputErrors {
@@ -21,6 +19,8 @@ interface IInputErrors {
21
19
  }
22
20
 
23
21
  interface ValidatorField {
22
+ base64Type(validTypes: string[], message: string): ValidatorField;
23
+ base64MaxSize(maxSize: number, message: string): ValidatorField;
24
24
  cnpj(message: string): ValidatorField;
25
25
  cpf(message: string): ValidatorField;
26
26
  each(
@@ -58,7 +58,6 @@ interface ValidatorField {
58
58
  asNumber(message: string): ValidatorField;
59
59
  asBoolean(message: string): ValidatorField;
60
60
  asStringArray(message: string): ValidatorField;
61
- asFileFromBase64(message: string, filename: string): ValidatorField;
62
61
  }
63
62
 
64
63
  /**
@@ -142,10 +141,6 @@ export function createValidator<
142
141
  typeof value === 'object' && value !== null && !Array.isArray(value);
143
142
  } else if (type === 'file') {
144
143
  isValidType = value instanceof File;
145
- } else if (type === 'base64') {
146
- isValidType =
147
- typeof value === 'string' &&
148
- !!value.match(/^data:([A-Za-z-+\/]+);base64,([A-Za-z0-9+/=]+)$/);
149
144
  } else if (type === 'date') {
150
145
  isValidType =
151
146
  typeof value === 'string' && !Number.isNaN(new Date(value).getTime());
@@ -176,6 +171,57 @@ export function createValidator<
176
171
  };
177
172
 
178
173
  const validator: ValidatorField = {
174
+ base64Type(validTypes: string[], message: string) {
175
+ if (shouldSkipValidation()) return validator;
176
+
177
+ const value = current.value;
178
+
179
+ if (typeof value !== 'string') {
180
+ current.pushError(message);
181
+ return validator;
182
+ }
183
+
184
+ const match = value.match(
185
+ /^data:([A-Za-z-+\/]+);(?:charset=utf-8;)?base64,/
186
+ );
187
+ if (!match) {
188
+ current.pushError(message);
189
+ return validator;
190
+ }
191
+
192
+ const mimeType = match[1];
193
+ if (!validTypes.includes(mimeType)) {
194
+ current.pushError(message);
195
+ }
196
+
197
+ return validator;
198
+ },
199
+
200
+ base64MaxSize(maxSize: number, message: string) {
201
+ if (shouldSkipValidation()) return validator;
202
+
203
+ const value = current.value;
204
+
205
+ if (typeof value !== 'string') {
206
+ current.pushError(message);
207
+ return validator;
208
+ }
209
+
210
+ const base64Content = value.split(',')[1];
211
+ if (!base64Content) {
212
+ current.pushError(message);
213
+ return validator;
214
+ }
215
+
216
+ // Calcula o tamanho do arquivo em bytes
217
+ const sizeInBytes = Math.ceil((base64Content.length * 3) / 4);
218
+ if (sizeInBytes > maxSize) {
219
+ current.pushError(message);
220
+ }
221
+
222
+ return validator;
223
+ },
224
+
179
225
  cnpj: (message: string) => {
180
226
  if (shouldSkipValidation()) {
181
227
  return validator;
@@ -636,28 +682,6 @@ export function createValidator<
636
682
 
637
683
  return validator;
638
684
  },
639
-
640
- asFileFromBase64(message: string, filename: string) {
641
- if (shouldSkipValidation()) return validator;
642
-
643
- const value = current.value;
644
-
645
- if (typeof value !== 'string') {
646
- current.pushError(message);
647
- return validator;
648
- }
649
-
650
- if (!value.match(/^data:([A-Za-z-+\/]+);base64,([A-Za-z0-9+/=]+)$/)) {
651
- current.pushError(message);
652
- return validator;
653
- }
654
-
655
- const file = fileUtils.base64ToFile(value, filename);
656
- current.value = file;
657
- current.inputs[current.field] = file as TRaw[keyof TRaw];
658
-
659
- return validator;
660
- },
661
685
  };
662
686
 
663
687
  return {