@excofy/utils 1.0.6 → 1.0.8

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 = [
@@ -154,6 +208,13 @@ function createValidator() {
154
208
  isNotRequired() {
155
209
  current.required = false;
156
210
  return validatorType;
211
+ },
212
+ conditionallyRequired(isRequired, message) {
213
+ current.required = isRequired;
214
+ if (isRequired && (current.value === void 0 || current.value === null || current.value === "")) {
215
+ current.pushError(message);
216
+ }
217
+ return validatorType;
157
218
  }
158
219
  };
159
220
  const validatorType = {
@@ -185,6 +246,32 @@ function createValidator() {
185
246
  return !current.required && (current.value === void 0 || current.value === null || current.value === "");
186
247
  };
187
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
+ },
188
275
  each(callback) {
189
276
  if (shouldSkipValidation()) {
190
277
  return validator;
package/dist/index.d.cts CHANGED
@@ -44,6 +44,8 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
44
44
  validate<K extends keyof TRaw>(field: K): {
45
45
  isRequired(message: string): {
46
46
  type(type: TTypes, message: string): {
47
+ cnpj: (message: string) => /*elided*/ any;
48
+ cpf: (message: string) => /*elided*/ any;
47
49
  each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): /*elided*/ any;
48
50
  email: (message: string) => /*elided*/ any;
49
51
  fileMaxSize: (maxSize: number, message: string) => /*elided*/ any;
@@ -72,6 +74,38 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
72
74
  };
73
75
  isNotRequired(): {
74
76
  type(type: TTypes, message: string): {
77
+ cnpj: (message: string) => /*elided*/ any;
78
+ cpf: (message: string) => /*elided*/ any;
79
+ each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): /*elided*/ any;
80
+ email: (message: string) => /*elided*/ any;
81
+ fileMaxSize: (maxSize: number, message: string) => /*elided*/ any;
82
+ fileType: (validTypes: string[], message: string) => /*elided*/ any;
83
+ length(length: number, message: string): /*elided*/ any;
84
+ min(min: number, message: string): /*elided*/ any;
85
+ max(max: number, message: string): /*elided*/ any;
86
+ transform<U>(fn: (value: TInputValue) => U): /*elided*/ any;
87
+ slug(message: string): /*elided*/ any;
88
+ sanitize(): /*elided*/ any;
89
+ sanitizePreservingNewlines(): /*elided*/ any;
90
+ sanitizeHTML(tags?: AllowedTag[]): /*elided*/ any;
91
+ url(message: string): /*elided*/ any;
92
+ uuid(message: string): /*elided*/ any;
93
+ oneOf: (types: string[], message: string) => /*elided*/ any;
94
+ test(validateFn: (value: TInputValue, context: {
95
+ field: string;
96
+ }) => boolean, message: string): /*elided*/ any;
97
+ videoUrl: {
98
+ youtube(message: string): /*elided*/ any;
99
+ };
100
+ asNumber(message: string): /*elided*/ any;
101
+ asBoolean(message: string): /*elided*/ any;
102
+ asStringArray(message: string): /*elided*/ any;
103
+ };
104
+ };
105
+ conditionallyRequired(isRequired: boolean, message: string): {
106
+ type(type: TTypes, message: string): {
107
+ cnpj: (message: string) => /*elided*/ any;
108
+ cpf: (message: string) => /*elided*/ any;
75
109
  each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): /*elided*/ any;
76
110
  email: (message: string) => /*elided*/ any;
77
111
  fileMaxSize: (maxSize: number, message: string) => /*elided*/ any;
package/dist/index.d.ts CHANGED
@@ -44,6 +44,8 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
44
44
  validate<K extends keyof TRaw>(field: K): {
45
45
  isRequired(message: string): {
46
46
  type(type: TTypes, message: string): {
47
+ cnpj: (message: string) => /*elided*/ any;
48
+ cpf: (message: string) => /*elided*/ any;
47
49
  each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): /*elided*/ any;
48
50
  email: (message: string) => /*elided*/ any;
49
51
  fileMaxSize: (maxSize: number, message: string) => /*elided*/ any;
@@ -72,6 +74,38 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
72
74
  };
73
75
  isNotRequired(): {
74
76
  type(type: TTypes, message: string): {
77
+ cnpj: (message: string) => /*elided*/ any;
78
+ cpf: (message: string) => /*elided*/ any;
79
+ each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): /*elided*/ any;
80
+ email: (message: string) => /*elided*/ any;
81
+ fileMaxSize: (maxSize: number, message: string) => /*elided*/ any;
82
+ fileType: (validTypes: string[], message: string) => /*elided*/ any;
83
+ length(length: number, message: string): /*elided*/ any;
84
+ min(min: number, message: string): /*elided*/ any;
85
+ max(max: number, message: string): /*elided*/ any;
86
+ transform<U>(fn: (value: TInputValue) => U): /*elided*/ any;
87
+ slug(message: string): /*elided*/ any;
88
+ sanitize(): /*elided*/ any;
89
+ sanitizePreservingNewlines(): /*elided*/ any;
90
+ sanitizeHTML(tags?: AllowedTag[]): /*elided*/ any;
91
+ url(message: string): /*elided*/ any;
92
+ uuid(message: string): /*elided*/ any;
93
+ oneOf: (types: string[], message: string) => /*elided*/ any;
94
+ test(validateFn: (value: TInputValue, context: {
95
+ field: string;
96
+ }) => boolean, message: string): /*elided*/ any;
97
+ videoUrl: {
98
+ youtube(message: string): /*elided*/ any;
99
+ };
100
+ asNumber(message: string): /*elided*/ any;
101
+ asBoolean(message: string): /*elided*/ any;
102
+ asStringArray(message: string): /*elided*/ any;
103
+ };
104
+ };
105
+ conditionallyRequired(isRequired: boolean, message: string): {
106
+ type(type: TTypes, message: string): {
107
+ cnpj: (message: string) => /*elided*/ any;
108
+ cpf: (message: string) => /*elided*/ any;
75
109
  each(callback: <U extends Record<string, TInputValue>>(item: U, index: number, subValidator: ReturnType<typeof createValidator<U>>) => void): /*elided*/ any;
76
110
  email: (message: string) => /*elided*/ any;
77
111
  fileMaxSize: (maxSize: number, message: string) => /*elided*/ any;
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 = [
@@ -120,6 +174,13 @@ function createValidator() {
120
174
  isNotRequired() {
121
175
  current.required = false;
122
176
  return validatorType;
177
+ },
178
+ conditionallyRequired(isRequired, message) {
179
+ current.required = isRequired;
180
+ if (isRequired && (current.value === void 0 || current.value === null || current.value === "")) {
181
+ current.pushError(message);
182
+ }
183
+ return validatorType;
123
184
  }
124
185
  };
125
186
  const validatorType = {
@@ -151,6 +212,32 @@ function createValidator() {
151
212
  return !current.required && (current.value === void 0 || current.value === null || current.value === "");
152
213
  };
153
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
+ },
154
241
  each(callback) {
155
242
  if (shouldSkipValidation()) {
156
243
  return validator;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@excofy/utils",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
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
 
@@ -68,6 +69,18 @@ export function createValidator<
68
69
  current.required = false;
69
70
  return validatorType;
70
71
  },
72
+ conditionallyRequired(isRequired: boolean, message: string) {
73
+ current.required = isRequired;
74
+ if (
75
+ isRequired &&
76
+ (current.value === undefined ||
77
+ current.value === null ||
78
+ current.value === '')
79
+ ) {
80
+ current.pushError(message);
81
+ }
82
+ return validatorType;
83
+ },
71
84
  };
72
85
 
73
86
  const validatorType = {
@@ -116,6 +129,36 @@ export function createValidator<
116
129
  };
117
130
 
118
131
  const validator = {
132
+ cnpj: (message: string) => {
133
+ if (shouldSkipValidation()) {
134
+ return validator;
135
+ }
136
+
137
+ if (typeof current.value === 'string') {
138
+ if (!isValidCNPJ(current.value)) {
139
+ current.pushError(message);
140
+ }
141
+ } else {
142
+ current.pushError(message);
143
+ }
144
+
145
+ return validator;
146
+ },
147
+ cpf: (message: string) => {
148
+ if (shouldSkipValidation()) {
149
+ return validator;
150
+ }
151
+
152
+ if (typeof current.value === 'string') {
153
+ if (!isValidCPF(current.value)) {
154
+ current.pushError(message);
155
+ }
156
+ } else {
157
+ current.pushError(message);
158
+ }
159
+
160
+ return validator;
161
+ },
119
162
  each(
120
163
  callback: <U extends Record<string, TInputValue>>(
121
164
  item: U,