@excofy/utils 1.0.7 → 1.0.9

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
@@ -38,6 +38,60 @@ __export(index_exports, {
38
38
  });
39
39
  module.exports = __toCommonJS(index_exports);
40
40
 
41
+ // src/helpers/document.ts
42
+ function isValidCPF(cpf) {
43
+ if (!cpf) {
44
+ return false;
45
+ }
46
+ const multiplicador1 = [10, 9, 8, 7, 6, 5, 4, 3, 2];
47
+ const multiplicador2 = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2];
48
+ let tempCpf = cpf.slice(0, 9);
49
+ let soma = 0;
50
+ for (let i = 0; i < 9; i++) {
51
+ soma += Number.parseInt(tempCpf[i]) * multiplicador1[i];
52
+ }
53
+ let resto = soma % 11;
54
+ resto = resto < 2 ? 0 : 11 - resto;
55
+ let digito = resto.toString();
56
+ tempCpf += digito;
57
+ soma = 0;
58
+ for (let i = 0; i < 10; i++) {
59
+ soma += Number.parseInt(tempCpf[i]) * multiplicador2[i];
60
+ }
61
+ resto = soma % 11;
62
+ resto = resto < 2 ? 0 : 11 - resto;
63
+ digito += resto;
64
+ return cpf.endsWith(digito);
65
+ }
66
+ function isValidCNPJ(cnpj) {
67
+ function validCalc(x, numbers2) {
68
+ const slice = numbers2.slice(0, x);
69
+ let factor = x - 7;
70
+ let sum = 0;
71
+ for (let i = x; i >= 1; i--) {
72
+ const n = slice[x - i];
73
+ sum += n * factor--;
74
+ if (factor < 2) factor = 9;
75
+ }
76
+ const result = 11 - sum % 11;
77
+ return result > 9 ? 0 : result;
78
+ }
79
+ function matchNumbers(value = "") {
80
+ const match = value.toString().match(/\d/g);
81
+ return Array.isArray(match) ? match.map(Number) : [];
82
+ }
83
+ if (!cnpj) return false;
84
+ const numbers = matchNumbers(cnpj);
85
+ if (numbers.length !== 14) return false;
86
+ const items = new Set(numbers);
87
+ if (items.size === 1) return false;
88
+ const digits = numbers.slice(12);
89
+ const digit0 = validCalc(12, numbers);
90
+ if (digit0 !== digits[0]) return false;
91
+ const digit1 = validCalc(13, numbers);
92
+ return digit1 === digits[1];
93
+ }
94
+
41
95
  // src/helpers/sanitize.ts
42
96
  var import_xss = require("xss");
