@excofy/utils 1.0.5 → 1.0.7

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
@@ -154,6 +154,13 @@ function createValidator() {
154
154
  isNotRequired() {
155
155
  current.required = false;
156
156
  return validatorType;
157
+ },
158
+ conditionallyRequired(isRequired, message) {
159
+ current.required = isRequired;
160
+ if (isRequired && (current.value === void 0 || current.value === null || current.value === "")) {
161
+ current.pushError(message);
162
+ }
163
+ return validatorType;
157
164
  }
158
165
  };
159
166
  const validatorType = {
@@ -313,6 +320,19 @@ function createValidator() {
313
320
  }
314
321
  return validator;
315
322
  },
323
+ sanitizePreservingNewlines() {
324
+ if (shouldSkipValidation()) {
325
+ return validator;
326
+ }
327
+ if (typeof current.value === "string") {
328
+ const withBrs = current.value.replace(/\n/g, "<br>");
329
+ const sanitized = sanitizeValue(withBrs, ["br"]);
330
+ const cleaned = sanitized.trim().replace(/\s{2,}/g, " ").replace(/<br>/g, "\n");
331
+ current.value = cleaned;
332
+ current.inputs[current.field] = cleaned;
333
+ }
334
+ return validator;
335
+ },
316
336
  sanitizeHTML(tags) {
317
337
  if (shouldSkipValidation()) {
318
338
  return validator;
package/dist/index.d.cts CHANGED
@@ -54,6 +54,7 @@ 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;
@@ -81,6 +82,35 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
81
82
  transform<U>(fn: (value: TInputValue) => U): /*elided*/ any;
82
83
  slug(message: string): /*elided*/ any;
83
84
  sanitize(): /*elided*/ any;
85
+ sanitizePreservingNewlines(): /*elided*/ any;
86
+ sanitizeHTML(tags?: AllowedTag[]): /*elided*/ any;
87
+ url(message: string): /*elided*/ any;
88
+ uuid(message: string): /*elided*/ any;
89
+ oneOf: (types: string[], message: string) => /*elided*/ any;
90
+ test(validateFn: (value: TInputValue, context: {
91
+ field: string;
92
+ }) => boolean, message: string): /*elided*/ any;
93
+ videoUrl: {
94
+ youtube(message: string): /*elided*/ any;
95
+ };
96
+ asNumber(message: string): /*elided*/ any;
97
+ asBoolean(message: string): /*elided*/ any;
98
+ asStringArray(message: string): /*elided*/ any;
99
+ };
100
+ };
101
+ conditionallyRequired(isRequired: boolean, message: string): {
102
+ type(type: TTypes, message: string): {
103
+ each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): /*elided*/ any;
104
+ email: (message: string) => /*elided*/ any;
105
+ fileMaxSize: (maxSize: number, message: string) => /*elided*/ any;
106
+ fileType: (validTypes: string[], message: string) => /*elided*/ any;
107
+ length(length: number, message: string): /*elided*/ any;
108
+ min(min: number, message: string): /*elided*/ any;
109
+ max(max: number, message: string): /*elided*/ any;
110
+ transform<U>(fn: (value: TInputValue) => U): /*elided*/ any;
111
+ slug(message: string): /*elided*/ any;
112
+ sanitize(): /*elided*/ any;
113
+ sanitizePreservingNewlines(): /*elided*/ any;
84
114
  sanitizeHTML(tags?: AllowedTag[]): /*elided*/ any;
85
115
  url(message: string): /*elided*/ any;
86
116
  uuid(message: string): /*elided*/ any;
package/dist/index.d.ts CHANGED
@@ -54,6 +54,7 @@ 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;
@@ -81,6 +82,35 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
81
82
  transform<U>(fn: (value: TInputValue) => U): /*elided*/ any;
82
83
  slug(message: string): /*elided*/ any;
83
84
  sanitize(): /*elided*/ any;
85
+ sanitizePreservingNewlines(): /*elided*/ any;
86
+ sanitizeHTML(tags?: AllowedTag[]): /*elided*/ any;
87
+ url(message: string): /*elided*/ any;
88
+ uuid(message: string): /*elided*/ any;
89
+ oneOf: (types: string[], message: string) => /*elided*/ any;
90
+ test(validateFn: (value: TInputValue, context: {
91
+ field: string;
92
+ }) => boolean, message: string): /*elided*/ any;
93
+ videoUrl: {
94
+ youtube(message: string): /*elided*/ any;
95
+ };
96
+ asNumber(message: string): /*elided*/ any;
97
+ asBoolean(message: string): /*elided*/ any;
98
+ asStringArray(message: string): /*elided*/ any;
99
+ };
100
+ };
101
+ conditionallyRequired(isRequired: boolean, message: string): {
102
+ type(type: TTypes, message: string): {
103
+ each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): /*elided*/ any;
104
+ email: (message: string) => /*elided*/ any;
105
+ fileMaxSize: (maxSize: number, message: string) => /*elided*/ any;
106
+ fileType: (validTypes: string[], message: string) => /*elided*/ any;
107
+ length(length: number, message: string): /*elided*/ any;
108
+ min(min: number, message: string): /*elided*/ any;
109
+ max(max: number, message: string): /*elided*/ any;
110
+ transform<U>(fn: (value: TInputValue) => U): /*elided*/ any;
111
+ slug(message: string): /*elided*/ any;
112
+ sanitize(): /*elided*/ any;
113
+ sanitizePreservingNewlines(): /*elided*/ any;
84
114
  sanitizeHTML(tags?: AllowedTag[]): /*elided*/ any;
85
115
  url(message: string): /*elided*/ any;
86
116
  uuid(message: string): /*elided*/ any;
package/dist/index.js CHANGED
@@ -120,6 +120,13 @@ function createValidator() {
120
120
  isNotRequired() {
121
121
  current.required = false;
122
122
  return validatorType;
123
+ },
124
+ conditionallyRequired(isRequired, message) {
125
+ current.required = isRequired;
126
+ if (isRequired && (current.value === void 0 || current.value === null || current.value === "")) {
127
+ current.pushError(message);
128
+ }
129
+ return validatorType;
123
130
  }
124
131
  };
125
132
  const validatorType = {
@@ -279,6 +286,19 @@ function createValidator() {
279
286
  }
280
287
  return validator;
281
288
  },
289
+ sanitizePreservingNewlines() {
290
+ if (shouldSkipValidation()) {
291
+ return validator;
292
+ }
293
+ if (typeof current.value === "string") {
294
+ const withBrs = current.value.replace(/\n/g, "<br>");
295
+ const sanitized = sanitizeValue(withBrs, ["br"]);
296
+ const cleaned = sanitized.trim().replace(/\s{2,}/g, " ").replace(/<br>/g, "\n");
297
+ current.value = cleaned;
298
+ current.inputs[current.field] = cleaned;
299
+ }
300
+ return validator;
301
+ },
282
302
  sanitizeHTML(tags) {
283
303
  if (shouldSkipValidation()) {
284
304
  return validator;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@excofy/utils",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "Biblioteca de utilitários para o Excofy",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -68,6 +68,18 @@ export function createValidator<
68
68
  current.required = false;
69
69
  return validatorType;
70
70
  },
71
+ conditionallyRequired(isRequired: boolean, message: string) {
72
+ current.required = isRequired;
73
+ if (
74
+ isRequired &&
75
+ (current.value === undefined ||
76
+ current.value === null ||
77
+ current.value === '')
78
+ ) {
79
+ current.pushError(message);
80
+ }
81
+ return validatorType;
82
+ },
71
83
  };
72
84
 
73
85
  const validatorType = {
@@ -264,6 +276,7 @@ export function createValidator<
264
276
  }
265
277
  return validator;
266
278
  },
