@excofy/utils 1.0.4 → 1.0.6
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 +23 -0
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +23 -0
- package/package.json +1 -1
- package/src/helpers/validator.ts +44 -0
package/dist/index.cjs
CHANGED
|
@@ -313,6 +313,19 @@ function createValidator() {
|
|
|
313
313
|
}
|
|
314
314
|
return validator;
|
|
315
315
|
},
|
|
316
|
+
sanitizePreservingNewlines() {
|
|
317
|
+
if (shouldSkipValidation()) {
|
|
318
|
+
return validator;
|
|
319
|
+
}
|
|
320
|
+
if (typeof current.value === "string") {
|
|
321
|
+
const withBrs = current.value.replace(/\n/g, "<br>");
|
|
322
|
+
const sanitized = sanitizeValue(withBrs, ["br"]);
|
|
323
|
+
const cleaned = sanitized.trim().replace(/\s{2,}/g, " ").replace(/<br>/g, "\n");
|
|
324
|
+
current.value = cleaned;
|
|
325
|
+
current.inputs[current.field] = cleaned;
|
|
326
|
+
}
|
|
327
|
+
return validator;
|
|
328
|
+
},
|
|
316
329
|
sanitizeHTML(tags) {
|
|
317
330
|
if (shouldSkipValidation()) {
|
|
318
331
|
return validator;
|
|
@@ -359,6 +372,16 @@ function createValidator() {
|
|
|
359
372
|
current.pushError(message);
|
|
360
373
|
return validator;
|
|
361
374
|
},
|
|
375
|
+
test(validateFn, message) {
|
|
376
|
+
if (shouldSkipValidation()) return validator;
|
|
377
|
+
const isValid = validateFn(current.value, {
|
|
378
|
+
field: String(current.field)
|
|
379
|
+
});
|
|
380
|
+
if (!isValid) {
|
|
381
|
+
current.pushError(message);
|
|
382
|
+
}
|
|
383
|
+
return validator;
|
|
384
|
+
},
|
|
362
385
|
videoUrl: {
|
|
363
386
|
youtube(message) {
|
|
364
387
|
if (shouldSkipValidation()) {
|
package/dist/index.d.cts
CHANGED
|
@@ -54,10 +54,14 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
|
|
|
54
54
|
transform<U>(fn: (value: TInputValue) => U): /*elided*/ any;
|
|
55
55
|
slug(message: string): /*elided*/ any;
|
|
56
56
|
sanitize(): /*elided*/ any;
|
|
57
|
+
sanitizePreservingNewlines(): /*elided*/ any;
|
|
57
58
|
sanitizeHTML(tags?: AllowedTag[]): /*elided*/ any;
|
|
58
59
|
url(message: string): /*elided*/ any;
|
|
59
60
|
uuid(message: string): /*elided*/ any;
|
|
60
61
|
oneOf: (types: string[], message: string) => /*elided*/ any;
|
|
62
|
+
test(validateFn: (value: TInputValue, context: {
|
|
63
|
+
field: string;
|
|
64
|
+
}) => boolean, message: string): /*elided*/ any;
|
|
61
65
|
videoUrl: {
|
|
62
66
|
youtube(message: string): /*elided*/ any;
|
|
63
67
|
};
|
|
@@ -78,10 +82,14 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
|
|
|
78
82
|
transform<U>(fn: (value: TInputValue) => U): /*elided*/ any;
|
|
79
83
|
slug(message: string): /*elided*/ any;
|
|
80
84
|
sanitize(): /*elided*/ any;
|
|
85
|
+
sanitizePreservingNewlines(): /*elided*/ any;
|
|
81
86
|
sanitizeHTML(tags?: AllowedTag[]): /*elided*/ any;
|
|
82
87
|
url(message: string): /*elided*/ any;
|
|
83
88
|
uuid(message: string): /*elided*/ any;
|
|
84
89
|
oneOf: (types: string[], message: string) => /*elided*/ any;
|
|
90
|
+
test(validateFn: (value: TInputValue, context: {
|
|
91
|
+
field: string;
|
|
92
|
+
}) => boolean, message: string): /*elided*/ any;
|
|
85
93
|
videoUrl: {
|
|
86
94
|
youtube(message: string): /*elided*/ any;
|
|
87
95
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -54,10 +54,14 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
|
|
|
54
54
|
transform<U>(fn: (value: TInputValue) => U): /*elided*/ any;
|
|
55
55
|
slug(message: string): /*elided*/ any;
|
|
56
56
|
sanitize(): /*elided*/ any;
|
|
57
|
+
sanitizePreservingNewlines(): /*elided*/ any;
|
|
57
58
|
sanitizeHTML(tags?: AllowedTag[]): /*elided*/ any;
|
|
58
59
|
url(message: string): /*elided*/ any;
|
|
59
60
|
uuid(message: string): /*elided*/ any;
|
|
60
61
|
oneOf: (types: string[], message: string) => /*elided*/ any;
|
|
62
|
+
test(validateFn: (value: TInputValue, context: {
|
|
63
|
+
field: string;
|
|
64
|
+
}) => boolean, message: string): /*elided*/ any;
|
|
61
65
|
videoUrl: {
|
|
62
66
|
youtube(message: string): /*elided*/ any;
|
|
63
67
|
};
|
|
@@ -78,10 +82,14 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
|
|
|
78
82
|
transform<U>(fn: (value: TInputValue) => U): /*elided*/ any;
|
|
79
83
|
slug(message: string): /*elided*/ any;
|
|
80
84
|
sanitize(): /*elided*/ any;
|
|
85
|
+
sanitizePreservingNewlines(): /*elided*/ any;
|
|
81
86
|
sanitizeHTML(tags?: AllowedTag[]): /*elided*/ any;
|
|
82
87
|
url(message: string): /*elided*/ any;
|
|
83
88
|
uuid(message: string): /*elided*/ any;
|
|
84
89
|
oneOf: (types: string[], message: string) => /*elided*/ any;
|
|
90
|
+
test(validateFn: (value: TInputValue, context: {
|
|
91
|
+
field: string;
|
|
92
|
+
}) => boolean, message: string): /*elided*/ any;
|
|
85
93
|
videoUrl: {
|
|
86
94
|
youtube(message: string): /*elided*/ any;
|
|
87
95
|
};
|
package/dist/index.js
CHANGED
|
@@ -279,6 +279,19 @@ function createValidator() {
|
|
|
279
279
|
}
|
|
280
280
|
return validator;
|
|
281
281
|
},
|
|
282
|
+
sanitizePreservingNewlines() {
|
|
283
|
+
if (shouldSkipValidation()) {
|
|
284
|
+
return validator;
|
|
285
|
+
}
|
|
286
|
+
if (typeof current.value === "string") {
|
|
287
|
+
const withBrs = current.value.replace(/\n/g, "<br>");
|
|
288
|
+
const sanitized = sanitizeValue(withBrs, ["br"]);
|
|
289
|
+
const cleaned = sanitized.trim().replace(/\s{2,}/g, " ").replace(/<br>/g, "\n");
|
|
290
|
+
current.value = cleaned;
|
|
291
|
+
current.inputs[current.field] = cleaned;
|
|
292
|
+
}
|
|
293
|
+
return validator;
|
|
294
|
+
},
|
|
282
295
|
sanitizeHTML(tags) {
|
|
283
296
|
if (shouldSkipValidation()) {
|
|
284
297
|
return validator;
|
|
@@ -325,6 +338,16 @@ function createValidator() {
|
|
|
325
338
|
current.pushError(message);
|
|
326
339
|
return validator;
|
|
327
340
|
},
|
|
341
|
+
test(validateFn, message) {
|
|
342
|
+
if (shouldSkipValidation()) return validator;
|
|
343
|
+
const isValid = validateFn(current.value, {
|
|
344
|
+
field: String(current.field)
|
|
345
|
+
});
|
|
346
|
+
if (!isValid) {
|
|
347
|
+
current.pushError(message);
|
|
348
|
+
}
|
|
349
|
+
return validator;
|
|
350
|
+
},
|
|
328
351
|
videoUrl: {
|
|
329
352
|
youtube(message) {
|
|
330
353
|
if (shouldSkipValidation()) {
|
package/package.json
CHANGED
package/src/helpers/validator.ts
CHANGED
|
@@ -264,6 +264,7 @@ export function createValidator<
|
|
|
264
264
|
}
|
|
265
265
|
return validator;
|
|
266
266
|
},
|
|
267
|
+
|
|
267
268
|
sanitize() {
|
|
268
269
|
if (shouldSkipValidation()) {
|
|
269
270
|
return validator;
|
|
@@ -279,6 +280,32 @@ export function createValidator<
|
|
|
279
280
|
return validator;
|
|
280
281
|
},
|
|
281
282
|
|
|
283
|
+
sanitizePreservingNewlines() {
|
|
284
|
+
if (shouldSkipValidation()) {
|
|
285
|
+
return validator;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (typeof current.value === 'string') {
|
|
289
|
+
// Passo 1: substitui quebras de linha por <br> para preservar no HTML
|
|
290
|
+
const withBrs = current.value.replace(/\n/g, '<br>');
|
|
291
|
+
|
|
292
|
+
// Passo 2: sanitiza permitindo a tag 'br'
|
|
293
|
+
const sanitized = sanitizeValue(withBrs, ['br']);
|
|
294
|
+
|
|
295
|
+
// Passo 3: opcional, limpa espaços extras, mas sem remover \n, porque já substituímos
|
|
296
|
+
const cleaned = sanitized
|
|
297
|
+
.trim()
|
|
298
|
+
.replace(/\s{2,}/g, ' ')
|
|
299
|
+
.replace(/<br>/g, '\n');
|
|
300
|
+
|
|
301
|
+
// Atualiza os valores
|
|
302
|
+
current.value = cleaned;
|
|
303
|
+
current.inputs[current.field] = cleaned as TRaw[keyof TRaw];
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return validator;
|
|
307
|
+
},
|
|
308
|
+
|
|
282
309
|
sanitizeHTML(tags?: AllowedTag[]) {
|
|
283
310
|
if (shouldSkipValidation()) {
|
|
284
311
|
return validator;
|
|
@@ -337,6 +364,23 @@ export function createValidator<
|
|
|
337
364
|
return validator;
|
|
338
365
|
},
|
|
339
366
|
|
|
367
|
+
test(
|
|
368
|
+
validateFn: (value: TInputValue, context: { field: string }) => boolean,
|
|
369
|
+
message: string
|
|
370
|
+
) {
|
|
371
|
+
if (shouldSkipValidation()) return validator;
|
|
372
|
+
|
|
373
|
+
const isValid = validateFn(current.value, {
|
|
374
|
+
field: String(current.field),
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
if (!isValid) {
|
|
378
|
+
current.pushError(message);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return validator;
|
|
382
|
+
},
|
|
383
|
+
|
|
340
384
|
videoUrl: {
|
|
341
385
|
youtube(message: string) {
|
|
342
386
|
if (shouldSkipValidation()) {
|