43
97
  var allTags = [
@@ -192,6 +246,32 @@ function createValidator() {
192
246
  return !current.required && (current.value === void 0 || current.value === null || current.value === "");
193
247
  };
194
248
  const validator = {
249
+ cnpj: (message) => {
250
+ if (shouldSkipValidation()) {
251
+ return validator;
252
+ }
253
+ if (typeof current.value === "string") {
254
+ if (!isValidCNPJ(current.value)) {
255
+ current.pushError(message);
256
+ }
257
+ } else {
258
+ current.pushError(message);
259
+ }
260
+ return validator;
261
+ },
262
+ cpf: (message) => {
263
+ if (shouldSkipValidation()) {
264
+ return validator;
265
+ }
266
+ if (typeof current.value === "string") {
267
+ if (!isValidCPF(current.value)) {
268
+ current.pushError(message);
269
+ }
270
+ } else {
271
+ current.pushError(message);
272
+ }
273
+ return validator;
274
+ },
195
275
  each(callback) {
196
276
  if (shouldSkipValidation()) {
197
277
  return validator;
package/dist/index.d.cts CHANGED
@@ -17,6 +17,34 @@ type TTypes = 'string' | 'number' | 'boolean' | 'object' | 'array' | 'file' | 'd
17
17
  interface IInputErrors {
18
18
  [key: string]: TMessage[] | IInputErrors[];
19
19
  }
20
+ interface ValidatorField {
21
+ cnpj(message: string): ValidatorField;
22
+ cpf(message: string): ValidatorField;
23
+ each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): ValidatorField;
24
+ email(message: string): ValidatorField;
25
+ fileMaxSize(maxSize: number, message: string): ValidatorField;
26
+ fileType(validTypes: string[], message: string): ValidatorField;
27
+ length(length: number, message: string): ValidatorField;
28
+ min(min: number, message: string): ValidatorField;
29
+ max(max: number, message: string): ValidatorField;
30
+ transform<U>(fn: (value: TInputValue) => U): ValidatorField;
31
+ slug(message: string): ValidatorField;
32
+ sanitize(): ValidatorField;
33
+ sanitizePreservingNewlines(): ValidatorField;
34
+ sanitizeHTML(tags?: AllowedTag[]): ValidatorField;
35
+ url(message: string): ValidatorField;
36
+ uuid(message: string): ValidatorField;
37
+ oneOf(types: string[], message: string): ValidatorField;
38
+ test(validateFn: (value: TValue, context: {
39
+ field: string;
40
+ }) => boolean, message: string): ValidatorField;
41
+ videoUrl: {
42
+ youtube(message: string): ValidatorField;
43
+ };
44
+ asNumber(message: string): ValidatorField;
45
+ asBoolean(message: string): ValidatorField;
46
+ asStringArray(message: string): ValidatorField;
47
+ }
20
48
  /**
21
49
  * Cria um validador de inputs com suporte a validações encadeadas, transformação e sanitização.
22
50
  *
@@ -43,88 +71,13 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
43
71
  unflattenErrors(flat: Record<string, TMessage[]>): Record<string, unknown>;
44
72
  validate<K extends keyof TRaw>(field: K): {
45
73
  isRequired(message: string): {
46
- type(type: TTypes, message: string): {
47
- each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): /*elided*/ any;
48
- email: (message: string) => /*elided*/ any;
49
- fileMaxSize: (maxSize: number, message: string) => /*elided*/ any;
50
- fileType: (validTypes: string[], message: string) => /*elided*/ any;
51
- length(length: number, message: string): /*elided*/ any;
52
- min(min: number, message: string): /*elided*/ any;
53
- max(max: number, message: string): /*elided*/ any;
54
- transform<U>(fn: (value: TInputValue) => U): /*elided*/ any;
55
- slug(message: string): /*elided*/ any;
56
- sanitize(): /*elided*/ any;
57
- sanitizePreservingNewlines(): /*elided*/ any;
58
- sanitizeHTML(tags?: AllowedTag[]): /*elided*/ any;
59
- url(message: string): /*elided*/ any;
60
- uuid(message: string): /*elided*/ any;
61
- oneOf: (types: string[], message: string) => /*elided*/ any;
62
- test(validateFn: (value: TInputValue, context: {
63
- field: string;
64
- }) => boolean, message: string): /*elided*/ any;
65
- videoUrl: {
66
- youtube(message: string): /*elided*/ any;
67
- };
68
- asNumber(message: string): /*elided*/ any;
69
- asBoolean(message: string): /*elided*/ any;
70
- asStringArray(message: string): /*elided*/ any;
71
- };
74
+ type(type: TTypes, message: string): ValidatorField;
72
75
  };
73
76
  isNotRequired(): {
74
- type(type: TTypes, message: string): {
75
- each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): /*elided*/ any;
76
- email: (message: string) => /*elided*/ any;
77
- fileMaxSize: (maxSize: number, message: string) => /*elided*/ any;
78
- fileType: (validTypes: string[], message: string) => /*elided*/ any;
79
- length(length: number, message: string): /*elided*/ any;
80
- min(min: number, message: string): /*elided*/ any;
81
- max(max: number, message: string): /*elided*/ any;
82
- transform<U>(fn: (value: TInputValue) => U): /*elided*/ any;
83
- slug(message: string): /*elided*/ any;
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
- };
77
+ type(type: TTypes, message: string): ValidatorField;
100
78
  };
