@excofy/utils 2.2.0 → 2.3.0
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 +36 -40
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +36 -40
- package/package.json +1 -1
- package/src/helpers/validator.ts +51 -23
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 = [
|
|
@@ -276,6 +252,42 @@ function createValidator() {
|
|
|
276
252
|
return !current.required && (current.value === void 0 || current.value === null || current.value === "");
|
|
277
253
|
};
|
|
278
254
|
const validator = {
|
|
255
|
+
base64Type(validTypes, message) {
|
|
256
|
+
if (shouldSkipValidation()) return validator;
|
|
257
|
+
const value = current.value;
|
|
258
|
+
if (typeof value !== "string") {
|
|
259
|
+
current.pushError(message);
|
|
260
|
+
return validator;
|
|
261
|
+
}
|
|
262
|
+
const match = value.match(/^data:([A-Za-z-+\/]+);base64,/);
|
|
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
|
@@ -18,6 +18,8 @@ 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
|
@@ -18,6 +18,8 @@ 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 = [
|
|
@@ -238,6 +214,42 @@ function createValidator() {
|
|
|
238
214
|
return !current.required && (current.value === void 0 || current.value === null || current.value === "");
|
|
239
215
|
};
|
|
240
216
|
const validator = {
|
|
217
|
+
base64Type(validTypes, message) {
|
|
218
|
+
if (shouldSkipValidation()) return validator;
|
|
219
|
+
const value = current.value;
|
|
220
|
+
if (typeof value !== "string") {
|
|
221
|
+
current.pushError(message);
|
|
222
|
+
return validator;
|
|
223
|
+
}
|
|
224
|
+
const match = value.match(/^data:([A-Za-z-+\/]+);base64,/);
|
|
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
package/src/helpers/validator.ts
CHANGED
|
@@ -21,6 +21,8 @@ interface IInputErrors {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
interface ValidatorField {
|
|
24
|
+
base64Type(validTypes: string[], message: string): ValidatorField;
|
|
25
|
+
base64MaxSize(maxSize: number, message: string): ValidatorField;
|
|
24
26
|
cnpj(message: string): ValidatorField;
|
|
25
27
|
cpf(message: string): ValidatorField;
|
|
26
28
|
each(
|
|
@@ -58,7 +60,6 @@ interface ValidatorField {
|
|
|
58
60
|
asNumber(message: string): ValidatorField;
|
|
59
61
|
asBoolean(message: string): ValidatorField;
|
|
60
62
|
asStringArray(message: string): ValidatorField;
|
|
61
|
-
asFileFromBase64(message: string, filename: string): ValidatorField;
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
/**
|
|
@@ -176,6 +177,55 @@ export function createValidator<
|
|
|
176
177
|
};
|
|
177
178
|
|
|
178
179
|
const validator: ValidatorField = {
|
|
180
|
+
base64Type(validTypes: string[], message: string) {
|
|
181
|
+
if (shouldSkipValidation()) return validator;
|
|
182
|
+
|
|
183
|
+
const value = current.value;
|
|
184
|
+
|
|
185
|
+
if (typeof value !== 'string') {
|
|
186
|
+
current.pushError(message);
|
|
187
|
+
return validator;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const match = value.match(/^data:([A-Za-z-+\/]+);base64,/);
|
|
191
|
+
if (!match) {
|
|
192
|
+
current.pushError(message);
|
|
193
|
+
return validator;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const mimeType = match[1];
|
|
197
|
+
if (!validTypes.includes(mimeType)) {
|
|
198
|
+
current.pushError(message);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return validator;
|
|
202
|
+
},
|
|
203
|
+
|
|
204
|
+
base64MaxSize(maxSize: number, message: string) {
|
|
205
|
+
if (shouldSkipValidation()) return validator;
|
|
206
|
+
|
|
207
|
+
const value = current.value;
|
|
208
|
+
|
|
209
|
+
if (typeof value !== 'string') {
|
|
210
|
+
current.pushError(message);
|
|
211
|
+
return validator;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const base64Content = value.split(',')[1];
|
|
215
|
+
if (!base64Content) {
|
|
216
|
+
current.pushError(message);
|
|
217
|
+
return validator;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Calcula o tamanho do arquivo em bytes
|
|
221
|
+
const sizeInBytes = Math.ceil((base64Content.length * 3) / 4);
|
|
222
|
+
if (sizeInBytes > maxSize) {
|
|
223
|
+
current.pushError(message);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return validator;
|
|
227
|
+
},
|
|
228
|
+
|
|
179
229
|
cnpj: (message: string) => {
|
|
180
230
|
if (shouldSkipValidation()) {
|
|
181
231
|
return validator;
|
|
@@ -636,28 +686,6 @@ export function createValidator<
|
|
|
636
686
|
|
|
637
687
|
return validator;
|
|
638
688
|
},
|
|
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
689
|
};
|
|
662
690
|
|
|
663
691
|
return {
|