279
+
267
280
  sanitize() {
268
281
  if (shouldSkipValidation()) {
269
282
  return validator;
@@ -279,6 +292,32 @@ export function createValidator<
279
292
  return validator;
280
293
  },
281
294
 
295
+ sanitizePreservingNewlines() {
296
+ if (shouldSkipValidation()) {
297
+ return validator;
298
+ }
299
+
300
+ if (typeof current.value === 'string') {
301
+ // Passo 1: substitui quebras de linha por <br> para preservar no HTML
302
+ const withBrs = current.value.replace(/\n/g, '<br>');
303
+
304
+ // Passo 2: sanitiza permitindo a tag 'br'
305
+ const sanitized = sanitizeValue(withBrs, ['br']);
306
+
307
+ // Passo 3: opcional, limpa espaços extras, mas sem remover \n, porque já substituímos
308
+ const cleaned = sanitized
309
+ .trim()
310
+ .replace(/\s{2,}/g, ' ')
311
+ .replace(/<br>/g, '\n');
312
+
313
+ // Atualiza os valores
314
+ current.value = cleaned;
315
+ current.inputs[current.field] = cleaned as TRaw[keyof TRaw];
316
+ }
317
+
318
+ return validator;
319
+ },
320
+
282
321
  sanitizeHTML(tags?: AllowedTag[]) {
283
322
  if (shouldSkipValidation()) {
284
323
  return validator;