101
79
  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;
114
- sanitizeHTML(tags?: AllowedTag[]): /*elided*/ any;
115
- url(message: string): /*elided*/ any;
116
- uuid(message: string): /*elided*/ any;
117
- oneOf: (types: string[], message: string) => /*elided*/ any;
118
- test(validateFn: (value: TInputValue, context: {
119
- field: string;
120
- }) => boolean, message: string): /*elided*/ any;
121
- videoUrl: {
122
- youtube(message: string): /*elided*/ any;
123
- };
124
- asNumber(message: string): /*elided*/ any;
125
- asBoolean(message: string): /*elided*/ any;
126
- asStringArray(message: string): /*elided*/ any;
127
- };
80
+ type(type: TTypes, message: string): ValidatorField;
128
81
  };
129
82
  };
130
83
  };
package/dist/index.d.ts CHANGED
@@ -17,6 +17,34 @@ type TTypes = 'string' | 'number' | 'boolean' | 'object' | 'array' | 'file' | 'd
17
17
  interface IInputErrors {
18
18
  [key: string]: TMessage[] | IInputErrors[];
19
19
  }
20
+ interface ValidatorField {
21
+ cnpj(message: string): ValidatorField;
22
+ cpf(message: string): ValidatorField;
23
+ each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): ValidatorField;
24
+ email(message: string): ValidatorField;
25
+ fileMaxSize(maxSize: number, message: string): ValidatorField;
26
+ fileType(validTypes: string[], message: string): ValidatorField;
27
+ length(length: number, message: string): ValidatorField;
28
+ min(min: number, message: string): ValidatorField;
29
+ max(max: number, message: string): ValidatorField;
30
+ transform<U>(fn: (value: TInputValue) => U): ValidatorField;
31
+ slug(message: string): ValidatorField;
32
+ sanitize(): ValidatorField;
33
+ sanitizePreservingNewlines(): ValidatorField;
34
+ sanitizeHTML(tags?: AllowedTag[]): ValidatorField;
35
+ url(message: string): ValidatorField;
36
+ uuid(message: string): ValidatorField;
37
+ oneOf(types: string[], message: string): ValidatorField;
38
+ test(validateFn: (value: TValue, context: {
39
+ field: string;
40
+ }) => boolean, message: string): ValidatorField;
41
+ videoUrl: {
42
+ youtube(message: string): ValidatorField;
43
+ };
44
+ asNumber(message: string): ValidatorField;
45
+ asBoolean(message: string): ValidatorField;
46
+ asStringArray(message: string): ValidatorField;
47
+ }
20
48
  /**
21
49
  * Cria um validador de inputs com suporte a validações encadeadas, transformação e sanitização.
22
50
  *
@@ -43,88 +71,13 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
43
71
  unflattenErrors(flat: Record<string, TMessage[]>): Record<string, unknown>;
44
72
  validate<K extends keyof TRaw>(field: K): {
45
73
  isRequired(message: string): {
46
- type(type: TTypes, message: string): {
47
- each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): /*elided*/ any;
48
- email: (message: string) => /*elided*/ any;
49
- fileMaxSize: (maxSize: number, message: string) => /*elided*/ any;
50
- fileType: (validTypes: string[], message: string) => /*elided*/ any;
51
- length(length: number, message: string): /*elided*/ any;
52
- min(min: number, message: string): /*elided*/ any;
53
- max(max: number, message: string): /*elided*/ any;
54
- transform<U>(fn: (value: TInputValue) => U): /*elided*/ any;
55
- slug(message: string): /*elided*/ any;
56
- sanitize(): /*elided*/ any;
57
- sanitizePreservingNewlines(): /*elided*/ any;
58
- sanitizeHTML(tags?: AllowedTag[]): /*elided*/ any;
59
- url(message: string): /*elided*/ any;
60
- uuid(message: string): /*elided*/ any;
61
- oneOf: (types: string[], message: string) => /*elided*/ any;
62
- test(validateFn: (value: TInputValue, context: {
63
- field: string;
64
- }) => boolean, message: string): /*elided*/ any;
65
- videoUrl: {
66
- youtube(message: string): /*elided*/ any;
67
- };
68
- asNumber(message: string): /*elided*/ any;
69
- asBoolean(message: string): /*elided*/ any;
70
- asStringArray(message: string): /*elided*/ any;
71
- };
74
+ type(type: TTypes, message: string): ValidatorField;
72
75
  };
73
76
  isNotRequired(): {
74
- type(type: TTypes, message: string): {
75
- each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): /*elided*/ any;
76
- email: (message: string) => /*elided*/ any;
77
- fileMaxSize: (maxSize: number, message: string) => /*elided*/ any;
78
- fileType: (validTypes: string[], message: string) => /*elided*/ any;
79
- length(length: number, message: string): /*elided*/ any;
80
- min(min: number, message: string): /*elided*/ any;
81
- max(max: number, message: string): /*elided*/ any;
82
- transform<U>(fn: (value: TInputValue) => U): /*elided*/ any;
83
- slug(message: string): /*elided*/ any;
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
- };
77
+ type(type: TTypes, message: string): ValidatorField;
100
78
  };
101
79
  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;
114
- sanitizeHTML(tags?: AllowedTag[]): /*elided*/ any;
115
- url(message: string): /*elided*/ any;
116
- uuid(message: string): /*elided*/ any;
117
- oneOf: (types: string[], message: string) => /*elided*/ any;
118
- test(validateFn: (value: TInputValue, context: {
119
- field: string;
120
- }) => boolean, message: string): /*elided*/ any;
121
- videoUrl: {
122
- youtube(message: string): /*elided*/ any;
123
- };
124
- asNumber(message: string): /*elided*/ any;
125
- asBoolean(message: string): /*elided*/ any;
126
- asStringArray(message: string): /*elided*/ any;
127
- };
80
+ type(type: TTypes, message: string): ValidatorField;
128
81
  };
129
82
  };
130
83
  };
package/dist/index.js CHANGED
@@ -4,6 +4,60 @@ var __export = (target, all) => {
4
4
  __defProp(target, name, { get: all[name], enumerable: true });
5
5
  };
6
6
 
7
+ // src/helpers/document.ts
8
+ function isValidCPF(cpf) {
9
+ if (!cpf) {
10
+ return false;
11
+ }
12
+ const multiplicador1 = [10, 9, 8, 7, 6, 5, 4, 3, 2];
13
+ const multiplicador2 = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2];
14
+ let tempCpf = cpf.slice(0, 9);
15
+ let soma = 0;
16
+ for (let i = 0; i < 9; i++) {
17
+ soma += Number.parseInt(tempCpf[i]) * multiplicador1[i];
18
+ }
19
+ let resto = soma % 11;
20
+ resto = resto < 2 ? 0 : 11 - resto;
21
+ let digito = resto.toString();
22
+ tempCpf += digito;
23
+ soma = 0;
24
+ for (let i = 0; i < 10; i++) {
25
+ soma += Number.parseInt(tempCpf[i]) * multiplicador2[i];
26
+ }
27
+ resto = soma % 11;
28
+ resto = resto < 2 ? 0 : 11 - resto;
29
+ digito += resto;
30
+ return cpf.endsWith(digito);
31
+ }
32
+ function isValidCNPJ(cnpj) {
33
+ function validCalc(x, numbers2) {
34
+ const slice = numbers2.slice(0, x);
35
+ let factor = x - 7;
36
+ let sum = 0;
37
+ for (let i = x; i >= 1; i--) {
38
+ const n = slice[x - i];
39
+ sum += n * factor--;
40
+ if (factor < 2) factor = 9;
41
+ }
42
+ const result = 11 - sum % 11;
43
+ return result > 9 ? 0 : result;
44
+ }
45
+ function matchNumbers(value = "") {
46
+ const match = value.toString().match(/\d/g);
47
+ return Array.isArray(match) ? match.map(Number) : [];
48
+ }
49
+ if (!cnpj) return false;
50
+ const numbers = matchNumbers(cnpj);
51
+ if (numbers.length !== 14) return false;
52
+ const items = new Set(numbers);
53
+ if (items.size === 1) return false;
54
+ const digits = numbers.slice(12);
55
+ const digit0 = validCalc(12, numbers);
56
+ if (digit0 !== digits[0]) return false;
57
+ const digit1 = validCalc(13, numbers);
58
+ return digit1 === digits[1];
59
+ }
60
+
7
61
  // src/helpers/sanitize.ts
8
62
  import { FilterXSS } from "xss";
9
63
  var allTags = [
@@ -158,6 +212,32 @@ function createValidator() {
158
212
  return !current.required && (current.value === void 0 || current.value === null || current.value === "");
159
213
  };
160
214
  const validator = {
215
+ cnpj: (message) => {
216
+ if (shouldSkipValidation()) {
217
+ return validator;
218
+ }
219
+ if (typeof current.value === "string") {
220
+ if (!isValidCNPJ(current.value)) {
221
+ current.pushError(message);
222
+ }
223
+ } else {
224
+ current.pushError(message);
225
+ }
226
+ return validator;
227
+ },
228
+ cpf: (message) => {
229
+ if (shouldSkipValidation()) {
230
+ return validator;
231
+ }
232
+ if (typeof current.value === "string") {
233
+ if (!isValidCPF(current.value)) {
234
+ current.pushError(message);
235
+ }
236
+ } else {
237
+ current.pushError(message);
238
+ }
239
+ return validator;
240
+ },
161
241
  each(callback) {
162
242
  if (shouldSkipValidation()) {
163
243
  return validator;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@excofy/utils",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "Biblioteca de utilitários para o Excofy",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -0,0 +1,77 @@
1
+ export function isValidCPF(cpf: string): boolean {
2
+ if (!cpf) {
3
+ return false;
4
+ }
5
+
6
+ const multiplicador1 = [10, 9, 8, 7, 6, 5, 4, 3, 2];
7
+ const multiplicador2 = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2];
8
+ let tempCpf = cpf.slice(0, 9);
9
+ let soma = 0;
10
+
11
+ for (let i = 0; i < 9; i++) {
12
+ soma += Number.parseInt(tempCpf[i]) * multiplicador1[i];
13
+ }
14
+
15
+ let resto = soma % 11;
16
+ resto = resto < 2 ? 0 : 11 - resto;
17
+
18
+ let digito = resto.toString();
19
+ tempCpf += digito;
20
+ soma = 0;
21
+
22
+ for (let i = 0; i < 10; i++) {
23
+ soma += Number.parseInt(tempCpf[i]) * multiplicador2[i];
24
+ }
25
+
26
+ resto = soma % 11;
27
+ resto = resto < 2 ? 0 : 11 - resto;
28
+ digito += resto;
29
+
30
+ return cpf.endsWith(digito);
31
+ }
32
+
33
+ export function isValidCNPJ(cnpj: string): boolean {
34
+ function validCalc(x: number, numbers: number[]) {
35
+ const slice = numbers.slice(0, x);
36
+ let factor = x - 7;
37
+ let sum = 0;
38
+
39
+ for (let i = x; i >= 1; i--) {
40
+ const n = slice[x - i];
41
+ sum += n * factor--;
42
+ if (factor < 2) factor = 9;
43
+ }
44
+
45
+ const result = 11 - (sum % 11);
46
+
47
+ return result > 9 ? 0 : result;
48
+ }
49
+
50
+ function matchNumbers(value: string | number | number[] = '') {
51
+ const match = value.toString().match(/\d/g);
52
+ return Array.isArray(match) ? match.map(Number) : [];
53
+ }
54
+
55
+ if (!cnpj) return false;
56
+
57
+ // Elimina tudo que não é dígito
58
+ const numbers = matchNumbers(cnpj);
59
+
60
+ // Valida a quantidade de dígitos
61
+ if (numbers.length !== 14) return false;
62
+
63
+ // Elimina inválidos com todos os dígitos iguais
64
+ const items = new Set(numbers);
65
+ if (items.size === 1) return false;
66
+
67
+ // Separa os 2 últimos dígitos verificadores
68
+ const digits = numbers.slice(12);
69
+
70
+ // Valida 1o. dígito verificador
71
+ const digit0 = validCalc(12, numbers);
72
+ if (digit0 !== digits[0]) return false;
73
+
74
+ // Valida 2o. dígito verificador
75
+ const digit1 = validCalc(13, numbers);
76
+ return digit1 === digits[1];
77
+ }
@@ -1,3 +1,4 @@
1
+ import { isValidCNPJ, isValidCPF } from './document';
1
2
  import { sanitizeValue, type AllowedTag } from './sanitize';
2
3
  import { video } from './video';
3
4
 
@@ -17,6 +18,42 @@ interface IInputErrors {
17
18
  [key: string]: TMessage[] | IInputErrors[];
18
19
  }
19
20
 
21
+ interface ValidatorField {
22
+ cnpj(message: string): ValidatorField;
23
+ cpf(message: string): ValidatorField;
24
+ each(
25
+ callback: <U extends Record<string, TInputValue>>(
26
+ item: U,
27
+ index: number,
28
+ subValidator: ReturnType<typeof createValidator<U>>
29
+ ) => void
30
+ ): ValidatorField;
31
+ email(message: string): ValidatorField;
32
+ fileMaxSize(maxSize: number, message: string): ValidatorField;
33
+ fileType(validTypes: string[], message: string): ValidatorField;
34
+ length(length: number, message: string): ValidatorField;
35
+ min(min: number, message: string): ValidatorField;
36
+ max(max: number, message: string): ValidatorField;
37
+ transform<U>(fn: (value: TInputValue) => U): ValidatorField;
38
+ slug(message: string): ValidatorField;
39
+ sanitize(): ValidatorField;
40
+ sanitizePreservingNewlines(): ValidatorField;
41
+ sanitizeHTML(tags?: AllowedTag[]): ValidatorField;
42
+ url(message: string): ValidatorField;
43
+ uuid(message: string): ValidatorField;
44
+ oneOf(types: string[], message: string): ValidatorField;
45
+ test(
46
+ validateFn: (value: TValue, context: { field: string }) => boolean,
47
+ message: string
48
+ ): ValidatorField;
49
+ videoUrl: {
50
+ youtube(message: string): ValidatorField;
51
+ };
52
+ asNumber(message: string): ValidatorField;
53
+ asBoolean(message: string): ValidatorField;
54
+ asStringArray(message: string): ValidatorField;
55
+ }
56
+
20
57
  /**
21
58
  * Cria um validador de inputs com suporte a validações encadeadas, transformação e sanitização.
22
59
  *
@@ -127,7 +164,37 @@ export function createValidator<
127
164
  );
128
165
  };
129
166
 
130
- const validator = {
167
+ const validator: ValidatorField = {
168
+ cnpj: (message: string) => {
169
+ if (shouldSkipValidation()) {
170
+ return validator;
171
+ }
172
+
173
+ if (typeof current.value === 'string') {
174
+ if (!isValidCNPJ(current.value)) {
175
+ current.pushError(message);
176
+ }
177
+ } else {
178
+ current.pushError(message);
179
+ }
180
+
181
+ return validator;
182
+ },
183
+ cpf: (message: string) => {
184
+ if (shouldSkipValidation()) {
185
+ return validator;
186
+ }
187
+
188
+ if (typeof current.value === 'string') {
189
+ if (!isValidCPF(current.value)) {
190
+ current.pushError(message);
191
+ }
192
+ } else {
193
+ current.pushError(message);
194
+ }
195
+
196
+ return validator;
197
+ },
131
198
  each(
132
199
  callback: <U extends Record<string, TInputValue>>(
133
200
  item: U,